Redis Hash哈希

#region hash
/// <summary>
/// 写入hash
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="list"></param>
/// <param name="getModelId"></param>
/// <param name="expireTime"></param>
public static void HashSet<T>(string key, List<T> list, Func<T, string> getModelId, DateTime? expireTime = null) {
try {
List<HashEntry> listHashEntry = new List<HashEntry>();
foreach (var item in list) {
string json = JsonConvert.SerializeObject(item);
listHashEntry.Add(new HashEntry(getModelId(item), json));
}
_db.HashSet(key, listHashEntry.ToArray());
_db.KeyExpire(key, expireTime.HasValue ? expireTime : DateTime.Now.AddMinutes(timeOut));

}
catch (Exception) {
throw;
}
}

/// <summary>
/// 在hash中设置值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="hashField"></param>
/// <param name="value"></param>
/// <param name="expireTime"></param>
/// <returns></returns>
public static bool HashSet<T>(string key, string hashField, T value, DateTime? expireTime = null) {
var result = _db.HashSet(key, hashField, JsonConvert.SerializeObject(value));
_db.KeyExpire(key, expireTime.HasValue ? expireTime : DateTime.Now.AddMinutes(timeOut));
return result;
}
/// <summary>
/// 删除hash中一个值
/// </summary>
/// <param name="key"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public static bool HashDelete(string key, string hashField) {
return _db.HashDelete(key, hashField);
}
/// <summary>
/// 判断某个值是否存在hash中
/// </summary>
/// <param name="key"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public static bool HashExists(string key, string hashField) {
return _db.HashExists(key, hashField);
}
/// <summary>
/// 获取Hash中的单个key的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="hashFildValue"></param>
/// <returns></returns>
public static T HashGet<T>(string key, string hashFildValue) {
try {
var value = _db.HashGet(key, hashFildValue);
if (value.HasValue)
return JsonConvert.DeserializeObject<T>(value);
return default(T);
}
catch (Exception) {
throw;
}
}
/// <summary>
/// 获取hash中的多个key的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="hashFields"></param>
/// <returns></returns>
public static List<T> GetHashKey<T>(string key, List<RedisValue> hashFields) {
try {
var result = new List<T>();
RedisValue[] value = _db.HashGet(key, hashFields.ToArray());
foreach (var item in value) {
if (!item.IsNullOrEmpty) {
result.Add(JsonConvert.DeserializeObject<T>(item));
}
}
return result;
}
catch (Exception) {
throw;
}
}

/// <summary>
/// 获取hashkey所有Redis key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static List<T> GetHashKey<T>(string key) {
try {
List<T> result = new List<T>();
RedisValue[] arr = _db.HashKeys(key);
foreach (var item in arr) {
if (!item.IsNullOrEmpty) {
result.Add(JsonConvert.DeserializeObject<T>(item));
}
}
return result;
}
catch (Exception) {
throw;
}
}


/// <summary>
/// 获取hashkey所有的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static List<T> HashGetAll<T>(string key) {
try {
List<T> result = new List<T>();
HashEntry[] arr = _db.HashGetAll(key);
foreach (var item in arr) {
if (!item.Value.IsNullOrEmpty) {
result.Add(JsonConvert.DeserializeObject<T>(item.Value));
}
}
return result;
}
catch (Exception) {
throw;
}
}

#endregion

posted @ 2019-09-30 16:52  艺洁  阅读(245)  评论(0编辑  收藏  举报