Beispiel #1
0
        /// <summary>
        /// 返回有序集 key 中,指定区间内的成员。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List <Dictionary <string, string> > SortedSetRangeWithScore(string key, int start, int end)
        {
            var list = RedisManager.ReadDataBase().SortedSetRangeByRankWithScores(key, start, end);
            List <Dictionary <string, string> > list_result = new List <Dictionary <string, string> >();

            for (int i = 0; i < list.Count(); i++)
            {
                Dictionary <string, string> dict = new Dictionary <string, string>();
                dict.Add("member", list[i].Element);
                dict.Add("score", list[i].Score.ToString());
                list_result.Add(dict);
            }
            return(list_result);
        }
Beispiel #2
0
        /// <summary>
        /// 迭代集合键中的元素
        /// </summary>
        /// <param name="key">集合名称</param>
        /// <param name="pattern">获取的值</param>
        /// <param name="pagesize">最大结果数</param>
        /// <returns></returns>
        public List <Dictionary <string, string> > SortedSetScan(string key, string pattern, int pagesize, int cursor)
        {
            List <Dictionary <string, string> > list_result = new List <Dictionary <string, string> >();
            var list = RedisManager.ReadDataBase().SortedSetScan(key, pattern, pagesize, cursor).ToArray();

            for (int i = 0; i < list.Length; i++)
            {
                Dictionary <string, string> dict = new Dictionary <string, string>();
                dict.Add("member", list[i].Element);
                dict.Add("score", list[i].Score.ToString());
                list_result.Add(dict);
            }
            return(list_result);
        }
Beispiel #3
0
 public long ListInsert(string key, RedisCommandDirection direct, string pivot, string value)
 {
     if (direct == RedisCommandDirection.BEFORE)
     {
         return(RedisManager.WriteDataBase().ListInsertBefore(key, pivot, value));
     }
     else if (direct == RedisCommandDirection.AFTER)
     {
         return(RedisManager.WriteDataBase().ListInsertAfter(key, pivot, value));
     }
     else
     {
         throw new Exception("插入值的方向错误!");
     }
 }
Beispiel #4
0
        /// <summary>
        /// 获取所有指定key的value,对于不存在的string或者不存在的key,返回null
        /// </summary>
        /// <param name="keys"></param>
        public ArrayList StringGet(string[] keys)
        {
            RedisKey[] list = new RedisKey[keys.Length];
            for (int i = 0; i < keys.Length; i++)
            {
                list[i] = keys[i];
            }
            var       sets        = RedisManager.ReadDataBase().StringGet(list);
            ArrayList list_result = new ArrayList();

            foreach (var item in sets)
            {
                list_result.Add(item.ToString());
            }
            return(list_result);
        }
Beispiel #5
0
        /// <summary>
        /// 返回一个集合的全部成员,该集合是所有给定集合之间的交集。
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public ArrayList SetInter(string[] keys)
        {
            List <RedisKey> list_key = new List <RedisKey>();

            for (int i = 0; i < keys.Length; i++)
            {
                list_key.Add(keys[i]);
            }
            var       list        = RedisManager.ReadDataBase().SetCombine(SetOperation.Intersect, list_key.ToArray());
            ArrayList list_result = new ArrayList();

            for (int i = 0; i < list.Length; i++)
            {
                list_result.Add(list[i]);
            }
            return(list_result);
        }
Beispiel #6
0
 /// <summary>
 /// 同时将多个 field-value (域-值)对设置到哈希表 key 中。
 /// </summary>
 /// <param name="key">哈希表的名称</param>
 /// <param name="fields">域的名称</param>
 /// <param name="values">域的值</param>
 public void HashSet(string key, string[] fields, string[] values)
 {
     //判断两个数组的长度是否相等
     if (fields.Length != values.Length)
     {
         throw new Exception("域的名称和域的值的数组长度不等!");
     }
     if (fields.Length <= 0)
     {
         throw new Exception("域的名称数组长度不能小于0");
     }
     HashEntry[] hashlist = new HashEntry[fields.Length];
     for (int i = 0; i < fields.Length; i++)
     {
         HashEntry entry = new HashEntry(fields[i], values[i]);
         hashlist[i] = entry;
     }
     RedisManager.WriteDataBase().HashSet(key, hashlist);
 }
Beispiel #7
0
        /// <summary>
        /// 查看给定键距离过期键有多少秒
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public double KeyTTL(string key)
        {
            var flag = RedisManager.ReadDataBase().KeyExists(key);

            if (!flag)
            {
                return(-2);
            }
            var time = RedisManager.ReadDataBase().KeyTimeToLive(key);

            if (time == null)
            {
                return(-1);
            }
            else
            {
                return(time.Value.TotalSeconds);
            }
        }
Beispiel #8
0
 /// <summary>
 /// 移除集合 key 中的一个元素,不存在的 member 元素会被忽略。
 /// </summary>
 /// <param name="key">集合名称</param>
 /// <param name="value">删除的值</param>
 /// <returns></returns>
 public bool SetRemove(string key, string value)
 {
     return(RedisManager.WriteDataBase().SetRemove(key, value));
 }
Beispiel #9
0
 /// <summary>
 /// 随机获取集合中的一个元素
 /// </summary>
 /// <param name="key">集合名称</param>
 /// <returns></returns>
 public string SetRandMember(string key)
 {
     return(RedisManager.ReadDataBase().SetRandomMember(key));
 }
Beispiel #10
0
 /// <summary>
 /// 当队列存在时,从队列右边入队一个元素
 /// </summary>
 /// <param name="key">列表名称</param>
 /// <param name="value">插入的值</param>
 /// <returns></returns>
 public long ListRightPushX(string key, string value)
 {
     return(RedisManager.WriteDataBase().ListRightPush(key, value, When.Exists));
 }
Beispiel #11
0
 /// <summary>
 /// 查看哈希表 key 中,给定域 field 是否存在。
 /// </summary>
 /// <param name="key">哈希表的名称</param>
 /// <param name="field">域的名称</param>
 /// <returns></returns>
 public bool HashExists(string key, string field)
 {
     return(RedisManager.ReadDataBase().HashExists(key, field));
 }
Beispiel #12
0
        /// <summary>
        /// 但它将结果保存到 destination 集合,该集合是所有给定集合的并集。
        /// </summary>
        /// <param name="destination">合并生成的集合</param>
        /// <param name="keys">所有合并的集合名称</param>
        /// <returns></returns>
        public long SetUnionStore(string destination, string[] keys)
        {
            var redisKeys = Array.ConvertAll <string, RedisKey>(keys, m => (RedisKey)m);

            return(RedisManager.ReadDataBase().SetCombineAndStore(SetOperation.Union, destination, redisKeys));
        }
Beispiel #13
0
 /// <summary>
 /// 删除哈希表 key 中的一个指定域,不存在的域将被忽略。
 /// </summary>
 /// <param name="key">哈希表的名称</param>
 /// <param name="field">域的名称</param>
 /// <returns></returns>
 public bool HashDelete(string key, string field)
 {
     return(RedisManager.WriteDataBase().HashDelete(key, field));
 }
Beispiel #14
0
 /// <summary>
 /// 移除键的过期时间
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public bool KeyPersist(string key)
 {
     return(RedisManager.WriteDataBase().KeyPersist(key));
 }
Beispiel #15
0
        /// <summary>
        /// 移除集合 key 中的多个 member 元素,不存在的 member 元素会被忽略。
        /// </summary>
        /// <param name="key">集合名称</param>
        /// <param name="value">删除的值</param>
        /// <returns></returns>
        public long SetRemove(string key, string[] value)
        {
            var list = Array.ConvertAll <string, RedisValue>(value, s => (RedisValue)s);

            return(RedisManager.WriteDataBase().SetRemove(key, values: list.ToArray()));
        }
Beispiel #16
0
 /// <summary>
 /// 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。
 /// </summary>
 /// <param name="key">键名</param>
 /// <param name="value">过期时间(秒)</param>
 /// <returns></returns>
 public bool KeyExpire(string key, double value)
 {
     return(RedisManager.WriteDataBase().KeyExpire(key, TimeSpan.FromSeconds(value)));
 }
Beispiel #17
0
 /// <summary>
 /// 将集合之间给定的差集保存在dest集合中,如果集合已存在则将其覆盖
 /// </summary>
 /// <param name="first"></param>
 /// <param name="second"></param>
 /// <param name="dest">生成的集合</param>
 /// <returns></returns>
 public long SetDiffStore(string first, string second, string dest)
 {
     return(RedisManager.WriteDataBase().SetCombineAndStore(SetOperation.Difference, dest, first, second));
 }
Beispiel #18
0
 /// <summary>
 /// 查找所有符合pattern的key
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public ArrayList Keys(string key)
 {
     return(RedisManager.Keys(key));
 }
Beispiel #19
0
        /// <summary>
        /// 返回一个集合的全部成员,该集合是所有给定集合的并集。
        /// </summary>
        /// <param name="first">第一个集合</param>
        /// <param name="second">第二个集合</param>
        /// <returns></returns>
        public string[] SetUnion(string first, string second)
        {
            var list = RedisManager.ReadDataBase().SetCombine(SetOperation.Union, first, second);

            return(Array.ConvertAll <RedisValue, string>(list, m => m.ToString()));
        }
Beispiel #20
0
 /// <summary>
 /// 将集合之间给定的差集保存在dest集合中,如果集合已存在则将其覆盖
 /// </summary>
 /// <param name="first"></param>
 /// <param name="second"></param>
 /// <param name="dest">生成的集合</param>
 /// <returns></returns>
 public long SetInterStore(string first, string second, string dest)
 {
     return(RedisManager.WriteDataBase().SetCombineAndStore(SetOperation.Intersect, dest, first, second));
 }
Beispiel #21
0
 /// <summary>
 /// 但它将结果保存到 destination 集合,该集合是所有给定集合的并集。
 /// </summary>
 /// <param name="destination">合并生成的集合</param>
 /// <param name="first">第一个集合</param>
 /// <param name="second">第二个集合</param>
 /// <returns></returns>
 public long SetUnionStore(string destination, string first, string second)
 {
     return(RedisManager.ReadDataBase().SetCombineAndStore(SetOperation.Union, destination, first, second));
 }
Beispiel #22
0
 /// <summary>
 /// 将 member 元素从 source 集合移动到 destination 集合。
 /// </summary>
 /// <param name="source">源集合</param>
 /// <param name="destination">目标集合</param>
 /// <param name="value">移动的值</param>
 /// <returns></returns>
 public bool SetMove(string source, string destination, string value)
 {
     return(RedisManager.WriteDataBase().SetMove(source, destination, value));
 }
Beispiel #23
0
        /// <summary>
        /// 迭代集合键中的元素
        /// </summary>
        /// <param name="key">集合名称</param>
        /// <param name="pattern">获取的值</param>
        /// <param name="pagesize">最大结果数</param>
        /// <returns></returns>
        public string[] SetScan(string key, string pattern, int pagesize, int cursor)
        {
            var list = RedisManager.ReadDataBase().SetScan(key, pattern, pagesize, cursor).ToArray();

            return(Array.ConvertAll <RedisValue, string>(list, m => m.ToString()));
        }
Beispiel #24
0
 /// <summary>
 /// 返回集合的基数
 /// </summary>
 /// <param name="key">集合名称</param>
 /// <returns></returns>
 public long SetLength(string key)
 {
     return(RedisManager.ReadDataBase().SetLength(key));
 }
Beispiel #25
0
 /// <summary>
 /// 删除哈希表 key 中的一个指定域,不存在的域将被忽略。
 /// </summary>
 /// <param name="key">哈希表的名称</param>
 /// <param name="field">域的名称</param>
 /// <returns></returns>
 public long HashDelete(string key, string[] fields)
 {
     return(RedisManager.WriteDataBase().HashDelete(key, Array.ConvertAll <string, RedisValue>(fields, m => (RedisValue)m)));
 }
Beispiel #26
0
 /// <summary>
 /// 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。
 /// </summary>
 /// <param name="key">键名</param>
 /// <param name="value">时间戳</param>
 /// <returns></returns>
 public bool KeyExpireAt(string key, long value)
 {
     return(RedisManager.WriteDataBase().KeyExpire(key, new DateTime(value, DateTimeKind.Local)));
 }
Beispiel #27
0
 /// <summary>
 /// 返回哈希表 key 中给定域 field 的值。
 /// </summary>
 /// <param name="key">哈希表的名称</param>
 /// <param name="field">域的名称</param>
 /// <returns></returns>
 public string HashGet(string key, string field)
 {
     return(RedisManager.ReadDataBase().HashGet(key, field));
 }
Beispiel #28
0
 /// <summary>
 /// 判断成员是否是集合中的成员
 /// </summary>
 /// <param name="key">集合名称</param>
 /// <param name="member">成员的值</param>
 /// <returns></returns>
 public bool SetContains(string key, string member)
 {
     return(RedisManager.ReadDataBase().SetContains(key, member));
 }
Beispiel #29
0
        /// <summary>
        /// 返回哈希表 key 中,所有的域和值。
        /// </summary>
        /// <param name="key">哈希表的名称</param>
        /// <returns></returns>
        public Dictionary <string, string> HashGetAll(string key)
        {
            var entry = RedisManager.ReadDataBase().HashGetAll(key);

            return(entry.ToDictionary(m => m.Name.ToString(), n => n.Value.ToString()));
        }
Beispiel #30
0
 /// <summary>
 /// 移除并返回集合中的一个随机元素。
 /// </summary>
 /// <param name="key">集合名称</param>
 /// <returns></returns>
 public string SetPop(string key)
 {
     return(RedisManager.WriteDataBase().SetPop(key));
 }