• redis 专题 (二)哈希操作


    直接上源代码,代码中有很详细的注释:

    
    

    /// <summary>
    /// Hash跟string的区别,在于redis内部的算法。相对来说,hash的总体性能高于string
    /// </summary>


    using
    (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //删除当前数据库中的所有Key 默认删除的是db0 client.FlushDb(); //删除所有数据库中的key client.FlushAll(); ///大key string hashid = "keyiwang"; #region 向hashid集合中添加key/value //client.SetEntryInHash(hashid, "id", "001"); //Console.WriteLine(client.GetValuesFromHash(hashid, "id").FirstOrDefault()); //client.SetEntryInHash(hashid, "name", "周芷若"); //Console.WriteLine(client.GetValuesFromHash(hashid, "name").FirstOrDefault()); //client.SetEntryInHash(hashid, "socre", "100"); //Console.WriteLine(client.GetValuesFromHash(hashid, "socre").FirstOrDefault()); #endregion #region 批量新增key的值 //Dictionary<string, string> pairs = new Dictionary<string, string>(); //pairs.Add("id", "001"); //pairs.Add("name", "周芷若"); //client.SetRangeInHash(hashid, pairs); ////获取当前key的值 //Console.WriteLine(client.GetValueFromHash(hashid, "id")); //Console.WriteLine(client.GetValueFromHash(hashid, "name")); ////一次性的获取所有想要获取的小key(属性的)值 如果key不存在,则返回空,不抛出异常 //var list = client.GetValuesFromHash(hashid, "id", "abc", "name"); //Console.WriteLine("*********"); //foreach (var item in list) //{ // Console.WriteLine(item); //} #endregion #region 如果hashid集合中存在key/value则不添加返回false,如果不存在在添加key/value,返回true //Console.WriteLine(client.SetEntryInHashIfNotExists(hashid, "name", "你好美")); //Console.WriteLine(client.SetEntryInHashIfNotExists(hashid, "name", "你好美 哈哈哈")); //Console.WriteLine(client.GetValuesFromHash(hashid, "name").FirstOrDefault()); #endregion #region 存储对象T t到hash集合中 ////urn: 类名: id的值,必须要有id,不区分大小写 //client.StoreAsHash<UserInfo>(new UserInfo() { Id = 1, Name = "倚天屠龙记", number = 0 }); ////如果id存在的话,则覆盖之前相同的id //client.StoreAsHash<UserInfo>(new UserInfo() { Id = 2, Name = "天龙八部" }); ////获取对象T中ID为id的数据。 必须要有属性id,不区分大小写 //Console.WriteLine(client.GetFromHash<UserInfo>(1).Name); //var olduserinfo = client.GetFromHash<UserInfo>(1); //olduserinfo.number = 4; //client.StoreAsHash<UserInfo>(olduserinfo); //Console.WriteLine("最后的结果" + client.GetFromHash<UserInfo>(1).number); //client.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "001", Name = "倚天屠龙记" }); //Console.WriteLine(client.GetFromHash<UserInfoTwo>("001").Name); //client.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "002", Name = "天龙八部" }); //Console.WriteLine(client.GetFromHash<UserInfoTwo>("002").Name); //UserInfo lisi = new UserInfo() { Id = 1, Name = "李四", number = 0 }; //client.StoreAsHash<UserInfo>(lisi); //Console.WriteLine(client.GetFromHash<UserInfo>(1).number); ////做个自增 //var oldzhang = client.GetFromHash<UserInfo>(1); //oldzhang.number++; //client.StoreAsHash<UserInfo>(oldzhang); #endregion #region 获取所有hashid数据集的key/value数据集合 //Dictionary<string, string> pairs = new Dictionary<string, string>(); //pairs.Add("id", "001"); //pairs.Add("name", "周芷若"); //client.SetRangeInHash(hashid, pairs); //var dics = client.GetAllEntriesFromHash(hashid); //foreach (var item in dics) //{ // Console.WriteLine(item.Key + ":" + item.Value); //} #endregion //#region 获取hashid数据集中的数据总数 //Dictionary<string, string> pairs = new Dictionary<string, string>(); //pairs.Add("id", "001"); //pairs.Add("name", "周芷若"); //client.SetRangeInHash(hashid, pairs); //Console.WriteLine(client.GetHashCount(hashid)); //#endregion #region 获取hashid数据集中所有key的集合 //Dictionary<string, string> pairs = new Dictionary<string, string>(); //pairs.Add("id", "001"); //pairs.Add("name", "周芷若"); //client.SetRangeInHash(hashid, pairs); //var keys = client.GetHashKeys(hashid); //foreach (var item in keys) //{ // Console.WriteLine(item); //} #endregion #region 获取hashid数据集中的所有value集合 //Dictionary<string, string> pairs = new Dictionary<string, string>(); //pairs.Add("id", "001"); //pairs.Add("name", "周芷若"); //client.SetRangeInHash(hashid, pairs); //var values = client.GetHashValues(hashid); //foreach (var item in values) //{ // Console.WriteLine(item); //} #endregion #region 删除hashid数据集中的key数据 //Dictionary<string, string> pairs = new Dictionary<string, string>(); //pairs.Add("id", "001"); //pairs.Add("name", "周芷若"); //client.SetRangeInHash(hashid, pairs); //client.RemoveEntryFromHash(hashid, "id"); //var values = client.GetHashValues(hashid); //foreach (var item in values) //{ // Console.WriteLine(item); //} #endregion #region 判断hashid数据集中是否存在key的数据 //Dictionary<string, string> pairs = new Dictionary<string, string>(); //pairs.Add("id", "001"); //pairs.Add("name", "周芷若"); //client.SetRangeInHash(hashid, pairs); //Console.WriteLine(client.HashContainsEntry(hashid, "id")); //T F //Console.WriteLine(client.HashContainsEntry(hashid, "number"));// T F #endregion #region 给hashid数据集key的value加countby,返回相加后的数据 //Dictionary<string, string> pairs = new Dictionary<string, string>(); //pairs.Add("id", "001"); //pairs.Add("name", "周芷若"); //pairs.Add("number", "2"); //client.SetRangeInHash(hashid, pairs); //Console.WriteLine(client.IncrementValueInHash(hashid, "number", 2)); //注意,存的值必须是数字类型,否则抛出异常 #endregion #region 自定义 //HashTool.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "10001", Name = "周芷若" }); //var user = HashTool.GetFromHash<UserInfoTwo>("10001"); //Console.WriteLine("华丽丽的结束"); #endregion }

    .Net操作Redis数据类型哈希Hash

    public class DoRedisHash :DoRedisBase
        {
            #region 添加
            /// <summary>
            /// 向hashid集合中添加key/value
            /// </summary>       
            public bool SetEntryInHash(string hashid, string key, string value)
            {
                return RedisBase.Core.SetEntryInHash(hashid,key,value);
            }
            /// <summary>
            /// 如果hashid集合中存在key/value则不添加返回false,如果不存在在添加key/value,返回true
            /// </summary>
            public bool SetEntryInHashIfNotExists(string hashid, string key, string value)
            {
                return RedisBase.Core.SetEntryInHashIfNotExists(hashid, key, value);
            }
            /// <summary>
            /// 存储对象T t到hash集合中
            /// </summary>
            public void StoreAsHash<T>(T t)
            {
                RedisBase.Core.StoreAsHash<T>(t);
            }
            #endregion
            #region 获取
            /// <summary>
            /// 获取对象T中ID为id的数据。
            /// </summary>
            public T GetFromHash<T>(object id)
            {
                return RedisBase.Core.GetFromHash<T>(id);
            }
            /// <summary>
            /// 获取所有hashid数据集的key/value数据集合
            /// </summary>
            public Dictionary<string, string> GetAllEntriesFromHash(string hashid)
            {
                return RedisBase.Core.GetAllEntriesFromHash(hashid);
            }
            /// <summary>
            /// 获取hashid数据集中的数据总数
            /// </summary>
            public long GetHashCount(string hashid)
            {
                return RedisBase.Core.GetHashCount(hashid);
            }
            /// <summary>
            /// 获取hashid数据集中所有key的集合
            /// </summary>
            public List<string> GetHashKeys(string hashid)
            {
                return RedisBase.Core.GetHashKeys(hashid);
            }
            /// <summary>
            /// 获取hashid数据集中的所有value集合
            /// </summary>
            public List<string> GetHashValues(string hashid)
            {
                return RedisBase.Core.GetHashValues(hashid);
            }
            /// <summary>
            /// 获取hashid数据集中,key的value数据
            /// </summary>
            public string GetValueFromHash(string hashid, string key)
            {
                return RedisBase.Core.GetValueFromHash(hashid, key);
            }
            /// <summary>
            /// 获取hashid数据集中,多个keys的value集合
            /// </summary>
            public List<string> GetValuesFromHash(string hashid, string[] keys)
            {
                return RedisBase.Core.GetValuesFromHash(hashid, keys);
            }
            #endregion
            #region 删除
            #endregion
            /// <summary>
            /// 删除hashid数据集中的key数据
            /// </summary>
            public bool RemoveEntryFromHash(string hashid, string key)
            {
                return RedisBase.Core.RemoveEntryFromHash(hashid, key);
            }     
            #region 其它
            /// <summary>
            /// 判断hashid数据集中是否存在key的数据
            /// </summary>
            public bool HashContainsEntry(string hashid, string key)
            {
                return RedisBase.Core.HashContainsEntry(hashid,key);
            }
            /// <summary>
            /// 给hashid数据集key的value加countby,返回相加后的数据
            /// </summary>
            public double IncrementValueInHash(string hashid, string key, double countBy)
            {
                return RedisBase.Core.IncrementValueInHash(hashid, key, countBy);
            }
            #endregion
        }
  • 相关阅读:
    Redis Hashes 巧用sort排序
    Redis 压缩存储的配置
    计算
    关于时间大小判断的坑和网上工具类的看法
    Mysql中字段类型之时间戳大坑2
    Mysql中字段类型之时间戳大坑
    Spring和springmvc父子容器注解扫描问题详解
    JXL导出Excel工具类
    Maven学习
    MySQL之账户管理
  • 原文地址:https://www.cnblogs.com/fei686868/p/13255205.html
Copyright © 2020-2023  润新知