/// <summary> /// 鍙朙ist鐨勬墍鏈夊€? /// </summary> /// <param name="listid"></param> /// <param name="poolType"></param> /// <returns></returns> public static List <string> LGetAll(string listid, RedisPoolType poolType) { List <string> list = new List <string>(); PooledRedisClientManager pool = GetRedisPool(poolType); IRedisClient redis = pool.GetReadOnlyClient(); try { list = redis.GetAllItemsFromList(listid); if (redis != null) { redis.Dispose(); } } catch { throw; } finally { if (redis != null) { redis.Dispose(); } } return(list); }
public RedisCache() { if (_redis != null) { _redis.Dispose(); _redis.Shutdown(); } }
/// <summary> /// 关闭 /// </summary> public void Close() { if (_Redis != null) { _Redis.Dispose(); _Redis = null; } }
private void CloseForm() { try { isSave = false; if (marketTcpClient != null) { marketTcpClient.shutdown(); } if (marketErrorLoger != null) { marketErrorLoger.close(); } if (saveToFile != null) { saveToFile.Abort(); } if (reconnectThread != null) { reconnectThread.Abort(); } if (Redis != null) { Redis.Dispose(); } } catch { } }
/// <summary> /// 获取Hash集合数量 /// </summary> /// <param name="key">Hashid</param> public long Hash_GetCount(string key) { IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return(0); } redis = manager.GetClient(); return(redis.GetHashCount(key)); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.Set_Remove"); } finally { if (redis != null) { redis.Dispose(); } } return(0); }
/// <summary> /// 从hash表获取数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <returns></returns> public T Hash_Get <T>(string key, string dataKey) { IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return(default(T)); } redis = manager.GetClient(); string value = redis.GetValueFromHash(key, dataKey); return(JsonUtils.Deserialize <T>(value)); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.Set_Remove"); } finally { if (redis != null) { redis.Dispose(); } } return(default(T)); }
/// <summary> /// 存储数据到hash表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <returns></returns> public bool Hash_Set <T>(string key, string dataKey, T t, int?timeout = null) { IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return(false); } redis = manager.GetClient(); string value = JsonUtils.SerializeObject(t); var result = redis.SetEntryInHash(key, dataKey, value); if (timeout.HasValue && timeout.Value > 0) { redis.ExpireEntryIn(key, new TimeSpan(0, timeout.Value, 0)); } return(result); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.Set_Remove"); } finally { if (redis != null) { redis.Dispose(); } } return(false); }
public List <T> List_GetList <T>(string key) { IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return(new List <T>()); } redis = manager.GetClient(); var c = redis.As <T>(); return(c.Lists[key].GetRange(0, c.Lists[key].Count)); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.List_GetList"); } finally { if (redis != null) { redis.Dispose(); } } return(new List <T>()); }
/// <summary> /// 设置缓存过期 /// </summary> /// <param name="key"></param> /// <param name="timeout"></param> public bool Item_SetExpire(string key, int timeout) { IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return(true); } redis = manager.GetClient(); return(redis.ExpireEntryIn(key, new TimeSpan(0, timeout, 0))); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.Item_SetExpire"); } finally { if (redis != null) { redis.Dispose(); } } return(false); }
/// <summary> /// 添加到哈希表中 /// </summary> /// <param name="hashId">哈希表ID</param> /// <param name="key">哈希表Key</param> /// <param name="value">值</param> /// <param name="updateIfExists">如果key存在是否更新</param> public static bool AddHash(string hashId, string key, string value, bool updateIfExists = true) { bool result = false; try { using (IRedisClient redis = RedisManager.GetClient()) { if (updateIfExists) { LogHelper.Debug("添加到哈希表中AddHash-SetEntryInHash"); result = redis.SetEntryInHash(hashId, key, value); } else { LogHelper.Debug("添加到哈希表中AddHash-SetEntryInHashIfNotExists"); result = redis.SetEntryInHashIfNotExists(hashId, key, value); } LogHelper.Debug("添加到哈希表中AddHash,hashId:" + hashId + ",key=" + key + ",value=" + value + ",updateIfExists=" + updateIfExists + ",result=" + result); redis.Dispose(); } } catch (Exception ex) { LogHelper.Error("添加到哈希表中AddHash异常", ex); } return(result); }
/// <summary> /// Get the next message off the Redis list, within a timeout /// </summary> /// <param name="timeoutInMilliseconds">The period to await a message</param> /// <returns>The message read from the list</returns> public Message Receive(int timeoutInMilliseconds) { _logger.Value.DebugFormat("RedisMessageConsumer: Preparing to retrieve next message from queue {0} with routing key {1} via exchange {2} on connection {3}", _queueName, _topic); var message = new Message(); IRedisClient client = null; try { client = GetClient(); EnsureConnection(client); (string msgId, string rawMsg)redisMessage = ReadMessage(client, timeoutInMilliseconds); message = new RedisMessageCreator().CreateMessage(redisMessage.rawMsg); _inflight.Add(message.Id, redisMessage.msgId); } catch (TimeoutException te) { _logger.Value.ErrorFormat("Could not connect to Redis client within {0} milliseconds", timeoutInMilliseconds.ToString()); throw new ChannelFailureException( string.Format("Could not connect to Redis client within {0} milliseconds", timeoutInMilliseconds.ToString()), te ); } catch (RedisException re) { _logger.Value.ErrorFormat($"Could not connect to Redis: {re.Message}"); throw new ChannelFailureException(string.Format("Could not connect to Redis client - see inner exception for details"), re); } finally { client?.Dispose(); } return(message); }
static void Main(string[] args) { try { //获取Redis操作接口 IRedisClient Redis = RedisManager.GetClient(); //放入内存 User u = new User(); u.Name = "小张"; u.Pwd = "123"; Redis.Set <User>("my_name", u); Redis.Set <int>("my_age", 12); //保存到硬盘 Redis.Save(); //释放内存 Redis.Dispose(); //取出数据 Console.WriteLine("取出刚才存进去的数据 \r\n 我的Name:{0}; 我的Age:{1}.", Redis.Get <string>("my_name"), Redis.Get <int>("my_age")); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); Console.ReadKey(); } }
/// <summary> /// Get the next message off the Redis list, within a timeout /// </summary> /// <param name="timeoutInMilliseconds">The period to await a message</param> /// <returns>The message read from the list</returns> public Message Receive(int timeoutInMilliseconds) { _logger.Value.DebugFormat("RedisMessageConsumer: Preparing to retrieve next message from queue {0} with routing key {1} via exchange {2} on connection {3}", _queueName, _topic); var message = new Message(); IRedisClient client = null; try { client = GetClient(); EnsureConnection(client); var redisMessage = ReadMessage(client, timeoutInMilliseconds); message = new BrighterMessageFactory().Create(redisMessage); } catch (TimeoutException te) { _logger.Value.ErrorFormat("Could not connect to redis client within {0} milliseconds", timeoutInMilliseconds.ToString()); throw new ChannelFailureException( string.Format("Could not connect to redis client within {0} milliseconds", timeoutInMilliseconds.ToString()), te ); } finally { client?.Dispose(); } return(message); }
/// <summary> /// 鍙栦竴涓湁搴忛泦鍚堥儴鍒嗘垚鍛樺強鍒嗘暟 /// </summary> /// <param name="key"></param> /// <param name="fromRank"></param> /// <param name="toRank"></param> /// <param name="isAsc"></param> /// <param name="poolType"></param> /// <returns></returns> public static IDictionary <string, double> ZGetRangeWithScores(string key, int fromRank, int toRank, bool isAsc, RedisPoolType poolType) { IDictionary <string, double> values = new Dictionary <string, double>(); PooledRedisClientManager pool = GetRedisPool(poolType); IRedisClient redis = pool.GetReadOnlyClient(); try { if (isAsc) { values = redis.GetRangeWithScoresFromSortedSet(key, fromRank, toRank); } else { values = redis.GetRangeWithScoresFromSortedSetDesc(key, fromRank, toRank); } } catch { throw; } finally { if (redis != null) { redis.Dispose(); } } return(values); }
/// <summary> /// 鍙栦竴涓湁搴忛泦鍚堥儴鍒嗘垚鍛樺強鍒嗘暟 /// </summary> /// <param name="key"></param> /// <param name="fromScore"></param> /// <param name="toScore"></param> /// <param name="skip"></param> /// <param name="take"></param> /// <param name="isAsc">鏄惁鍗囧簭</param> /// <param name="poolType"></param> /// <returns></returns> public static IDictionary <string, double> ZGetRangeByScoreWithScores(string key, long fromScore, long toScore, int?skip, int?take, bool isAsc, RedisPoolType poolType) { IDictionary <string, double> values = new Dictionary <string, double>(); PooledRedisClientManager pool = GetRedisPool(poolType); IRedisClient redis = pool.GetReadOnlyClient(); try { if (isAsc) { values = redis.GetRangeWithScoresFromSortedSetByLowestScore(key, fromScore, toScore, skip, take); } else { values = redis.GetRangeWithScoresFromSortedSetByHighestScore(key, fromScore, toScore, skip, take); } } catch { throw; } finally { if (redis != null) { redis.Dispose(); } } return(values); }
/// <summary> /// /// </summary> /// <param name="manager"></param> /// <param name="pattern"></param> public void RemoveLike(PooledRedisClientManager manager, string pattern) { IRedisClient redis = null; try { redis = manager.GetClient(); var keys = redis.SearchKeys(pattern); if (keys != null && keys.Any()) { redis.RemoveAll(keys); } } catch (Exception ex) { Logger.Error(ex, "RedisBase.RemovePattern"); } finally { if (redis != null) { redis.Dispose(); } } }
/// <summary> /// 设置单体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="t"></param> /// <param name="timeout">分钟</param> /// <returns></returns> public bool Item_Set <T>(string key, T t, int timeout) { if (t == null) { return(false); } IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return(false); } redis = manager.GetClient(); redis.SetEntry(key, JsonUtils.SerializeObject(t), new TimeSpan(0, timeout, 0)); return(true); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.Item_Set"); } finally { if (redis != null) { redis.Dispose(); } } return(false); }
protected virtual void Dispose(bool disposing) { if (disposing) { _publisher.Dispose(); } }
public void List_RemoveAll <T>(string key) { IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return; } redis = manager.GetClient(); var redisTypedClient = redis.As <T>(); redisTypedClient.Lists[key].RemoveAll(); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.List_RemoveAll"); } finally { if (redis != null) { redis.Dispose(); } } }
public void Dispose() { if (redisClient != null) { redisClient.Dispose(); } }
public bool Set_Remove <T>(string key, T t) { IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return(true); } redis = manager.GetClient(); var redisTypedClient = redis.As <T>(); return(redisTypedClient.Sets[key].Remove(t)); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.Set_Remove"); } finally { if (redis != null) { redis.Dispose(); } } return(false); }
///// <summary> ///// ///// </summary> //public MemcachedCacheProvider() //{ // _objTotalOperations = new PerformanceCounter(StrCacheCatName, StrTotalOpName); // _objOperationsPerSecond = new PerformanceCounter(StrCacheCatName, StrOpPerSecName); // _objAddOperations = new PerformanceCounter(StrCacheCatName, StrAddOpName); // _objGetOperations = new PerformanceCounter(StrCacheCatName, StrGetOpName); // _objAddPerSecondOperations = new PerformanceCounter(StrCacheCatName, StrAddOpName); // _objGetPerSecondOperations = new PerformanceCounter(StrCacheCatName, StrGetOpPerSecName); //} /// <inheritdoc /> public override void Dispose() { if (_client != null) { _client.Dispose(); } }
/// <summary> /// 移除hash中的某值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <returns></returns> public bool Hash_Remove(string key, string dataKey) { IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return(false); } redis = manager.GetClient(); return(redis.RemoveEntryFromHash(key, dataKey)); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.Set_Remove"); } finally { if (redis != null) { redis.Dispose(); } } return(false); }
public void Dispose() { using (IRedisClient redis = PooleClient.GetClient()) { redis.Dispose(); } }
/// <summary> /// 设置缓存过期 /// </summary> /// <param name="key"></param> /// <param name="datetime"></param> public void Hash_SetExpire(string key, DateTime datetime) { IRedisClient redis = null; try { var manager = GetClientManager(key); if (manager == null) { return; } redis = manager.GetClient(); redis.ExpireEntryAt(key, datetime); } catch (Exception ex) { RemoveClientFromPool(key); Logger.Error(ex, "RedisBase.Set_Remove"); } finally { if (redis != null) { redis.Dispose(); } } }
public void Dispose() { Console.WriteLine($"Disposing"); _subscription.Dispose(); _client.Dispose(); }
public void Dispose() { if (_client != null) { _client.Dispose(); } }
public virtual void Dispose() { if (redis != null) { redis.Dispose(); } }
public virtual void Dispose() { db?.Dispose(); redis?.Dispose(); messageProducer?.Dispose(); using (authRepository as IDisposable) {} }
/// <summary> /// 鍙栨湁搴忛泦鍚堢殑骞堕泦 /// </summary> /// <param name="desKey"></param> /// <param name="zSetKeys"></param> /// <param name="aggregate"></param> /// <param name="poolType"></param> /// <returns></returns> public static long ZUnionStore(string desKey, string[] zSetKeys, RedisAggregate aggregate, RedisPoolType poolType) { long rtn = 0; PooledRedisClientManager pool = GetRedisPool(poolType); IRedisClient redis = pool.GetReadOnlyClient(); try { if (zSetKeys == null || zSetKeys.Length == 0) { redis.Remove(desKey); redis.AddItemToSortedSet(desKey, "-1", -1); rtn = 0; } else { string[] args = new string[] { "AGGREGATE", aggregate.ToString() }; rtn = redis.StoreUnionFromSortedSets(desKey, zSetKeys, args); } } catch { throw; } finally { if (redis != null) { redis.Dispose(); } } return(rtn); }
/// <summary> /// 释放一个链接 /// </summary> /// <param name="redisClient"></param> public void Dispose(IRedisClient redisClient) { redisClient.Dispose(); }