C# Redis Server分布式緩存編程(二)


在Redis編程中, 實體和集合類型則更加有趣和實用

namespace Zeus.Cache.Redis.Demo
{
    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; }
    }
}
namespace Zeus.Cache.Redis.Demo
{
    public class Phone
    {
        public int Id { get; set; }
        public string Model { get; set; }
        public string Manufacturer { get; set; }
        public Person Owner { get; set; }
    }
}
public void EntityDemo()
        {
            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(2000);
                    // creating a new Phone entry
                    phoneFive = new Phone
                    {
                        Id = 5,
                        Manufacturer = "Nokia",
                        Model = "Lumia 200",
                        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);
                }

                string message = "Phone model is " + phoneFive.Manufacturer;
                message += "\nPhone Owner Name is: " + phoneFive.Owner.Name;

                Console.WriteLine(message);
            }
        }

運行結果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM