#region Redis Hash散列数据类型操作 /// <summary> /// Redis散列数据类型 批量新增 /// </summary> void HashSet(string key, List<HashEntry> hashEntrys, CommandFlags flags = CommandFlags.None); /// <summary> /// Redis散列数据类型 新增一个 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="val"></param> void HashSet<T>(string key, string field, T val, When when = When.Always, CommandFlags flags = CommandFlags.None); /// <summary> /// Redis散列数据类型 获取指定key的指定field /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <returns></returns> T HashGet<T>(string key, string field); /// <summary> /// Redis散列数据类型 获取所有field所有值,以 HashEntry[]形式返回 /// </summary> /// <param name="key"></param> /// <param name="flags"></param> /// <returns></returns> HashEntry[] HashGetAll(string key, CommandFlags flags = CommandFlags.None); /// <summary> /// Redis散列数据类型 获取key中所有field的值。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="flags"></param> /// <returns></returns> List<T> HashGetAllValues<T>(string key, CommandFlags flags = CommandFlags.None); /// <summary> /// Redis散列数据类型 获取所有Key名称 /// </summary> /// <param name="key"></param> /// <param name="flags"></param> /// <returns></returns> string[] HashGetAllKeys(string key, CommandFlags flags = CommandFlags.None); /// <summary> /// Redis散列数据类型 单个删除field /// </summary> /// <param name="key"></param> /// <param name="hashField"></param> /// <param name="flags"></param> /// <returns></returns> bool HashDelete(string key, string hashField, CommandFlags flags = CommandFlags.None); /// <summary> /// Redis散列数据类型 批量删除field /// </summary> /// <param name="key"></param> /// <param name="hashFields"></param> /// <param name="flags"></param> /// <returns></returns> long HashDelete(string key, string[] hashFields, CommandFlags flags = CommandFlags.None); /// <summary> /// Redis散列数据类型 判断指定键中是否存在此field /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="flags"></param> /// <returns></returns> bool HashExists(string key, string field, CommandFlags flags = CommandFlags.None); /// <summary> /// Redis散列数据类型 获取指定key中field数量 /// </summary> /// <param name="key"></param> /// <param name="flags"></param> /// <returns></returns> long HashLength(string key, CommandFlags flags = CommandFlags.None); /// <summary> /// Redis散列数据类型 为key中指定field增加incrVal值 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="incrVal"></param> /// <param name="flags"></param> /// <returns></returns> double HashIncrement(string key, string field, double incrVal, CommandFlags flags = CommandFlags.None); #endregion
#region Redis Hash散列数据类型操作 /// <summary> /// Redis散列数据类型 批量新增 /// </summary> public void HashSet(string key, List<HashEntry> hashEntrys, CommandFlags flags = CommandFlags.None) { _db.HashSet(key, hashEntrys.ToArray(), flags); } /// <summary> /// Redis散列数据类型 新增一个 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="val"></param> public void HashSet<T>(string key, string field, T val, When when = When.Always, CommandFlags flags = CommandFlags.None) { _db.HashSet(key, field, SerializeContent(val), when, flags); } /// <summary> /// Redis散列数据类型 获取指定key的指定field /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <returns></returns> public T HashGet<T>(string key, string field) { return DeserializeContent<T>(_db.HashGet(key, field)); } /// <summary> /// Redis散列数据类型 获取所有field所有值,以 HashEntry[]形式返回 /// </summary> /// <param name="key"></param> /// <param name="flags"></param> /// <returns></returns> public HashEntry[] HashGetAll(string key, CommandFlags flags = CommandFlags.None) { return _db.HashGetAll(key,flags); } /// <summary> /// Redis散列数据类型 获取key中所有field的值。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="flags"></param> /// <returns></returns> public List<T> HashGetAllValues<T>(string key, CommandFlags flags = CommandFlags.None) { List<T> list = new List<T>(); var hashVals = _db.HashValues(key, flags).ToArray(); foreach (var item in hashVals) { list.Add(DeserializeContent<T>(item)); } return list; } /// <summary> /// Redis散列数据类型 获取所有Key名称 /// </summary> /// <param name="key"></param> /// <param name="flags"></param> /// <returns></returns> public string[] HashGetAllKeys(string key, CommandFlags flags = CommandFlags.None) { return _db.HashKeys(key, flags).ToStringArray(); } /// <summary> /// Redis散列数据类型 单个删除field /// </summary> /// <param name="key"></param> /// <param name="hashField"></param> /// <param name="flags"></param> /// <returns></returns> public bool HashDelete(string key,string hashField, CommandFlags flags = CommandFlags.None) { return _db.HashDelete(key, hashField,flags); } /// <summary> /// Redis散列数据类型 批量删除field /// </summary> /// <param name="key"></param> /// <param name="hashFields"></param> /// <param name="flags"></param> /// <returns></returns> public long HashDelete(string key, string[] hashFields, CommandFlags flags = CommandFlags.None) { List<RedisValue> list = new List<RedisValue>(); for(int i = 0; i < hashFields.Length; i++) { list.Add(hashFields[i]); } return _db.HashDelete(key, list.ToArray(), flags); } /// <summary> /// Redis散列数据类型 判断指定键中是否存在此field /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="flags"></param> /// <returns></returns> public bool HashExists(string key,string field, CommandFlags flags = CommandFlags.None) { return _db.HashExists(key, field, flags); } /// <summary> /// Redis散列数据类型 获取指定key中field数量 /// </summary> /// <param name="key"></param> /// <param name="flags"></param> /// <returns></returns> public long HashLength(string key, CommandFlags flags = CommandFlags.None) { return _db.HashLength(key, flags); } /// <summary> /// Redis散列数据类型 为key中指定field增加incrVal值 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="incrVal"></param> /// <param name="flags"></param> /// <returns></returns> public double HashIncrement(string key,string field,double incrVal, CommandFlags flags = CommandFlags.None) { return _db.HashIncrement(key, field, incrVal, flags); } #endregion
/// <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; }
/// <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()); }
源文:https://www.cnblogs.com/tdws/p/5823935.html