zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    maven
    面试宝典之Java程序运行原理
    并发队列总结
    HashMap探究
    Linux安装python应用之前需要安装的库
    Linux中为Python应用安装uwsgi
    常用SQL语句
    Windows CE无法连接Win 10
    无法加载 DLL xpstar.dll 或它引用的一个 DLL。原因: 126(找不到指定的模块。)。
    SQL Server 数据恢复
  • 原文地址:https://www.cnblogs.com/l1pe1/p/7346149.html
Copyright © 2011-2022 走看看