• 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

  • 相关阅读:
    Lucene 3.5 提供深度分页支持 searchAfter方法 方法的应用
    如何解决ORA12547错误
    sde 安装
    软件安装之arcsde10.0集群
    Linux 64bit下Oracle11g安装手册
    Lucene 3.5 提供深度分页支持 searchAfter方法 方法的应用
    Creating a Feature Set via C#
    sde 安装
    9.15
    9.18
  • 原文地址:https://www.cnblogs.com/yyjspace/p/11613396.html
Copyright © 2020-2023  润新知