Exemplo n.º 1
0
        /// <summary>
        /// 获取hashkey所有的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List <T> HashGetAll <T>(string key)
        {
            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    IDatabase   db     = redis.GetDatabase();
                    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 ex)
            {
                NLogger.Error(ex.ToString(), "Cache");

                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取Hash中的单个key的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">Redis Key</param>
        /// <param name="hasFildValue">RedisValue</param>
        /// <returns></returns>
        public static T GetHashKey <T>(string key, string hasFildValue)
        {
            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    IDatabase db = redis.GetDatabase();

                    if (!string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(hasFildValue))
                    {
                        RedisValue value = db.HashGet(key, hasFildValue);
                        if (!value.IsNullOrEmpty)
                        {
                            return(JsonConvert.DeserializeObject <T>(value));
                        }
                    }
                    return(default(T));
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString(), "Cache");

                return(default(T));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取hash中的多个key的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">Redis Key</param>
        /// <param name="listhashFields">RedisValue value</param>
        /// <returns></returns>
        public static List <T> GetHashKey <T>(string key, List <RedisValue> listhashFields)
        {
            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    IDatabase db = redis.GetDatabase();


                    List <T> result = new List <T>();
                    if (!string.IsNullOrWhiteSpace(key) && listhashFields.Count > 0)
                    {
                        RedisValue[] value = db.HashGet(key, listhashFields.ToArray());
                        foreach (var item in value)
                        {
                            if (!item.IsNullOrEmpty)
                            {
                                result.Add(JsonConvert.DeserializeObject <T>(item));
                            }
                        }
                    }
                    return(result);
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString(), "Cache");

                return(null);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// SortedSet  排序 获取
        /// </summary>
        /// <typeparam name="T">实体对象</typeparam>
        /// <param name="key">主键</param>
        /// <param name="start">开始  默认 0</param>
        /// <param name="end">结束 默认-1 </param>
        /// <param name="order">排序 默认降序</param>
        /// <returns></returns>
        public static List <T> SortedSetRangeByRank <T>(string key, int start = 0, int end = -1, Order order = Order.Descending)
        {
            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    IDatabase db   = redis.GetDatabase();
                    var       list = db.SortedSetRangeByRank(key, start, end, order);
                    if (list != null && list.Length > 0)
                    {
                        List <T> result = new List <T>();
                        foreach (var item in list)
                        {
                            var data = JsonHelper.DeserializeObject <T>(item.ToString());
                            result.Add(data);
                        }
                        return(result);
                    }
                    return(null);
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString(), "Cache");

                return(null);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// 删除多个key
 /// </summary>
 /// <param name="keys">rediskey</param>
 /// <returns>成功删除的个数</returns>
 public static long keyDelete(RedisKey[] keys)
 {
     try
     {
         using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
         {
             IDatabase db = redis.GetDatabase();
             return(db.KeyDelete(keys));
         }
     }
     catch (Exception ex)
     {
         NLogger.Error(ex.ToString(), "Cache");
         return(0);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// 读取缓存 object
 /// </summary>
 /// <param name="key">主键</param>
 /// <returns></returns>
 public static object Get(string key)
 {
     try
     {
         using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
         {
             IDatabase db = redis.GetDatabase();
             return(db.StringGet(key));
         }
     }
     catch (Exception ex)
     {
         NLogger.Error(ex.ToString(), "Cache");
         return(null);
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// 设置缓存  (永久)
 /// </summary>
 /// <param name="key">主键</param>
 /// <param name="value">值</param>
 public static bool Set(string key, string value)
 {
     try
     {
         using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
         {
             IDatabase db = redis.GetDatabase();
             return(db.StringSet(key, value));
         }
     }
     catch (Exception ex)
     {
         NLogger.Error(ex.ToString(), "Cache");
         return(false);
     }
 }
Exemplo n.º 8
0
        /// <summary>
        ///  Hash  删除
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="hashField">字段主键</param>
        /// <returns></returns>
        public static bool HashDelete(string key, string hashField)
        {
            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    IDatabase db = redis.GetDatabase();
                    return(db.HashDelete(key, hashField));
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString(), "Cache");

                return(false);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 过期 (时间)
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="expiry">时间</param>
        /// <returns></returns>
        public static bool KeyExpire(string key, DateTime expiry)
        {
            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    IDatabase db = redis.GetDatabase();
                    return(db.KeyExpire(key, expiry));
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString(), "Cache");

                return(false);
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// SortedSet 添加
 /// </summary>
 /// <typeparam name="T">实体对象</typeparam>
 /// <param name="key">主键</param>
 /// <param name="value">实体值</param>
 /// <param name="score">排序值</param>
 /// <returns></returns>
 public static bool SortedSetAdd <T>(string key, T value, double score)
 {
     try
     {
         using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
         {
             IDatabase db         = redis.GetDatabase();
             var       jsonString = JsonHelper.SerializeObject(value);
             return(db.SortedSetAdd(key, jsonString, score));
         }
     }
     catch (Exception ex)
     {
         NLogger.Error(ex.ToString(), "Cache");
         return(false);
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// 计数器 -
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="i">值</param>
        public static long Decrement(string key, int i)
        {
            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    IDatabase db = redis.GetDatabase();
                    return(db.StringDecrement(key, i));
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString(), "Cache");

                return(0);
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// 设置缓存 T
 /// </summary>
 /// <param name="key">主键</param>
 /// <param name="value">值</param>
 /// <param name="t">过期时间(分钟)</param>
 public static bool Set <T>(string key, T value, int i)
 {
     try
     {
         using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
         {
             TimeSpan  t          = new TimeSpan(0, i, 0);
             IDatabase db         = redis.GetDatabase();
             var       jsonString = JsonHelper.SerializeObject(value);
             return(db.StringSet(key, jsonString, t));
         }
     }
     catch (Exception ex)
     {
         NLogger.Error(ex.ToString(), "Cache");
         return(false);
     }
 }
Exemplo n.º 13
0
        /// <summary>
        ///  Hash  添加
        /// </summary>
        /// <typeparam name="T">实体对象</typeparam>
        /// <param name="key">主键</param>
        /// <param name="hashField">字段主键</param>
        /// <param name="value">实体值</param>
        /// <returns></returns>
        public static bool HashSet <T>(string key, string hashField, T value)
        {
            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    IDatabase db         = redis.GetDatabase();
                    var       jsonString = JsonHelper.SerializeObject(value);
                    return(db.HashSet(key, hashField, jsonString));
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString(), "Cache");

                return(false);
            }
        }
Exemplo n.º 14
0
 /// <summary>
 /// 读取缓存 List<T>
 /// </summary>
 /// <param name="key">主键</param>
 /// <returns></returns>
 public static List <T> GetList <T>(string key)
 {
     try
     {
         using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
         {
             IDatabase db     = redis.GetDatabase();
             string    result = db.StringGet(key);
             var       empty  = JsonHelper.DeserializeObject <List <T> >(result);
             return(empty);
         }
     }
     catch (Exception ex)
     {
         NLogger.Error(ex.ToString(), "Cache");
         return(null);
     }
 }
Exemplo n.º 15
0
        /// <summary>
        /// 追加值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static long StringAppend(string key, string value)
        {
            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    IDatabase db = redis.GetDatabase();
                    ////追加值,返回追加后长度
                    long appendlong = db.StringAppend(key, value);
                    return(appendlong);
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString(), "Cache");

                return(0);
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// 主键key查找
        /// </summary>
        /// <param name="key">关键字</param>
        public static List <string> GetByKey(string key)
        {
            List <string> result = new List <string>();

            try
            {
                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(GetConn.GetInstance().Conn()))
                {
                    string host   = ZConfig.GetConfigString("redispath");
                    var    server = redis.GetServer(host);

                    foreach (var item in server.Keys(pattern: key))
                    {
                        result.Add(item);
                    }
                    return(result);
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString(), "Cache");
                return(new List <string>());
            }
        }