ServiceStack.redis用法

using System;
using System.Collections.Generic;
using ServiceStack.Redis;

namespace SysBuild
{
    class Program
    {
        //linux服务器地址
        static private string host = "182.180.50.168";
        //static private string host = "127.0.0.1";
        static private int port = 6379;
        static RedisClient redisClient = new RedisClient(host, port);//redis服务IP和端口
        static void Main(string[] args)
        {
            //创建一个键a,指定值
            redisClient.Set<string>("a", "1");
            //获取键a对应的值
            var a1 = redisClient.Get<string>("a");
            //删除键a
            redisClient.Remove("a");
            //创建一个键a,指定值,并指定10s有效
            redisClient.Set<string>("b", "1", new TimeSpan(0, 0, 60));

            //队列操作之入队
            redisClient.EnqueueItemOnList("list", "haha");
            redisClient.EnqueueItemOnList("list", "haha111111111");
            redisClient.EnqueueItemOnList("list", "haha22");
            redisClient.EnqueueItemOnList("list", "hahset33333333333333333");
            //读取队列深度
            var a = redisClient.GetListCount("list");
            //队列操作之出队
            for (int i = 0; i < a; i++)
            {
                Console.WriteLine(redisClient.DequeueItemFromList("list"));
            }


            redisClient.SetEntryInHash("Hash", "Name", "wujf");
            redisClient.SetEntryInHash("Hash", "Age", "99");
            redisClient.SetEntryInHash("Hash", "Sex", "男");
            redisClient.SetEntryInHash("Hash", "Address", "上海市XX号XX室");
            //集合类指定超时的方法
            redisClient.ExpireEntryIn("Hash", new TimeSpan(0, 0, 100));
            var dic = redisClient.GetAllEntriesFromHash("Hash");
            Console.WriteLine("Key-----Value");
            foreach (var keyVal in dic)
            {
                Console.WriteLine(string.Format("{0}-----{1}", keyVal.Key, keyVal.Value));
            }
            List<string> haskKey = redisClient.GetHashKeys("Hash");
            List<string> haskVal = redisClient.GetHashValues("Hash");
            foreach (string key in haskKey)
            {
                Console.WriteLine("HashID--Key:{0}", key);
            }
            foreach (string val in haskVal)
            {
                Console.WriteLine("HashID--val:{0}", val);
            }

            //栈使用,先进后出
            redisClient.PushItemToList("stack", "1");
            redisClient.PushItemToList("stack", "2");
            redisClient.PushItemToList("stack", "3");
            redisClient.PushItemToList("stack", "4");
            int count = redisClient.GetListCount("stack");
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(redisClient.PopItemFromList("stack"));
            }

            //对Set类型进行操作  
            redisClient.AddItemToSet("set", "ddd");
            redisClient.AddItemToSet("set", "ccc");
            redisClient.AddItemToSet("set", "tttt");
            redisClient.AddItemToSet("set", "sssh");
            redisClient.AddItemToSet("set", "hhhh");
            HashSet<string> hashset = redisClient.GetAllItemsFromSet("set");
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }

            //求并集              
            redisClient.AddItemToSet("set1", "aa");
            redisClient.AddItemToSet("set1", "bb");
            redisClient.AddItemToSet("set2", "bb");
            redisClient.AddItemToSet("set2", "cc");
            hashset = redisClient.GetUnionFromSets(new string[] { "set1", "set2" });
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }
            //求交集  
            hashset = redisClient.GetIntersectFromSets(new string[] { "set1", "set2" });
            //求差集.  
            hashset = redisClient.GetDifferencesFromSet("set1", new string[] { "set2" });

            //Sorted Set类型排序
            redisClient.AddItemToSortedSet("sortList", "1");
            redisClient.AddItemToSortedSet("sortList", "9");
            redisClient.AddItemToSortedSet("sortList", "3");
            redisClient.AddItemToSortedSet("sortList", "8");
            List<string> sortList = redisClient.GetAllItemsFromSortedSet("sortList");
            foreach (string str in sortList)
            {
                Console.WriteLine(str);
            }


            Console.ReadKey();
        }
    }
}
posted @ 2017-08-11 15:22  桃花雪  阅读(1211)  评论(0编辑  收藏  举报