StackExchange.Redis帮助类解决方案RedisRepository封装(散列Hash类型数据操作)

本文版权归博客园和作者本人共同所有,转载和爬虫请注明本系列分享地址:http://www.cnblogs.com/tdws/p/5815735.html

上一篇文章的不合理之处,已经有所修改。

今天分享的是Hash散列数据类型操作,不过我也觉得有了前两篇的基础搭建后,你就能自己按照StackExchange中所封装的方法,进行调用并再次封装。在实际项目中,有些方法可能并不需要,比如获取所有Field以及其Value。当真正封装成dll的时候,有些方法,我们可以在接口中禁用,在非用不可时,我们可以将其开放。

先上接口代码吧:如果有朋友希望从头看起,请进入第一章分享链接 http://www.cnblogs.com/tdws/p/5815735.html

 1 #region Redis Hash散列数据类型操作
 2 
 3         /// 
 4         /// Redis散列数据类型  批量新增
 5         /// 
 6         void HashSet(string key, List hashEntrys, CommandFlags flags = CommandFlags.None);
 7 
 8         /// 
 9         /// Redis散列数据类型  新增一个
10         /// 
11         /// 
12         /// 
13         /// 
14         void HashSet(string key, string field, T val, When when = When.Always, CommandFlags flags = CommandFlags.None);
15 
16         /// 
17         ///  Redis散列数据类型 获取指定key的指定field
18         /// 
19         /// 
20         /// 
21         /// 
22         T HashGet(string key, string field);
23 
24         /// 
25         ///  Redis散列数据类型 获取所有field所有值,以 HashEntry[]形式返回
26         /// 
27         /// 
28         /// 
29         /// 
30         HashEntry[] HashGetAll(string key, CommandFlags flags = CommandFlags.None);
31 
32         /// 
33         /// Redis散列数据类型 获取key中所有field的值。
34         /// 
35         /// 
36         /// 
37         /// 
38         /// 
39         List HashGetAllValues(string key, CommandFlags flags = CommandFlags.None);
40 
41         /// 
42         /// Redis散列数据类型 获取所有Key名称
43         /// 
44         /// 
45         /// 
46         /// 
47         string[] HashGetAllKeys(string key, CommandFlags flags = CommandFlags.None);
48 
49         /// 
50         ///  Redis散列数据类型  单个删除field
51         /// 
52         /// 
53         /// 
54         /// 
55         /// 
56         bool HashDelete(string key, string hashField, CommandFlags flags = CommandFlags.None);
57 
58         /// 
59         ///  Redis散列数据类型  批量删除field
60         /// 
61         /// 
62         /// 
63         /// 
64         /// 
65         long HashDelete(string key, string[] hashFields, CommandFlags flags = CommandFlags.None);
66 
67         /// 
68         ///  Redis散列数据类型 判断指定键中是否存在此field
69         /// 
70         /// 
71         /// 
72         /// 
73         /// 
74         bool HashExists(string key, string field, CommandFlags flags = CommandFlags.None);
75 
76         /// 
77         /// Redis散列数据类型  获取指定key中field数量
78         /// 
79         /// 
80         /// 
81         /// 
82         long HashLength(string key, CommandFlags flags = CommandFlags.None);
83 
84         /// 
85         /// Redis散列数据类型  为key中指定field增加incrVal值
86         /// 
87         /// 
88         /// 
89         /// 
90         /// 
91         /// 
92         double HashIncrement(string key, string field, double incrVal, CommandFlags flags = CommandFlags.None);
93 
94 
95         #endregion

下面是实现的代码,其实就是简单调用下dll为我们提供好的方法。

  1  #region Redis Hash散列数据类型操作
  2         /// 
  3         /// Redis散列数据类型  批量新增
  4         /// 
  5         public void HashSet(string key, List hashEntrys, CommandFlags flags = CommandFlags.None)
  6         {
  7             _db.HashSet(key, hashEntrys.ToArray(), flags);
  8         }
  9         /// 
 10         /// Redis散列数据类型  新增一个
 11         /// 
 12         /// 
 13         /// 
 14         /// 
 15         public void HashSet(string key, string field, T val, When when = When.Always, CommandFlags flags = CommandFlags.None)
 16         {
 17             _db.HashSet(key, field, SerializeContent(val), when, flags);
 18         }
 19         /// 
 20         ///  Redis散列数据类型 获取指定key的指定field
 21         /// 
 22         /// 
 23         /// 
 24         /// 
 25         public T HashGet(string key, string field)
 26         {
 27             return DeserializeContent(_db.HashGet(key, field));
 28         }
 29         /// 
 30         ///  Redis散列数据类型 获取所有field所有值,以 HashEntry[]形式返回
 31         /// 
 32         /// 
 33         /// 
 34         /// 
 35         public HashEntry[] HashGetAll(string key, CommandFlags flags = CommandFlags.None)
 36         {
 37             return _db.HashGetAll(key,flags);
 38         }
 39         /// 
 40         /// Redis散列数据类型 获取key中所有field的值。
 41         /// 
 42         /// 
 43         /// 
 44         /// 
 45         /// 
 46         public List HashGetAllValues(string key, CommandFlags flags = CommandFlags.None)
 47         {
 48             List list = new List();
 49             var hashVals = _db.HashValues(key, flags).ToArray();
 50             foreach (var item in hashVals)
 51             {
 52                 list.Add(DeserializeContent(item));
 53             }
 54             return list;
 55         }
 56 
 57         /// 
 58         /// Redis散列数据类型 获取所有Key名称
 59         /// 
 60         /// 
 61         /// 
 62         /// 
 63         public string[] HashGetAllKeys(string key, CommandFlags flags = CommandFlags.None)
 64         {
 65             return _db.HashKeys(key, flags).ToStringArray();
 66         }
 67         /// 
 68         ///  Redis散列数据类型  单个删除field
 69         /// 
 70         /// 
 71         /// 
 72         /// 
 73         /// 
 74         public bool HashDelete(string key,string hashField, CommandFlags flags = CommandFlags.None)
 75         {
 76             return _db.HashDelete(key, hashField,flags);
 77         }
 78         /// 
 79         ///  Redis散列数据类型  批量删除field
 80         /// 
 81         /// 
 82         /// 
 83         /// 
 84         /// 
 85         public long HashDelete(string key, string[] hashFields, CommandFlags flags = CommandFlags.None)
 86         {
 87             List list = new List();
 88             for(int i = 0; i < hashFields.Length; i++)
 89             {
 90                 list.Add(hashFields[i]);
 91             }
 92             return _db.HashDelete(key, list.ToArray(), flags);
 93         }
 94         /// 
 95         ///  Redis散列数据类型 判断指定键中是否存在此field
 96         /// 
 97         /// 
 98         /// 
 99         /// 
100         /// 
101         public bool HashExists(string key,string field, CommandFlags flags = CommandFlags.None)
102         {
103             return _db.HashExists(key, field, flags);
104         }
105         /// 
106         /// Redis散列数据类型  获取指定key中field数量
107         /// 
108         /// 
109         /// 
110         /// 
111         public long HashLength(string key, CommandFlags flags = CommandFlags.None)
112         {
113             return _db.HashLength(key, flags);
114         }
115         /// 
116         /// Redis散列数据类型  为key中指定field增加incrVal值
117         /// 
118         /// 
119         /// 
120         /// 
121         /// 
122         /// 
123         public double HashIncrement(string key,string field,double incrVal, CommandFlags flags = CommandFlags.None)
124         {
125             return _db.HashIncrement(key, field, incrVal, flags);
126         }
127  #endregion

另外,当你需要批量对Hash中增加数据的时候,如果你增加一个Person对象。他拥有姓名,年龄,性别等属性,你想将他存在一个三列当中,并将属性名称作为field名称时,你可以使用如下方法。

/// 
        /// 复杂类的对象 转化为List  此方法不支持List等对象,需另外封装
        /// 
        /// 
        /// 
        /// 
        /// 
        public List ObjectToHashEntryList(string key, T obj) where T : class, new()
        {
            var people = new People() { Name = "ws", Age = 18 };
            List list = new List();
            foreach (PropertyInfo p in obj.GetType().GetProperties())
            {
                var name = p.Name.ToString();
                var val = p.GetValue(obj);
                list.Add(new HashEntry(name, SerializeContent(val)));
            }
            HashSet(key, list);
            return list;
        }

但是如果你想将一个List存入hash中的话,field名称还挺不好为你取的。

有了下面的地方法,你可以通过你传入的一个方法,这个方法用于根据item定义field名称。

         /// 
        /// Hash中存储一个集合
///
/// /// /// /// public static void HashSet(string key, List list,Funcstring> ModelIdentity) { List list = new List(); foreach (var item in list) { string json = SerializeContent(item); list.Add(new HashEntry(ModelIdentity(item), json)); } db.HashSet(key, list.ToArray()); }

 针对如上两种方法,获取hashGet方法也可以跟着改变,需要反序列化成泛型T类型的对象哟。

一直在用别人的RedisHelper, 自己封装的可能会有些问题,分享出来也真诚欢迎大家来给出指导,我将会进一步改进。

 

你可能感兴趣的:(StackExchange.Redis帮助类解决方案RedisRepository封装(散列Hash类型数据操作))