Redis之 散列(hashes)

Redis hash 看起来就像一个 “hash” 的样子,由键值对组成:

Hash 便于表示 objects,实际上,你可以放入一个 hash 的域数量实际上没有限制(除了可用内存以外)。所以,你可以在你的应用中以不同的方式使用 hash。

值得注意的是,小的 hash 被用特殊方式编码,非常节约内存。


1、删除操作

        /// <summary>
        /// 根据hashId移除指定key的数据
        /// 原指令:HDEL key field [field ...]
        /// 时间复杂度:O(1)
        /// 参考:http://www.redis.cn/commands/hdel.html
        /// </summary>
        public bool Remove(string hashId, string key)
        {
            return Redis.RemoveEntryFromHash(hashId, key);
        }

        /// <summary>
        /// 移除未指定hashId的key数据
        /// 从 key 指定的哈希集中移除指定的域。在哈希集中不存在的域将被忽略。
        /// 如果 key 指定的哈希集不存在,它将被认为是一个空的哈希集,该命令将返回0。
        /// 原指令:HDEL key field [field ...]
        /// 时间复杂度:O(N) N是被删除的字段数量。
        /// </summary>
        public bool Remove(string key)
        {
            return Redis.Remove(key);
        }

        /// <summary>
        /// 移除未指定hashId的所有keys的数据
        /// 原指令:HDEL key field [field ...]
        /// 时间复杂度:O(N) N是被删除的字段数量。
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public void RemoveAll(string[] keys)
        {
            Redis.RemoveAll(keys);
        }
       
2、Set操作

/// <summary>
        /// 设置 key 指定的哈希集中指定字段的值。
        /// 如果 key 指定的哈希集不存在,会创建一个新的哈希集并与 key 关联。
        /// 如果字段在哈希集中存在,它将被重写,返回False。
        /// 原指令:HSET key field value
        /// 时间复杂度:O(1)
        /// 
        /// </summary>
        public bool Set<T>(string hashId, string key, T t)
        {
            string value = redisOper.SerializeToString<T>(t);
            return Redis.SetEntryInHash(hashId, key, value);
        }

        /// <summary>
        /// 设置 key 指定的哈希集中指定字段的值。
        /// 如果不存在则写入,true,如果存在则不写入返回false
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hashId"></param>
        /// <param name="key"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool SetIfNotExists<T>(string hashId, string key, T t)
        {
            string value = redisOper.SerializeToString<T>(t);
            return Redis.SetEntryInHashIfNotExists(hashId, key, value);
        }

3、Get 操作

/// <summary>
        /// 返回 key 指定的哈希集包含的字段的数量。
        /// 原指令:HLEN key
        /// 时间复杂度:O(1)
        /// 参考:http://www.redis.cn/commands/hlen.html
        /// </summary>
        /// <param name="hashId"></param>
        /// <returns></returns>
        public long GetLen(string hashId)
        {
            return Redis.GetHashCount(hashId);
        }

        /// <summary>
        /// 返回 key 指定的哈希集中该字段所关联的值
        /// 原指令:HGET key field
        /// 时间复杂度:O(1)
        /// 参考:http://www.redis.cn/commands/hget.html
        /// </summary>
        public T Get<T>(string hashId, string key)
        {
            string value = Redis.GetValueFromHash(hashId, key);
            return redisOper.DeserializeFromString<T>(value);
        }

        /// <summary>
        /// 返回 key 指定的哈希集中指定字段的值。
        /// 对于哈希集中不存在的每个字段,返回 nil 值。因为不存在的keys被认为是一个空的哈希集,
        /// 对一个不存在的 key 执行 HMGET 将返回一个只含有 nil 值的列表
        /// 原指令:HMGET key field [field ...]
        /// 时间复杂度:O(N)
        /// 参考:http://www.redis.cn/commands/hmget.html
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hashId"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public List<T> GetMValues<T>(string hashId, string[] keys)
        {
            var result = new List<T>();
            var list = Redis.GetValuesFromHash(hashId, keys);
            if (list != null && list.Count > 0)
            {
                list.ForEach(x =>
                {
                    result.Add(redisOper.DeserializeFromString<T>(x));
                });
            }
            return result;
        }

        /// <summary>
        /// 返回 hashId 指定的哈希集中所有字段的key。
        /// 原指令:HGETALL key 返回 key 指定的哈希集中所有的字段和值。返回值中,每个字段名的下一个是它的值,所以返回值的长度是哈希集大小的两倍
        /// 时间复杂度:O(N)
        /// 参考:http://www.redis.cn/commands/hgetall.html
        /// </summary>
        /// <param name="hashId"></param>
        /// <returns></returns>
        public List<string> GetAllKeys(string hashId)
        {
            return Redis.GetHashKeys(hashId);
        }

        /// <summary>
        /// 返回 hashId 指定的哈希集中所有字段的值。
        /// 原指令:HVALS key/HGETALL key 这2个命令都可以 未看源码具体实现未知
        /// 时间复杂度:O(N)
        /// 参考:http://www.redis.cn/commands/hvals.html
        /// </summary>
        public List<T> GetAllValues<T>(string hashId)
        {
            var result = new List<T>();
            var list = Redis.GetHashValues(hashId);
            if (list != null && list.Count > 0)
            {
                list.ForEach(x =>
                {
                    result.Add(redisOper.DeserializeFromString<T>(x));
                });
            }
            return result;
        }

        /// <summary>
        /// 获取所有hashid数据集的key/value数据集合
        /// </summary>
        public Dictionary<string, T> GetAllEntriesFromHash<T>(string hashId)
        {
            var result = new Dictionary<string, T>();
            var list = Redis.GetAllEntriesFromHash(hashId);
            if (list != null && list.Count > 0)
            {
                foreach (var kv in list)
                {
                    result.Add(kv.Key, redisOper.DeserializeFromString<T>(kv.Value));
                }
            }
            return result;
        }

4、其他
/// <summary>
        /// 判断某个数据是否存在
        /// 原指令:HEXISTS key field
        /// 时间复杂度:O(1)
        /// 参考:http://www.redis.cn/commands/hexists.html
        /// </summary>
        public bool Exist(string hashId, string key)
        {
            return Redis.HashContainsEntry(hashId, key);
        }


        /// <summary>
        /// i++,i--操作 自增自减操作
        /// 给hashid数据集key的value加countby,返回相加后的数据
        /// 为指定key的hash的field字段值执行float类型的increment加。如果field不存在,则在执行该操作前设置为0.如果出现下列情况之一,则返回错误: 
        /// field的值包含的类型错误(不是字符串)。当前field或者increment不能解析为一个float类型。
        /// 原指令:HINCRBYFLOAT key field increment
        /// 时间复杂度:O(1)
        /// 参考:http://www.redis.cn/commands/hincrbyfloat.html
        /// </summary>
        public long IncrementValueInHash(string hashId, string key, int countBy)
        {
            return Redis.IncrementValueInHash(hashId, key, countBy);
        }

        /// <summary>
        /// i++,i--操作 自增自减操作
        /// 给hashid数据集key的value加countby,返回相加后的数据
        /// 为指定key的hash的field字段值执行float类型的increment加。如果field不存在,则在执行该操作前设置为0.如果出现下列情况之一,则返回错误: 
        /// field的值包含的类型错误(不是字符串)。当前field或者increment不能解析为一个float类型。
        /// 原指令:HINCRBYFLOAT key field increment
        /// 时间复杂度:O(1)
        /// 参考:http://www.redis.cn/commands/hincrbyfloat.html
        /// </summary>
        public double IncrementValueInHash(string hashId, string key, double countBy)
        {
            return Redis.IncrementValueInHash(hashId, key, countBy);
        }

        /// <summary>
        /// i++,i--操作 自增自减操作
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long IncrementValueBy(string key)
        {
            return Redis.IncrementValue(key);
        }

        /// <summary>
        /// i++,i--操作 自增自减操作
        /// 增加 key 指定的哈希集中指定字段的数值。如果 key 不存在,会创建一个新的哈希集并与 key 关联。
        /// 如果字段不存在,则字段的值在该操作执行前被设置为 0HINCRBY 支持的值的范围限定在 64位 有符号整数
        /// 原指令:HINCRBY key field increment
        /// 时间复杂度:O(1)
        /// 参考:http://www.redis.cn/commands/hincrby.html
        /// </summary>
        public long IncrementValueBy(string key, int countBy)
        {
            return Redis.IncrementValueBy(key, countBy);
        }

        public long IncrementValueBy(string key, long countBy)
        {
            return Redis.IncrementValueBy(key, countBy);
        }

        public double IncrementValueBy(string key, double countBy)
        {
            return Redis.IncrementValueBy(key, countBy);
        }

        /// <summary>
        /// 设置缓存过期
        /// 未指定hashId 可以设置过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public bool SetExpire(string key, DateTime datetime)
        {
            return Redis.ExpireEntryAt(key, datetime);
        }

        /// <summary>
        /// 设置缓存过期
        /// 未指定hashId 可以设置过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expireIn"></param>
        /// <returns></returns>
        public bool ExpireEntryIn(string key, TimeSpan expireIn)
        {
            return Redis.ExpireEntryIn(key, expireIn);
        }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值