Пример #1
0
        /// <summary>
        /// get value by key
        /// </summary>
        /// <param name="KeyName"></param>
        /// <param name="Field"></param>
        /// <returns></returns>
        public T GetHashValue <T>(String KeyName, String Field)
        {
            if (conn == null)
            {
                throw new Exception("Connection has not initialize.");
            }

            return(conn.HGet <T>(KeyName, Field));
        }
Пример #2
0
 /// <summary>
 /// 根据表名,键名,获取hash值
 /// </summary>
 /// <param name="key">表名</param>
 /// <param name="field">键名</param>
 /// <returns></returns>
 public static string GetHash(string key, string field)
 {
     try
     {
         return(_redisManager.HGet(key, field));
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #3
0
        /// <summary>
        /// 获取Key缓存数据(指定hashFiled)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public T GetHash <T>(string key, string hashField)
        {
            var value = _client.HGet(key, hashField);

            if (!string.IsNullOrEmpty(value))
            {
                return(JsonConvert.DeserializeObject <T>(value));
            }
            else
            {
                return(default(T));
            }
        }
 public bool TryGetValue(string key, out byte[] value)
 {
     if (redisDB.HExists(this.Id, key))
     {
         value = redisDB.HGet <byte[]>(this.Id, key);
         redisDB.ExpireAsync(this.Id, config.ExpireSeconds);
         return(true);
     }
     else
     {
         value = null;
         return(false);
     }
 }
Пример #5
0
        private static void Main(string[] args)
        {
            var csredis = new CSRedisClient("127.0.0.1:6379,password=123,defaultDatabase=1,poolsize=50,ssl=false,writeBuffer=10240");

            csredis.Set("name", "张三");//设置值。默认永不过期
            Console.WriteLine(csredis.Get("name"));

            csredis.Set("time", DateTime.Now, 1);
            Console.WriteLine(csredis.Get <DateTime>("time"));
            Thread.Sleep(1100);
            Console.WriteLine(csredis.Get <DateTime>("time"));

            // 列表
            csredis.RPush("list", "第一个元素");
            csredis.RPush("list", "第二个元素");
            csredis.LInsertBefore("list", "第二个元素", "我是新插入的第二个元素!");
            Console.WriteLine($"list的长度为{csredis.LLen("list")}");

            Console.WriteLine($"list的第二个元素为{csredis.LIndex("list", 1)}");
            // 哈希
            csredis.HSet("person", "name", "zhulei");
            csredis.HSet("person", "sex", "男");
            csredis.HSet("person", "age", "28");
            csredis.HSet("person", "adress", "hefei");
            Console.WriteLine($"person这个哈希中的age为{csredis.HGet<int>("person", "age")}");

            // 集合
            csredis.SAdd("students", "zhangsan", "lisi");
            csredis.SAdd("students", "wangwu");
            csredis.SAdd("students", "zhaoliu");
            Console.WriteLine($"students这个集合的大小为{csredis.SCard("students")}");
            Console.WriteLine($"students这个集合是否包含wagnwu:{csredis.SIsMember("students", "wangwu")}");

            //有序集合
            csredis.ZAdd("socre", (1, "张三"), (2, "李四"));
            csredis.ZCard("score");
            csredis.ZCount("score", 1, 3);

            Console.ReadLine();
        }
 public string GetKeyHash(string key, string field)
 {
     return(csClient.HGet(key, field));
 }
Пример #7
0
        /// <summary>
        /// 从hash表获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="dataKey"></param>
        /// <returns></returns>
        public T HashGet <T>(string key, string dataKey)
        {
            string value = csredis.HGet(key, dataKey);

            return(ConvertObj <T>(value));
        }
Пример #8
0
 /// <summary>
 /// 判断客户端是否在线
 /// </summary>
 /// <param name="clientId"></param>
 /// <returns></returns>
 public bool HasOnline(Guid clientId)
 {
     return(_redis.HGet <int>($"{_redisPrefix}Online", clientId.ToString()) > 0);
 }