你的位置:首页 > ASP.net教程

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

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

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

/// <summary>    /// 复杂类的对象 转化为List<HashEntry> 此方法不支持List等对象,需另外封装    /// </summary>    /// <typeparam name="T"></typeparam>    /// <param name="key"></param>    /// <param name="obj"></param>    /// <returns></returns>    public List<HashEntry> ObjectToHashEntryList<T>(string key, T obj) where T : class, new()    {      var people = new People() { Name = "ws", Age = 18 };      List<HashEntry> list = new List<HashEntry>();      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<T>存入hash中的话,field名称还挺不好为你取的。

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

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

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

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