C#客户端Redis服务器的分布式缓存

2019-12-30 11:51:33于丽

类型化实体集更有意思和更实用,这是因为它们操作的是确切类型的对象。在下面的代码示例中,有两个类分别定义为Phone和Person——phone的主人。每个phone实例引用它的主人。下面的代码演示我们如何通过标准添加、删除和发现缓存项:

 



    public class Phone  {     public int Id { get; set; }     public string Model { get; set; }     public string Manufacturer { get; set; }     public Person Owner { get; set; }  }    public class Person  {      public int Id { get; set; }      public string Name { get; set; }      public string Surname { get; set; }      public int Age { get; set; }      public string Profession { get; set; }  }    using (RedisClient redisClient = new RedisClient(host))  {       IRedisTypedClient<phone> phones = redisClient.As<phone>();       Phone phoneFive = phones.GetValue("5");       if (phoneFive == null)       {            // make a small delay            Thread.Sleep(5000);            // creating a new Phone entry            phoneFive = new Phone            {                 Id = 5,                 Manufacturer = "Motorolla",                 Model = "xxxxx",                 Owner = new Person                 {                      Id = 1,                      Age = 90,                      Name = "OldOne",                      Profession = "sportsmen",                      Surname = "OldManSurname"                 }            };            // adding Entry to the typed entity set            phones.SetEntry(phoneFive.Id.ToString(), phoneFive);       }       message = "Phone model is " + phoneFive.Manufacturer;       message += "Phone Owner Name is: " + phoneFive.Owner.Name;  }