Redis 读取及存储

 

使用NuGet安装ServiceStack.Redis,这是微软提供已经封装好的对redis操作类。包含4个dll

 

连接redis服务器,读取以及存储

 

  public static ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient("127.0.0.1", 6379);
        static void Main(string[] args)
        {
            AddString(client);
            Console.ReadLine();
        }
         /// <summary>
        /// 添加字符串
        /// </summary>
        /// <param name="client"></param>
        private static void AddString(RedisClient client)
        {
            client.Add("address", "qihaohao");
            Console.WriteLine(client.Get<string>("address"));
        }
        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="client"></param>
        private static void AddPerson(RedisClient client)
        {
            var person = new Person() { Name = "qhh", Age = 26 };
            client.Add("person", person);
            var cachePerson = client.Get<Person>("person");
            Console.WriteLine("name=" + cachePerson.Name + "----age=" + cachePerson.Age);
        }

        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

 

        /// <summary>
        /// 哈希结构数据
        /// </summary>
        /// <param name="client"></param>
        private static void Addhash(RedisClient client)
        {
            client.SetEntryInHash("HashId", "Name", "QHH");
            client.SetEntryInHash("HashId", "Age", "26");
            var hash = client.GetHashValues("HashId");
            foreach (var item in hash)
            {
                Console.WriteLine(item);
            }
        }

 

        /// <summary>
        /// 队列
        /// </summary>
        /// <param name="client"></param>
        private static void AddQueue(RedisClient client)
        {
            client.EnqueueItemOnList("QueueListId", "1.qhh");
            client.EnqueueItemOnList("QueueListId", "2.qihaohao");
            client.EnqueueItemOnList("QueueListId", "3.qihh");
            var queue = client.GetListCount("QueueListId");
            for (int i = 0; i < queue; i++)
            {
                Console.WriteLine(client.DequeueItemFromList("QueueListId"));
            }
        }

 

        /// <summary>
        /// 栈
        /// </summary>
        /// <param name="client"></param>
        private static void AddStack(RedisClient client)
        {
            client.PushItemToList("StackListId", "1.qhh");  //入栈
            client.PushItemToList("StackListId", "2.qihaohao");
            client.PushItemToList("StackListId", "3.qihh");
            var stackCount = client.GetListCount("StackListId");
            for (int i = 0; i < stackCount; i++)
            {
                Console.WriteLine(client.PopItemFromList("StackListId"));
            }
        }
        /// <summary>
        /// 集合
        /// </summary>
        /// <param name="client"></param>
        private static void AddSet(RedisClient client)
        {
            client.AddItemToSet("Set1001", "qhh");
            client.AddItemToSet("Set1001", "qihaohao");
            client.AddItemToSet("Set1001", "qihh");
            var set = client.GetAllItemsFromSet("Set1001");
            foreach (var item in set)
            {
                Console.WriteLine(item);
            }
        }

 

/// <summary>
       /// 集合
       /// </summary>
       /// <param name="client"></param>
        private static void AddSortSet(RedisClient client)
        {
            client.AddItemToSortedSet("SetSorted1001", "1.qhh");
            client.AddItemToSortedSet("SetSorted1001", "2.qihaohao");
            client.AddItemToSortedSet("SetSorted1001", "3.qihh");
            var sortset = client.GetAllItemsFromSortedSet("SetSorted1001");
            foreach (var item in sortset)
            {
                Console.WriteLine(item);
            }

            var sortset1 = client.GetRangeFromSortedSet("SetSorted1001", 0, 0);

            foreach (var item in sortset1)
            {
                Console.WriteLine(item);
            }

            var list = client.GetRangeFromSortedSetDesc("SetSorted1001", 0, 0);

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }
        /// <summary>
        /// 返回两个集合的并集
        /// </summary>
        /// <param name="client"></param>
        private static void Union(RedisClient client)
        {
            client.AddItemToSet("Set8001", "A");
            client.AddItemToSet("Set8001", "B");
            client.AddItemToSet("Set8001", "C");
            client.AddItemToSet("Set8001", "D");


            client.AddItemToSet("Set8002", "E");
            client.AddItemToSet("Set8002", "F");
            client.AddItemToSet("Set8002", "G");
            client.AddItemToSet("Set8002", "D");
            var setunion = client.GetUnionFromSets("Set8001", "Set8002");
            foreach (var item in setunion)
            {
                Console.WriteLine(item);
            }
        }


        /// <summary>
        /// 返回两个集合的 交集
        /// </summary>
        /// <param name="client"></param>
        private static void JJ(RedisClient client)
        {
            var hashG = client.GetIntersectFromSets(new string[] { "Set8001", "Set8002" });
            foreach (var item in hashG)
            {
                Console.WriteLine(item);
            }
        }

        /// <summary>
        /// 返回两个集合的差集
        /// </summary>
        /// <param name="client"></param>
        private static void CJ(RedisClient client)
        {
            var hashD = client.GetDifferencesFromSet("Set8001", new string[] { "Set8002" });  //[返回存在于第一个集合,但是不存在于其他集合的数据。差集]
            foreach (var item in hashD)
            {
                Console.WriteLine(item);
            }
        }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值