• 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

  • 相关阅读:
    携程呼叫中心异地双活——座席服务的高可用
    从5台服务器到两地三中心:魅族系统运维架构演进之路(含PPT)
    从“两地三中心”到“分布式多活”
    “两地三中心”容灾备份设计与实现_数据备份_数据恢复-阿里云
    膏方_百度百科
    矿泉水瓶加湿器 办公室空气加湿器迷你便携水瓶座 小型USB迷你家用静音 白色【图片 价格 品牌 报价】-京东
    【资料下载】ANTLR的最全的官方文档:The Definitive ANTLR Reference:v2,v3,v4版本都有下载 | 在路上
    “百万年薪40多万个税”高不高?你怎么看?_财经_腾讯网
    浙江四大滑雪场攻略及2015滑雪门票价格
    druid parser
  • 原文地址:https://www.cnblogs.com/yyjspace/p/11613396.html
Copyright © 2020-2023  润新知