using Microsoft.Extensions.Options; using ServiceStack; using ServiceStack.Redis; using ServiceStack.Redis.Pipeline; using ServiceStack.Text; using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Yi.Framework.Common.IOCOptions; namespace Yi.Framework.Core { public class CacheClientDB : IDisposable { private readonly RedisConnOptions _RedisOptions; #region TestRedisCrack //static CacheClientDB() //{ // try // { // Parallel.For(0, 10000, (i) => // { // using (RedisClient client = new RedisClient("192.168.3.254")) // { // client.Set("name" + i, i); // client.Incr("name" + i); // Console.WriteLine(i); // } // }); // Console.WriteLine("ok"); // Console.WriteLine("Hello World!"); // } // catch (Exception ex) // { // Console.WriteLine(ex.Message); // } //} #endregion public CacheClientDB(IOptionsMonitor redisConnOptions) { this._RedisOptions = redisConnOptions.CurrentValue; client = new RedisClient(_RedisOptions.Host, _RedisOptions.Prot, _RedisOptions.Password, _RedisOptions.DB); } // 管道模式 三种模式 public IRedisClient GetClient() { return client; } private IRedisClient client; public void Dispose() { this.TryCatchException(delegate { this.client.Dispose(); }, string.Empty); } // 为了以后全链路做准备 private void TryCatchException(Action action, string key) { try { action(); } catch (Exception e) { Console.WriteLine(e.Message); } } private T TryCatch(Func action, string key) { Stopwatch sw = Stopwatch.StartNew(); //Exception ex = null; //bool isError = false; T result; try { result = action(); } catch (Exception exinfo) { object p=null; result =(T)p; //isError = true; Console.WriteLine(exinfo); } finally { sw.Stop(); } return result; } private void TryCatch(Action action, string key) { Stopwatch sw = Stopwatch.StartNew(); //bool isError = false; //Exception ex = null; try { action(); } catch (Exception exinfo) { //isError = true; Console.WriteLine(exinfo); } finally { sw.Stop(); } } public bool Add(string key, T value) { return this.TryCatch(() => this.client.Add(key, value), key); } /// /// 简单模式 事务模式 /// /// /// /// /// /// public bool Add(string key, T value, DateTime expiresAt) { return this.TryCatch(() => this.client.Add(key, value, expiresAt), key); } public bool Add(string key, T value, TimeSpan expiresIn) { return this.TryCatch(() => this.client.Add(key, value, expiresIn), key); } public long Decrement(string key, uint amount) { return this.TryCatch(() => this.client.Decrement(key, amount), key); } public void FlushAll() { this.TryCatch(delegate { this.client.FlushAll(); }, string.Empty); } public T Get(string key) { return this.TryCatch(() => this.client.Get(key), key); } public IDictionary GetAll(IEnumerable keys) { return this.TryCatch>(() => this.client.GetAll(keys), keys.FirstOrDefault()); } public long Increment(string key, uint amount) { return this.TryCatch(() => this.client.Increment(key, amount), key); } public bool Remove(string key) { return this.TryCatch(() => this.client.Remove(key), key); } public void RemoveAll(IEnumerable keys) { this.TryCatch(delegate { this.client.RemoveAll(keys); }, keys.FirstOrDefault()); } public bool Replace(string key, T value) { return this.TryCatch(() => this.client.Replace(key, value), key); } public bool Replace(string key, T value, DateTime expiresAt) { return this.TryCatch(() => this.client.Replace(key, value, expiresAt), key); } public bool Replace(string key, T value, TimeSpan expiresIn) { return this.TryCatch(() => this.client.Replace(key, value, expiresIn), key); } public bool Set(string key, T value) { return this.TryCatch(() => this.client.Set(key, value), key); } public bool Set(string key, T value, DateTime expiresAt) { return this.TryCatch(() => this.client.Set(key, value, expiresAt), key); } public bool Set(string key, T value, TimeSpan expiresIn) { return this.TryCatch(() => this.client.Set(key, value, expiresIn), key); } public void SetAll(IDictionary values) { this.TryCatch(delegate { this.client.SetAll(values); }, values.Keys.FirstOrDefault()); } public void Delete(T entity) where T : class, new() { this.TryCatch(delegate { this.client.Delete(entity); }, string.Empty); } public void DeleteAll() where TEntity : class, new() { this.TryCatch(delegate { this.client.DeleteAll(); }, string.Empty); } public void DeleteById(object id) where T : class, new() { this.TryCatch(delegate { this.client.DeleteById(id); }, string.Empty); } public void DeleteByIds(ICollection ids) where T : class, new() { this.TryCatch(delegate { this.client.DeleteById(ids); }, string.Empty); } public T GetById(object id) where T : class, new() { return this.TryCatch(() => this.client.GetById(id), string.Empty); } public IList GetByIds(ICollection ids) where T : class, new() { return this.TryCatch>(() => this.client.GetByIds(ids), string.Empty); } public T Store(T entity) where T : class, new() { return this.TryCatch(() => this.client.Store(entity), string.Empty); } public void StoreAll(IEnumerable entities) where TEntity : class, new() { this.TryCatch(delegate { this.client.StoreAll(entities); }, string.Empty); } public void AddItemToList(string listId, string value) { this.TryCatch(delegate { this.client.AddItemToList(listId, value); }, listId); } public void AddItemToSet(string setId, string item) { this.TryCatch(delegate { this.client.AddItemToSet(setId, item); }, setId); } public bool AddItemToSortedSet(string setId, string value) { return this.TryCatch(() => this.client.AddItemToSortedSet(setId, value), setId); } public bool AddItemToSortedSet(string setId, string value, double score) { return this.TryCatch(() => this.client.AddItemToSortedSet(setId, value, score), setId); } public void AddRangeToList(string listId, List values) { this.TryCatch(delegate { this.client.AddRangeToList(listId, values); }, listId); } public void AddRangeToSet(string setId, List items) { this.TryCatch(delegate { this.client.AddRangeToSet(setId, items); }, setId); } public bool AddRangeToSortedSet(string setId, List values, double score) { return this.TryCatch(() => this.client.AddRangeToSortedSet(setId, values, score), setId); } public bool AddRangeToSortedSet(string setId, List values, long score) { return this.TryCatch(() => this.client.AddRangeToSortedSet(setId, values, score), setId); } public long AppendToValue(string key, string value) { return this.TryCatch(() => this.client.AppendToValue(key, value), key); } public string BlockingDequeueItemFromList(string listId, TimeSpan? timeOut) { return this.TryCatch(() => this.client.BlockingDequeueItemFromList(listId, timeOut), listId); } public KeyValuePair BlockingDequeueItemFromLists(string[] listIds, TimeSpan? timeOut) { return this.TryCatch>(delegate { ItemRef item = this.client.BlockingDequeueItemFromLists(listIds, timeOut); return new KeyValuePair(item.Id, item.Item); }, listIds[0]); } public string BlockingPopAndPushItemBetweenLists(string fromListId, string toListId, TimeSpan? timeOut) { return this.TryCatch(() => this.client.BlockingPopAndPushItemBetweenLists(fromListId, toListId, timeOut), fromListId); } public string BlockingPopItemFromList(string listId, TimeSpan? timeOut) { return this.TryCatch(() => this.client.BlockingPopItemFromList(listId, timeOut), listId); } public KeyValuePair BlockingPopItemFromLists(string[] listIds, TimeSpan? timeOut) { return this.TryCatch>(delegate { ItemRef item = this.client.BlockingPopItemFromLists(listIds, timeOut); return new KeyValuePair(item.Id, item.Item); }, listIds[0]); } public string BlockingRemoveStartFromList(string listId, TimeSpan? timeOut) { return this.TryCatch(() => this.client.BlockingRemoveStartFromList(listId, timeOut), listId); } public KeyValuePair BlockingRemoveStartFromLists(string[] listIds, TimeSpan? timeOut) { return this.TryCatch>(delegate { ItemRef item = this.client.BlockingRemoveStartFromLists(listIds, timeOut); return new KeyValuePair(item.Id, item.Item); }, listIds[0]); } public bool ContainsKey(string key) { return this.TryCatch(() => this.client.ContainsKey(key), key); } public long DecrementValue(string key) { return this.TryCatch(() => this.client.DecrementValue(key), key); } public long DecrementValueBy(string key, int count) { return this.TryCatch(() => this.client.DecrementValueBy(key, count), key); } public string DequeueItemFromList(string listId) { return this.TryCatch(() => this.client.DequeueItemFromList(listId), listId); } public void EnqueueItemOnList(string listId, string value) { this.TryCatch(delegate { this.client.EnqueueItemOnList(listId, value); }, listId); } public bool ExpireEntryAt(string key, DateTime expireAt) { return this.TryCatch(() => this.client.ExpireEntryAt(key, expireAt), key); } public bool ExpireEntryIn(string key, TimeSpan expireIn) { return this.TryCatch(() => this.client.ExpireEntryIn(key, expireIn), key); } public Dictionary GetAllEntriesFromHash(string hashId) { return this.TryCatch>(() => this.client.GetAllEntriesFromHash(hashId), hashId); } public List GetAllItemsFromList(string listId) { return this.TryCatch>(() => this.client.GetAllItemsFromList(listId), listId); } public HashSet GetAllItemsFromSet(string setId) { return this.TryCatch>(() => this.client.GetAllItemsFromSet(setId), setId); } public List GetAllItemsFromSortedSet(string setId) { return this.TryCatch>(() => this.client.GetAllItemsFromSortedSet(setId), setId); } public List GetAllItemsFromSortedSetDesc(string setId) { return this.TryCatch>(() => this.client.GetAllItemsFromSortedSetDesc(setId), setId); } public List GetAllKeys() { return this.TryCatch>(() => this.client.GetAllKeys(), string.Empty); } public IDictionary GetAllWithScoresFromSortedSet(string setId) { return this.TryCatch>(() => this.client.GetAllWithScoresFromSortedSet(setId), setId); } public string GetAndSetEntry(string key, string value) { return this.TryCatch(() => this.client.GetAndSetValue(key, value), key); } public HashSet GetDifferencesFromSet(string fromSetId, params string[] withSetIds) { return this.TryCatch>(() => this.client.GetDifferencesFromSet(fromSetId, withSetIds), fromSetId); } public T GetFromHash(object id) { return this.TryCatch(() => this.client.GetFromHash(id), string.Empty); } public long GetHashCount(string hashId) { return this.TryCatch(() => this.client.GetHashCount(hashId), hashId); } public List GetHashKeys(string hashId) { return this.TryCatch>(() => this.client.GetHashKeys(hashId), hashId); } public List GetHashValues(string hashId) { return this.TryCatch>(() => this.client.GetHashValues(hashId), hashId); } public HashSet GetIntersectFromSets(params string[] setIds) { return this.TryCatch>(() => this.client.GetIntersectFromSets(setIds), setIds[0]); } public string GetItemFromList(string listId, int listIndex) { return this.TryCatch(() => this.client.GetItemFromList(listId, listIndex), listId); } public long GetItemIndexInSortedSet(string setId, string value) { return this.TryCatch(() => this.client.GetItemIndexInSortedSet(setId, value), setId); } public long GetItemIndexInSortedSetDesc(string setId, string value) { return this.TryCatch(() => this.client.GetItemIndexInSortedSetDesc(setId, value), setId); } public double GetItemScoreInSortedSet(string setId, string value) { return this.TryCatch(() => this.client.GetItemScoreInSortedSet(setId, value), setId); } public long GetListCount(string listId) { return this.TryCatch(() => this.client.GetListCount(listId), listId); } public string GetRandomItemFromSet(string setId) { return this.TryCatch(() => this.client.GetRandomItemFromSet(setId), setId); } public List GetRangeFromList(string listId, int startingFrom, int endingAt) { return this.TryCatch>(() => this.client.GetRangeFromList(listId, startingFrom, endingAt), listId); } public List GetRangeFromSortedList(string listId, int startingFrom, int endingAt) { return this.TryCatch>(() => this.client.GetRangeFromSortedList(listId, startingFrom, endingAt), listId); } public List GetRangeFromSortedSet(string setId, int fromRank, int toRank) { return this.TryCatch>(() => this.client.GetRangeFromSortedSet(setId, fromRank, toRank), setId); } public List GetRangeFromSortedSetByHighestScore(string setId, double fromScore, double toScore) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByHighestScore(setId, fromScore, toScore), setId); } public List GetRangeFromSortedSetByHighestScore(string setId, long fromScore, long toScore) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByHighestScore(setId, fromScore, toScore), setId); } public List GetRangeFromSortedSetByHighestScore(string setId, string fromStringScore, string toStringScore) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByHighestScore(setId, fromStringScore, toStringScore), setId); } public List GetRangeFromSortedSetByHighestScore(string setId, double fromScore, double toScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByHighestScore(setId, fromScore, toScore, skip, take), setId); } public List GetRangeFromSortedSetByHighestScore(string setId, long fromScore, long toScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByHighestScore(setId, fromScore, toScore, skip, take), setId); } public List GetRangeFromSortedSetByHighestScore(string setId, string fromStringScore, string toStringScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByHighestScore(setId, fromStringScore, toStringScore, skip, take), setId); } public List GetRangeFromSortedSetByLowestScore(string setId, double fromScore, double toScore) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByLowestScore(setId, fromScore, toScore), setId); } public List GetRangeFromSortedSetByLowestScore(string setId, long fromScore, long toScore) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByLowestScore(setId, fromScore, toScore), setId); } public List GetRangeFromSortedSetByLowestScore(string setId, string fromStringScore, string toStringScore) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByLowestScore(setId, fromStringScore, toStringScore), setId); } public List GetRangeFromSortedSetByLowestScore(string setId, double fromScore, double toScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByLowestScore(setId, fromScore, toScore, skip, take), setId); } public List GetRangeFromSortedSetByLowestScore(string setId, long fromScore, long toScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByLowestScore(setId, fromScore, toScore, skip, take), setId); } public List GetRangeFromSortedSetByLowestScore(string setId, string fromStringScore, string toStringScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetByLowestScore(setId, fromStringScore, toStringScore, skip, take), setId); } public List GetRangeFromSortedSetDesc(string setId, int fromRank, int toRank) { return this.TryCatch>(() => this.client.GetRangeFromSortedSetDesc(setId, fromRank, toRank), setId); } public IDictionary GetRangeWithScoresFromSortedSet(string setId, int fromRank, int toRank) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSet(setId, fromRank, toRank), setId); } public IDictionary GetRangeWithScoresFromSortedSetByHighestScore(string setId, double fromScore, double toScore) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByHighestScore(setId, fromScore, toScore), setId); } public IDictionary GetRangeWithScoresFromSortedSetByHighestScore(string setId, long fromScore, long toScore) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByHighestScore(setId, fromScore, toScore), setId); } public IDictionary GetRangeWithScoresFromSortedSetByHighestScore(string setId, string fromStringScore, string toStringScore) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByHighestScore(setId, fromStringScore, toStringScore), setId); } public IDictionary GetRangeWithScoresFromSortedSetByHighestScore(string setId, double fromScore, double toScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByHighestScore(setId, fromScore, toScore, skip, take), setId); } public IDictionary GetRangeWithScoresFromSortedSetByHighestScore(string setId, long fromScore, long toScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByHighestScore(setId, fromScore, toScore, skip, take), setId); } public IDictionary GetRangeWithScoresFromSortedSetByHighestScore(string setId, string fromStringScore, string toStringScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByHighestScore(setId, fromStringScore, toStringScore, skip, take), setId); } public IDictionary GetRangeWithScoresFromSortedSetByLowestScore(string setId, double fromScore, double toScore) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByHighestScore(setId, fromScore, toScore), setId); } public IDictionary GetRangeWithScoresFromSortedSetByLowestScore(string setId, long fromScore, long toScore) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByLowestScore(setId, fromScore, toScore), setId); } public IDictionary GetRangeWithScoresFromSortedSetByLowestScore(string setId, string fromStringScore, string toStringScore) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByLowestScore(setId, fromStringScore, toStringScore), setId); } public IDictionary GetRangeWithScoresFromSortedSetByLowestScore(string setId, double fromScore, double toScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByLowestScore(setId, fromScore, toScore, skip, take), setId); } public IDictionary GetRangeWithScoresFromSortedSetByLowestScore(string setId, long fromScore, long toScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByLowestScore(setId, fromScore, toScore, skip, take), setId); } public IDictionary GetRangeWithScoresFromSortedSetByLowestScore(string setId, string fromStringScore, string toStringScore, int? skip, int? take) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetByLowestScore(setId, fromStringScore, toStringScore, skip, take), setId); } public IDictionary GetRangeWithScoresFromSortedSetDesc(string setId, int fromRank, int toRank) { return this.TryCatch>(() => this.client.GetRangeWithScoresFromSortedSetDesc(setId, fromRank, toRank), setId); } public long GetSetCount(string setId) { return this.TryCatch(() => this.client.GetSetCount(setId), setId); } public List GetSortedEntryValues(string key, int startingFrom, int endingAt) { return this.TryCatch>(() => this.client.GetSortedEntryValues(key, startingFrom, endingAt), key); } public long GetSortedSetCount(string setId) { return this.TryCatch(() => this.client.GetSortedSetCount(setId), setId); } public long GetSortedSetCount(string setId, double fromScore, double toScore) { return this.TryCatch(() => this.client.GetSortedSetCount(setId, fromScore, toScore), setId); } public long GetSortedSetCount(string setId, long fromScore, long toScore) { return this.TryCatch(() => this.client.GetSortedSetCount(setId, fromScore, toScore), setId); } public long GetSortedSetCount(string setId, string fromStringScore, string toStringScore) { return this.TryCatch(() => this.client.GetSortedSetCount(setId, fromStringScore, toStringScore), setId); } public string GetSubstring(string key, int fromIndex, int toIndex) { return this.TryCatch(delegate { byte[] bytes = ((RedisClient)this.client).GetRange(key, fromIndex, toIndex); if (bytes != null) { return StringExtensions.FromUtf8Bytes(bytes); } return null; }, key); } public TimeSpan GetTimeToLive(string key) { return this.TryCatch(delegate { TimeSpan? t = this.client.GetTimeToLive(key); if (!t.HasValue) { return TimeSpan.Zero; } return t.Value; }, key); } public HashSet GetUnionFromSets(params string[] setIds) { return this.TryCatch>(() => this.client.GetUnionFromSets(setIds), setIds[0]); } public string GetValue(string key) { return this.TryCatch(() => this.client.GetValue(key), key); } public string GetValueFromHash(string hashId, string key) { return this.TryCatch(() => this.client.GetValueFromHash(hashId, key), hashId); } public List GetValues(List keys) { return this.TryCatch>(() => this.client.GetValues(keys), keys[0]); } public List GetValues(List keys) { return this.TryCatch>(() => this.client.GetValues(keys), keys[0]); } public List GetValuesFromHash(string hashId, params string[] keys) { return this.TryCatch>(() => this.client.GetValuesFromHash(hashId, keys), hashId); } public Dictionary GetValuesMap(List keys) { return this.TryCatch>(() => this.client.GetValuesMap(keys), keys[0]); } public Dictionary GetValuesMap(List keys) { return this.TryCatch>(() => this.client.GetValuesMap(keys), keys[0]); } public bool HashContainsEntry(string hashId, string key) { return this.TryCatch(() => this.client.HashContainsEntry(hashId, key), hashId); } public double IncrementItemInSortedSet(string setId, string value, double incrementBy) { return this.TryCatch(() => this.client.IncrementItemInSortedSet(setId, value, incrementBy), setId); } public double IncrementItemInSortedSet(string setId, string value, long incrementBy) { return this.TryCatch(() => this.client.IncrementItemInSortedSet(setId, value, incrementBy), setId); } public long IncrementValue(string key) { return this.TryCatch(() => this.client.IncrementValue(key), key); } public long IncrementValueBy(string key, int count) { return this.TryCatch(() => this.client.IncrementValueBy(key, count), key); } public long IncrementValueInHash(string hashId, string key, int incrementBy) { return this.TryCatch(() => this.client.IncrementValueInHash(hashId, key, incrementBy), hashId); } public void MoveBetweenSets(string fromSetId, string toSetId, string item) { this.TryCatch(delegate { this.client.MoveBetweenSets(fromSetId, toSetId, item); }, fromSetId); } public string PopAndPushItemBetweenLists(string fromListId, string toListId) { return this.TryCatch(() => this.client.PopAndPushItemBetweenLists(fromListId, toListId), fromListId); } public string PopItemFromList(string listId) { return this.TryCatch(() => this.client.PopItemFromList(listId), listId); } public string PopItemFromSet(string setId) { return this.TryCatch(() => this.client.PopItemFromSet(setId), setId); } public string PopItemWithHighestScoreFromSortedSet(string setId) { return this.TryCatch(() => this.client.PopItemWithHighestScoreFromSortedSet(setId), setId); } public string PopItemWithLowestScoreFromSortedSet(string setId) { return this.TryCatch(() => this.client.PopItemWithLowestScoreFromSortedSet(setId), setId); } public void PrependItemToList(string listId, string value) { this.TryCatch(delegate { this.client.PrependItemToList(listId, value); }, listId); } public void PrependRangeToList(string listId, List values) { this.TryCatch(delegate { this.client.PrependRangeToList(listId, values); }, listId); } public long PublishMessage(string toChannel, string message) { return this.TryCatch(() => this.client.PublishMessage(toChannel, message), string.Empty); } public void PushItemToList(string listId, string value) { this.TryCatch(delegate { this.client.PushItemToList(listId, value); }, listId); } public void RemoveAllFromList(string listId) { this.TryCatch(delegate { this.client.Remove(listId); }, listId); } public string RemoveEndFromList(string listId) { return this.TryCatch(() => this.client.RemoveEndFromList(listId), listId); } public bool RemoveEntry(params string[] args) { return this.TryCatch(() => this.client.RemoveEntry(args), args[0]); } public bool RemoveEntryFromHash(string hashId, string key) { return this.TryCatch(() => this.client.RemoveEntryFromHash(hashId, key), hashId); } public long RemoveItemFromList(string listId, string value) { return this.TryCatch(() => this.client.RemoveItemFromList(listId, value), listId); } public long RemoveItemFromList(string listId, string value, int noOfMatches) { return this.TryCatch(() => this.client.RemoveItemFromList(listId, value, noOfMatches), listId); } public void RemoveItemFromSet(string setId, string item) { this.TryCatch(delegate { this.client.RemoveItemFromSet(setId, item); }, setId); } public bool RemoveItemFromSortedSet(string setId, string value) { return this.TryCatch(() => this.client.RemoveItemFromSortedSet(setId, value), setId); } /// /// 骚操作-- redis 连接池-- 如果出现高并发,客户端的连接数量会上限,为了节省资源,重复利用连接对象,通过线程池去获取连接 /// /// /// /// /// public static IRedisClientsManager GetPoolClient(string host, int port, int db) { return new PooledRedisClientManager(db, host + ":" + port); } public long RemoveRangeFromSortedSet(string setId, int minRank, int maxRank) { return this.TryCatch(() => this.client.RemoveRangeFromSortedSet(setId, minRank, maxRank), setId); } public long RemoveRangeFromSortedSetByScore(string setId, double fromScore, double toScore) { return this.TryCatch(() => this.client.RemoveRangeFromSortedSetByScore(setId, fromScore, toScore), setId); } public long RemoveRangeFromSortedSetByScore(string setId, long fromScore, long toScore) { return this.TryCatch(() => this.client.RemoveRangeFromSortedSetByScore(setId, fromScore, toScore), setId); } public string RemoveStartFromList(string listId) { return this.TryCatch(() => this.client.RemoveStartFromList(listId), listId); } public void RenameKey(string fromName, string toName) { this.TryCatch(delegate { this.client.RenameKey(fromName, toName); }, string.Empty); } public List SearchKeys(string pattern) { return this.TryCatch>(() => this.client.SearchKeys(pattern), pattern); } public void SetAll(Dictionary map) { this.TryCatch(delegate { this.client.SetAll(map); }, string.Empty); } public void SetAll(IEnumerable keys, IEnumerable values) { this.TryCatch(delegate { this.client.SetAll(keys, values); }, string.Empty); } public bool SetContainsItem(string setId, string item) { return this.TryCatch(() => this.client.SetContainsItem(setId, item), setId); } public void SetEntry(string key, string value) { this.TryCatch(delegate { this.client.SetValue(key, value); }, key); } public void SetEntry(string key, string value, TimeSpan expireIn) { this.TryCatch(delegate { this.client.SetValue(key, value, expireIn); }, key); } public bool SetEntryIfNotExists(string key, string value) { return this.TryCatch(() => this.client.SetValueIfNotExists(key, value), key); } public bool SetEntryInHash(string hashId, string key, string value) { return this.TryCatch(() => this.client.SetEntryInHash(hashId, key, value), hashId); } public bool SetEntryInHashIfNotExists(string hashId, string key, string value) { return this.TryCatch(() => this.client.SetEntryInHashIfNotExists(hashId, key, value), hashId); } public void SetItemInList(string listId, int listIndex, string value) { this.TryCatch(delegate { this.client.SetItemInList(listId, listIndex, value); }, listId); } public void SetRangeInHash(string hashId, IEnumerable> keyValuePairs) { this.TryCatch(delegate { this.client.SetRangeInHash(hashId, keyValuePairs); }, hashId); } public bool SortedSetContainsItem(string setId, string value) { return this.TryCatch(() => this.client.SortedSetContainsItem(setId, value), setId); } public void StoreAsHash(T entity) { this.TryCatch(delegate { this.client.StoreAsHash(entity); }, string.Empty); } public bool SetEntryInHash(string hashId, string key, T value) { return this.TryCatch(() => this.client.SetEntryInHash(hashId, key, TextExtensions.SerializeToString(value)), hashId); } public bool SetEntryInHash(string hashId, string key, T value, TimeSpan expiresIn) { return this.TryCatch(() => this.client.SetEntryInHash(hashId, key, TextExtensions.SerializeToString(value)), hashId); } public T GetValueFromHash(string hashId, string key) { return this.TryCatch(() => JsonSerializer.DeserializeFromString(this.client.GetValueFromHash(hashId, key)), hashId); } public bool SetEntryInHashIfNotExists(string hashId, string key, T value) { return this.TryCatch(() => this.client.SetEntryInHashIfNotExists(hashId, key, TextExtensions.SerializeToString(value)), hashId); } public IDisposable AcquireLock(string key) { return this.TryCatch(() => this.client.AcquireLock(key), key); } public IDisposable AcquireLock(string key, TimeSpan timeOut) { return this.TryCatch(() => this.client.AcquireLock(key, timeOut), key); } public DateTime GetServerTime() { return this.TryCatch(() => this.client.GetServerTime(), string.Empty); } } }