diff --git a/README.md b/README.md index 392d8d61..97c71485 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ EasyCaching is an open source caching library that contains basic usages and som | Platform | Build Server | Master Status | Dev Status | |--------- |------------- |---------|---------| -| Github Action | Linux/Windows | [![Build&Test](https://github.com/dotnetcore/EasyCaching/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/dotnetcore/EasyCaching/actions/workflows/build.yml) | [![Build&Test](https://github.com/dotnetcore/EasyCaching/actions/workflows/build.yml/badge.svg?branch=dev)](https://github.com/dotnetcore/EasyCaching/actions/workflows/build.yml) | +| Github Action | Linux/Windows | [![Build&Test](https://github.com/dotnetcore/EasyCaching/actions/workflows/buildandtest.yml/badge.svg)](https://github.com/dotnetcore/EasyCaching/actions/workflows/buildandtest.yml) | [![Build&Test](https://github.com/dotnetcore/EasyCaching/actions/workflows/buildandtest.yml/badge.svg)](https://github.com/dotnetcore/EasyCaching/actions/workflows/buildandtest.yml) | ## Nuget Packages @@ -160,9 +160,5 @@ See [ToDo List](docs/ToDoList.md) Pull requests, issues and commentary! -Also can join our QQ group. - -![](media/qqgroup.jpg?raw=true) - ## License [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fdotnetcore%2FEasyCaching.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fdotnetcore%2FEasyCaching?ref=badge_large) diff --git a/build/releasenotes.props b/build/releasenotes.props index 93f2c552..f06fb69b 100644 --- a/build/releasenotes.props +++ b/build/releasenotes.props @@ -1,14 +1,18 @@ - 1. Upgrading dependencies. - + 1. IRedisCachingProvider Support SearchKeysAsync. + 2. IRedisCachingProvider Support ZRangeByScore/ZRangeByScoreAsync. + 1. Upgrading dependencies. + 2. Throw exception when can not connect to server - 1. Fix removebyprefix when enable keyprefix. - + 1. IRedisCachingProvider Support SearchKeysAsync/ZRangeByScore/ZRangeByScoreAsync. + 2. Make clear exception message when serializer not match or notfound. + 3. Upgrading dependencies. + 1. Upgrading dependencies. @@ -37,8 +41,10 @@ 1. Upgrading dependencies. - 1. Upgrading dependencies. - + 1. IRedisCachingProvider Support SearchKeysAsync/ZRangeByScore/ZRangeByScoreAsync. + 2. Make clear exception message when serializer not match or notfound. + 3. Upgrading dependencies. + 1. Upgrading dependencies. @@ -46,7 +52,7 @@ 1. Upgrading dependencies. - 1. Allow user to define client provided name. + 1. Upgrading dependencies. 1. Upgrading dependencies. diff --git a/build/version.props b/build/version.props index 2ad801b0..8516dbfe 100644 --- a/build/version.props +++ b/build/version.props @@ -1,24 +1,24 @@ - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 + 1.6.0 diff --git a/bus/EasyCaching.Bus.CSRedis/EasyCaching.Bus.CSRedis.csproj b/bus/EasyCaching.Bus.CSRedis/EasyCaching.Bus.CSRedis.csproj index bf1c4cf5..044c7fb9 100644 --- a/bus/EasyCaching.Bus.CSRedis/EasyCaching.Bus.CSRedis.csproj +++ b/bus/EasyCaching.Bus.CSRedis/EasyCaching.Bus.CSRedis.csproj @@ -32,7 +32,7 @@ - + diff --git a/bus/EasyCaching.Bus.RabbitMQ/EasyCaching.Bus.RabbitMQ.csproj b/bus/EasyCaching.Bus.RabbitMQ/EasyCaching.Bus.RabbitMQ.csproj index 9e2b6cf8..b58ec4f0 100644 --- a/bus/EasyCaching.Bus.RabbitMQ/EasyCaching.Bus.RabbitMQ.csproj +++ b/bus/EasyCaching.Bus.RabbitMQ/EasyCaching.Bus.RabbitMQ.csproj @@ -36,7 +36,7 @@ - + diff --git a/bus/EasyCaching.Bus.Redis/EasyCaching.Bus.Redis.csproj b/bus/EasyCaching.Bus.Redis/EasyCaching.Bus.Redis.csproj index 16674a4e..f6f506e2 100644 --- a/bus/EasyCaching.Bus.Redis/EasyCaching.Bus.Redis.csproj +++ b/bus/EasyCaching.Bus.Redis/EasyCaching.Bus.Redis.csproj @@ -31,7 +31,7 @@ - + diff --git a/interceptor/EasyCaching.Interceptor.Castle/EasyCaching.Interceptor.Castle.csproj b/interceptor/EasyCaching.Interceptor.Castle/EasyCaching.Interceptor.Castle.csproj index 7a00ffb9..fc182abd 100644 --- a/interceptor/EasyCaching.Interceptor.Castle/EasyCaching.Interceptor.Castle.csproj +++ b/interceptor/EasyCaching.Interceptor.Castle/EasyCaching.Interceptor.Castle.csproj @@ -36,7 +36,7 @@ - + diff --git a/serialization/EasyCaching.Serialization.Protobuf/EasyCaching.Serialization.Protobuf.csproj b/serialization/EasyCaching.Serialization.Protobuf/EasyCaching.Serialization.Protobuf.csproj index ea1dbfaa..465675d6 100644 --- a/serialization/EasyCaching.Serialization.Protobuf/EasyCaching.Serialization.Protobuf.csproj +++ b/serialization/EasyCaching.Serialization.Protobuf/EasyCaching.Serialization.Protobuf.csproj @@ -35,7 +35,7 @@ - + diff --git a/serialization/EasyCaching.Serialization.SystemTextJson/EasyCaching.Serialization.SystemTextJson.csproj b/serialization/EasyCaching.Serialization.SystemTextJson/EasyCaching.Serialization.SystemTextJson.csproj index 7769db49..b39653d2 100644 --- a/serialization/EasyCaching.Serialization.SystemTextJson/EasyCaching.Serialization.SystemTextJson.csproj +++ b/serialization/EasyCaching.Serialization.SystemTextJson/EasyCaching.Serialization.SystemTextJson.csproj @@ -31,7 +31,7 @@ - + diff --git a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Keys.cs b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Keys.cs index 72c542de..fbba8564 100755 --- a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Keys.cs +++ b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Keys.cs @@ -106,5 +106,22 @@ public List SearchKeys(string cacheKey, int? count) return keys; } + + public async Task> SearchKeysAsync(string cacheKey, int? count) + { + var keys = new List(); + + long nextCursor = 0; + do + { + var scanResult = await _cache.ScanAsync(nextCursor, cacheKey, count ?? 250); + nextCursor = scanResult.Cursor; + var items = scanResult.Items; + keys.AddRange(items); + } + while (nextCursor != 0); + + return keys; + } } } diff --git a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.SortedSet.cs b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.SortedSet.cs index 1c603ce3..9465fc3e 100755 --- a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.SortedSet.cs +++ b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.SortedSet.cs @@ -1,10 +1,10 @@ -namespace EasyCaching.CSRedis +namespace EasyCaching.CSRedis { using EasyCaching.Core; - using EasyCaching.Core.Internal; - using System.Collections.Generic; - using System.Threading.Tasks; - + using EasyCaching.Core.Internal; + using System.Collections.Generic; + using System.Threading.Tasks; + public partial class DefaultCSRedisCachingProvider : IRedisCachingProvider { public long ZAdd(string cacheKey, Dictionary cacheValues) @@ -25,48 +25,64 @@ public long ZAdd(string cacheKey, Dictionary cacheValues) public long ZCard(string cacheKey) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = _cache.ZCard(cacheKey); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = _cache.ZCard(cacheKey); return len; } public long ZCount(string cacheKey, double min, double max) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = _cache.ZCount(cacheKey, (decimal)min, (decimal)max); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = _cache.ZCount(cacheKey, (decimal)min, (decimal)max); return len; } public double ZIncrBy(string cacheKey, string field, double val = 1) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field)); - + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field)); + var value = _cache.ZIncrBy(cacheKey, field, (decimal)val); return (double)value; } public long ZLexCount(string cacheKey, string min, string max) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = _cache.ZLexCount(cacheKey, min, max); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = _cache.ZLexCount(cacheKey, min, max); return len; } public List ZRange(string cacheKey, long start, long stop) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var list = new List(); - - var bytes = _cache.ZRange(cacheKey, start, stop); - + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var list = new List(); + + var bytes = _cache.ZRange(cacheKey, start, stop); + + foreach (var item in bytes) + { + list.Add(_serializer.Deserialize(item)); + } + + return list; + } + + public List ZRangeByScore(string cacheKey, double min, double max, long? count = null, long offset = 0) + { + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var list = new List(); + + var bytes = _cache.ZRangeByScore(cacheKey, (decimal)min, (decimal)max, count, offset); + foreach (var item in bytes) { list.Add(_serializer.Deserialize(item)); - } - + } + return list; } @@ -74,8 +90,8 @@ public List ZRange(string cacheKey, long start, long stop) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var bytes = _serializer.Serialize(cacheValue); - + var bytes = _serializer.Serialize(cacheValue); + var index = _cache.ZRank(cacheKey, bytes); return index; @@ -86,27 +102,27 @@ public long ZRem(string cacheKey, IList cacheValues) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var bytes = new List(); - + var bytes = new List(); + foreach (var item in cacheValues) { bytes.Add(_serializer.Serialize(item)); - } - + } + var len = _cache.ZRem(cacheKey, bytes.ToArray()); return len; - } - - public double? ZScore(string cacheKey, T cacheValue) - { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var bytes = _serializer.Serialize(cacheValue); - - var score = _cache.ZScore(cacheKey, bytes); - - return (double?)score; + } + + public double? ZScore(string cacheKey, T cacheValue) + { + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var bytes = _serializer.Serialize(cacheValue); + + var score = _cache.ZScore(cacheKey, bytes); + + return (double?)score; } public async Task ZAddAsync(string cacheKey, Dictionary cacheValues) @@ -128,50 +144,66 @@ public async Task ZAddAsync(string cacheKey, Dictionary cach public async Task ZCardAsync(string cacheKey) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = await _cache.ZCardAsync(cacheKey); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = await _cache.ZCardAsync(cacheKey); return len; - } - + } + public async Task ZCountAsync(string cacheKey, double min, double max) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = await _cache.ZCountAsync(cacheKey, (decimal)min, (decimal)max); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = await _cache.ZCountAsync(cacheKey, (decimal)min, (decimal)max); return len; } public async Task ZIncrByAsync(string cacheKey, string field, double val = 1) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field)); - + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field)); + var value= await _cache.ZIncrByAsync(cacheKey, field, (decimal)val); return (double)value; - } - + } + public async Task ZLexCountAsync(string cacheKey, string min, string max) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = await _cache.ZLexCountAsync(cacheKey, min, max); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = await _cache.ZLexCountAsync(cacheKey, min, max); return len; } public async Task> ZRangeAsync(string cacheKey, long start, long stop) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var list = new List(); - - var bytes = await _cache.ZRangeAsync(cacheKey, start, stop); - + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var list = new List(); + + var bytes = await _cache.ZRangeAsync(cacheKey, start, stop); + foreach (var item in bytes) { list.Add(_serializer.Deserialize(item)); - } - + } + + return list; + } + + public async Task> ZRangeByScoreAsync(string cacheKey, double min, double max, long? count = null, long offset = 0) + { + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var list = new List(); + + var bytes = await _cache.ZRangeByScoreAsync(cacheKey, (decimal)min, (decimal)max, count, offset); + + foreach (var item in bytes) + { + list.Add(_serializer.Deserialize(item)); + } + return list; } @@ -179,8 +211,8 @@ public async Task> ZRangeAsync(string cacheKey, long start, long stop { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var bytes = _serializer.Serialize(cacheValue); - + var bytes = _serializer.Serialize(cacheValue); + var index = await _cache.ZRankAsync(cacheKey, bytes); return index; @@ -190,28 +222,28 @@ public async Task ZRemAsync(string cacheKey, IList cacheValues) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var bytes = new List(); - + var bytes = new List(); + foreach (var item in cacheValues) { bytes.Add(_serializer.Serialize(item)); - } - + } + var len = await _cache.ZRemAsync(cacheKey, bytes.ToArray()); return len; - } - - public async Task ZScoreAsync(string cacheKey, T cacheValue) - { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var bytes = _serializer.Serialize(cacheValue); - - var score = await _cache.ZScoreAsync(cacheKey, bytes); - - return (double?)score; - } - - } -} + } + + public async Task ZScoreAsync(string cacheKey, T cacheValue) + { + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var bytes = _serializer.Serialize(cacheValue); + + var score = await _cache.ZScoreAsync(cacheKey, bytes); + + return (double?)score; + } + + } +} diff --git a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs index b090616b..5a3a8790 100644 --- a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs +++ b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs @@ -86,12 +86,15 @@ public DefaultCSRedisCachingProvider( this._name = name; this._options = options; this._logger = loggerFactory?.CreateLogger(); - this._cache = clients.Single(x => x.Name.Equals(_name)); + this._cache = clients.FirstOrDefault(x => x.Name.Equals(_name)); + + if (this._cache == null) throw new EasyCachingNotFoundException(string.Format(EasyCachingConstValue.NotFoundCliExceptionMessage, _name)); + this._cacheStats = new CacheStats(); - this._serializer = !string.IsNullOrWhiteSpace(options.SerializerName) - ? serializers.Single(x => x.Name.Equals(options.SerializerName)) - : serializers.FirstOrDefault(x => x.Name.Equals(_name)) ?? serializers.Single(x => x.Name.Equals(EasyCachingConstValue.DefaultSerializerName)); + var serName = !string.IsNullOrWhiteSpace(options.SerializerName) ? options.SerializerName : _name; + this._serializer = serializers.FirstOrDefault(x => x.Name.Equals(serName)); + if (this._serializer == null) throw new EasyCachingNotFoundException(string.Format(EasyCachingConstValue.NotFoundSerExceptionMessage, serName)); this.ProviderName = this._name; this.ProviderType = CachingProviderType.Redis; @@ -267,6 +270,9 @@ private string HandlePrefix(string prefix) if (!prefix.EndsWith("*", StringComparison.OrdinalIgnoreCase)) prefix = string.Concat(prefix, "*"); + if (!string.IsNullOrWhiteSpace(_cache.Nodes?.Values?.FirstOrDefault()?.Prefix)) + prefix = _cache.Nodes?.Values?.FirstOrDefault()?.Prefix + prefix; + return prefix; } @@ -289,6 +295,11 @@ private string[] SearchRedisKeys(string pattern) } while (nextCursor != 0); + var prefix = _cache.Nodes?.Values?.FirstOrDefault()?.Prefix; + + if (!string.IsNullOrWhiteSpace(prefix)) + keys = keys.Select(x => x.Remove(0, prefix.Length)).ToList(); + return keys.Distinct().ToArray(); } diff --git a/src/EasyCaching.CSRedis/EasyCaching.CSRedis.csproj b/src/EasyCaching.CSRedis/EasyCaching.CSRedis.csproj index e589aa4d..9ddaee11 100644 --- a/src/EasyCaching.CSRedis/EasyCaching.CSRedis.csproj +++ b/src/EasyCaching.CSRedis/EasyCaching.CSRedis.csproj @@ -32,7 +32,7 @@ - + diff --git a/src/EasyCaching.Core/EasyCachingException.cs b/src/EasyCaching.Core/EasyCachingException.cs new file mode 100644 index 00000000..50c44352 --- /dev/null +++ b/src/EasyCaching.Core/EasyCachingException.cs @@ -0,0 +1,25 @@ +namespace EasyCaching.Core +{ + using System; + + public class EasyCachingException : Exception + { + public EasyCachingException(string message) + : base(message) + { + } + + public EasyCachingException(string message, Exception innerException) + : base(message, innerException) + { + } + } + + public class EasyCachingNotFoundException : Exception + { + public EasyCachingNotFoundException(string message) + : base(message) + { + } + } +} diff --git a/src/EasyCaching.Core/IRedisCachingProvider.cs b/src/EasyCaching.Core/IRedisCachingProvider.cs index c8183813..c31c6ed7 100644 --- a/src/EasyCaching.Core/IRedisCachingProvider.cs +++ b/src/EasyCaching.Core/IRedisCachingProvider.cs @@ -1,822 +1,852 @@ -namespace EasyCaching.Core -{ - using System; - using System.Collections.Generic; - using System.Threading.Tasks; - - /// - /// Redis caching provider. - /// - /// - /// Contains some features of redis - /// - public interface IRedisCachingProvider - { - string RedisName { get; } - - #region Keys - /// - /// https://redis.io/commands/del - /// - /// - /// - bool KeyDel(string cacheKey); - /// - /// https://redis.io/commands/del - /// - /// - /// - Task KeyDelAsync(string cacheKey); - /// - /// https://redis.io/commands/expire - /// - /// - /// - /// - bool KeyExpire(string cacheKey, int second); - /// - /// https://redis.io/commands/expire - /// - /// - /// - /// - Task KeyExpireAsync(string cacheKey, int second); - /// - /// - /// - /// - /// - Task KeyExistsAsync(string cacheKey); - /// - /// - /// - /// - /// - bool KeyExists(string cacheKey); - /// - /// https://redis.io/commands/ttl - /// - /// - /// - long TTL(string cacheKey); - /// - /// https://redis.io/commands/ttl - /// - /// - /// - Task TTLAsync(string cacheKey); - +namespace EasyCaching.Core +{ + using System; + using System.Collections.Generic; + using System.Threading.Tasks; + + /// + /// Redis caching provider. + /// + /// + /// Contains some features of redis + /// + public interface IRedisCachingProvider + { + string RedisName { get; } + + #region Keys + /// + /// https://redis.io/commands/del + /// + /// + /// + bool KeyDel(string cacheKey); + /// + /// https://redis.io/commands/del + /// + /// + /// + Task KeyDelAsync(string cacheKey); + /// + /// https://redis.io/commands/expire + /// + /// + /// + /// + bool KeyExpire(string cacheKey, int second); + /// + /// https://redis.io/commands/expire + /// + /// + /// + /// + Task KeyExpireAsync(string cacheKey, int second); + /// + /// + /// + /// + /// + Task KeyExistsAsync(string cacheKey); + /// + /// + /// + /// + /// + bool KeyExists(string cacheKey); + /// + /// https://redis.io/commands/ttl + /// + /// + /// + long TTL(string cacheKey); + /// + /// https://redis.io/commands/ttl + /// + /// + /// + Task TTLAsync(string cacheKey); + + /// + /// Searchs the keys. + /// + /// + /// + /// + List SearchKeys(string cacheKey, int? count = null); + /// /// Searchs the keys. /// /// /// /// - List SearchKeys(string cacheKey, int? count = null); - #endregion - - #region String - /// - /// https://redis.io/commands/incrby - /// - /// - /// - /// - long IncrBy(string cacheKey, long value = 1); - /// - /// https://redis.io/commands/incrby - /// - /// - /// - /// - Task IncrByAsync(string cacheKey, long value = 1); - /// - /// https://redis.io/commands/incrbyfloat - /// - /// - /// - /// - double IncrByFloat(string cacheKey, double value = 1); - /// - /// https://redis.io/commands/incrbyfloat - /// - /// - /// - /// - Task IncrByFloatAsync(string cacheKey, double value = 1); - /// - /// https://redis.io/commands/set - /// - /// - /// - /// - /// nx,xx, - /// - bool StringSet(string cacheKey, string cacheValue, System.TimeSpan? expiration = null, string when = ""); - /// - /// https://redis.io/commands/set - /// - /// - /// - /// - /// nx,xx, - /// - Task StringSetAsync(string cacheKey, string cacheValue, System.TimeSpan? expiration = null, string when = ""); - /// - /// https://redis.io/commands/get - /// - /// - /// - string StringGet(string cacheKey); - /// - /// https://redis.io/commands/get - /// - /// - /// - Task StringGetAsync(string cacheKey); - /// - /// https://redis.io/commands/strlen - /// - /// - /// - long StringLen(string cacheKey); - /// - /// https://redis.io/commands/strlen - /// - /// - /// - Task StringLenAsync(string cacheKey); - /// - /// https://redis.io/commands/setrange - /// - /// - /// - /// - /// - long StringSetRange(string cacheKey, long offest, string value); - /// - /// https://redis.io/commands/setrange - /// - /// - /// - /// - /// - Task StringSetRangeAsync(string cacheKey, long offest, string value); - /// - /// https://redis.io/commands/getrange - /// - /// - /// - /// - /// - string StringGetRange(string cacheKey, long start, long end); - /// - /// https://redis.io/commands/getrange - /// - /// - /// - /// - /// - Task StringGetRangeAsync(string cacheKey, long start, long end); - #endregion - - #region Hashes - /// - /// https://redis.io/commands/hmset - /// - /// - /// - /// - /// - bool HMSet(string cacheKey, Dictionary vals, TimeSpan? expiration = null); - /// - /// https://redis.io/commands/hset - /// - /// - /// - /// - /// - bool HSet(string cacheKey, string field, string cacheValue); - /// - /// https://redis.io/commands/hexists - /// - /// - /// - /// - bool HExists(string cacheKey, string field); - /// - /// https://redis.io/commands/hdel - /// - /// - /// - /// - long HDel(string cacheKey, IList fields = null); - /// - /// https://redis.io/commands/hget - /// - /// - /// - /// - string HGet(string cacheKey, string field); - /// - /// https://redis.io/commands/hgetall - /// - /// - /// - Dictionary HGetAll(string cacheKey); - /// - /// https://redis.io/commands/hincrby - /// - /// - /// - /// - /// - long HIncrBy(string cacheKey, string field, long val = 1); - /// - /// https://redis.io/commands/hkeys - /// - /// - /// - List HKeys(string cacheKey); - /// - /// https://redis.io/commands/hlen - /// - /// - /// - long HLen(string cacheKey); - /// - /// https://redis.io/commands/hvals - /// - /// - /// - List HVals(string cacheKey); - /// - /// https://redis.io/commands/hmget - /// - /// - /// - /// - Dictionary HMGet(string cacheKey, IList fields); - /// - /// https://redis.io/commands/hset - /// - /// - /// - /// - /// - Task HMSetAsync(string cacheKey, Dictionary vals, TimeSpan? expiration = null); - /// - /// https://redis.io/commands/hset - /// - /// - /// - /// - /// - Task HSetAsync(string cacheKey, string field, string cacheValue); - /// - /// https://redis.io/commands/hexists - /// - /// - /// - /// - Task HExistsAsync(string cacheKey, string field); - /// - /// https://redis.io/commands/hdel - /// - /// - /// - /// - Task HDelAsync(string cacheKey, IList fields = null); - /// - /// https://redis.io/commands/hget - /// - /// - /// - /// - Task HGetAsync(string cacheKey, string field); - /// - /// https://redis.io/commands/hgetall - /// - /// - /// - Task> HGetAllAsync(string cacheKey); - /// - /// https://redis.io/commands/hincrby - /// - /// - /// - /// - /// - Task HIncrByAsync(string cacheKey, string field, long val = 1); - /// - /// https://redis.io/commands/hkeys - /// - /// - /// - Task> HKeysAsync(string cacheKey); - /// - /// https://redis.io/commands/hlen - /// - /// - /// - Task HLenAsync(string cacheKey); - /// - /// https://redis.io/commands/hvals - /// - /// - /// - Task> HValsAsync(string cacheKey); - /// - /// https://redis.io/commands/hmget - /// - /// - /// - /// - Task> HMGetAsync(string cacheKey, IList fields); - #endregion - - #region List - /// - /// https://redis.io/commands/lindex - /// - /// - /// - /// - /// - T LIndex(string cacheKey, long index); - /// - /// https://redis.io/commands/llen - /// - /// - /// - long LLen(string cacheKey); - /// - /// https://redis.io/commands/lpop - /// - /// - /// - /// - T LPop(string cacheKey); - /// - /// https://redis.io/commands/lpush - /// - /// - /// - /// - /// - long LPush(string cacheKey, IList cacheValues); - /// - /// https://redis.io/commands/lrange - /// - /// - /// - /// - /// - /// - List LRange(string cacheKey, long start, long stop); - /// - /// https://redis.io/commands/lrem - /// - /// - /// - /// - /// - /// - long LRem(string cacheKey, long count, T cacheValue); - /// - /// https://redis.io/commands/lset - /// - /// - /// - /// - /// - /// - bool LSet(string cacheKey, long index, T cacheValue); - /// - /// https://redis.io/commands/ltrim - /// - /// - /// - /// - /// - bool LTrim(string cacheKey, long start, long stop); - /// - /// https://redis.io/commands/lpushx - /// - /// - /// - /// - /// - long LPushX(string cacheKey, T cacheValue); - /// - /// https://redis.io/commands/linsert - /// - /// - /// - /// - /// - /// - long LInsertBefore(string cacheKey, T pivot, T cacheValue); - /// - /// https://redis.io/commands/linsert - /// - /// - /// - /// - /// - /// - long LInsertAfter(string cacheKey, T pivot, T cacheValue); - /// - /// https://redis.io/commands/rpushx - /// - /// - /// - /// - /// - long RPushX(string cacheKey, T cacheValue); - /// - /// https://redis.io/commands/rpush - /// - /// - /// - /// - /// - long RPush(string cacheKey, IList cacheValues); - /// - /// https://redis.io/commands/rpop - /// - /// - /// - /// - T RPop(string cacheKey); - /// - /// https://redis.io/commands/lindex - /// - /// - /// - /// - /// - Task LIndexAsync(string cacheKey, long index); - /// - /// https://redis.io/commands/llen - /// - /// - /// - Task LLenAsync(string cacheKey); - /// - /// https://redis.io/commands/lpop - /// - /// - /// - /// - Task LPopAsync(string cacheKey); - /// - /// https://redis.io/commands/lpush - /// - /// - /// - /// - /// - Task LPushAsync(string cacheKey, IList cacheValues); - /// - /// https://redis.io/commands/lrange - /// - /// - /// - /// - /// - /// - Task> LRangeAsync(string cacheKey, long start, long stop); - /// - /// https://redis.io/commands/lrem - /// - /// - /// - /// - /// - /// - Task LRemAsync(string cacheKey, long count, T cacheValue); - /// - /// https://redis.io/commands/lset - /// - /// - /// - /// - /// - /// - Task LSetAsync(string cacheKey, long index, T cacheValue); - /// - /// https://redis.io/commands/ltrim - /// - /// - /// - /// - /// - Task LTrimAsync(string cacheKey, long start, long stop); - /// - /// https://redis.io/commands/lpushx - /// - /// - /// - /// - /// - Task LPushXAsync(string cacheKey, T cacheValue); - /// - /// https://redis.io/commands/linsert - /// - /// - /// - /// - /// - /// - Task LInsertBeforeAsync(string cacheKey, T pivot, T cacheValue); - /// - /// https://redis.io/commands/linsert - /// - /// - /// - /// - /// - /// - Task LInsertAfterAsync(string cacheKey, T pivot, T cacheValue); - /// - /// https://redis.io/commands/rpushx - /// - /// - /// - /// - /// - Task RPushXAsync(string cacheKey, T cacheValue); - /// - /// https://redis.io/commands/rpush - /// - /// - /// - /// - /// - Task RPushAsync(string cacheKey, IList cacheValues); - /// - /// https://redis.io/commands/rpop - /// - /// - /// - /// - Task RPopAsync(string cacheKey); - #endregion - - #region Set - /// - /// https://redis.io/commands/sadd - /// - /// - /// - /// - /// - /// - long SAdd(string cacheKey, IList cacheValues, TimeSpan? expiration = null); - /// - /// https://redis.io/commands/scard - /// - /// - /// - long SCard(string cacheKey); - /// - /// https://redis.io/commands/sismember - /// - /// - /// - /// - /// - bool SIsMember(string cacheKey, T cacheValue); - /// - /// https://redis.io/commands/smembers - /// - /// - /// - /// - List SMembers(string cacheKey); - /// - /// https://redis.io/commands/spop - /// - /// - /// - /// - T SPop(string cacheKey); - /// - /// https://redis.io/commands/srandmember - /// - /// - /// - /// - /// - List SRandMember(string cacheKey, int count = 1); - /// - /// https://redis.io/commands/srem - /// - /// - /// - /// - /// - long SRem(string cacheKey, IList cacheValues = null); - /// - /// https://redis.io/commands/sadd - /// - /// - /// - /// - /// - /// - Task SAddAsync(string cacheKey, IList cacheValues, TimeSpan? expiration = null); - /// - /// https://redis.io/commands/scard - /// - /// - /// - Task SCardAsync(string cacheKey); - /// - /// https://redis.io/commands/sismember - /// - /// - /// - /// - /// - Task SIsMemberAsync(string cacheKey, T cacheValue); - /// - /// https://redis.io/commands/smembers - /// - /// - /// - /// - Task> SMembersAsync(string cacheKey); - /// - /// https://redis.io/commands/spop - /// - /// - /// - /// - Task SPopAsync(string cacheKey); - /// - /// https://redis.io/commands/srandmember - /// - /// - /// - /// - /// - Task> SRandMemberAsync(string cacheKey, int count = 1); - /// - /// https://redis.io/commands/srem - /// - /// - /// - /// - /// - Task SRemAsync(string cacheKey, IList cacheValues = null); - #endregion - - #region Sorted Set - /// - /// https://redis.io/commands/zadd - /// - /// - /// - /// - /// - long ZAdd(string cacheKey, Dictionary cacheValues); - /// - /// https://redis.io/commands/zcard - /// - /// - /// - long ZCard(string cacheKey); - /// - /// https://redis.io/commands/zcount - /// - /// - /// - /// - /// + Task> SearchKeysAsync(string cacheKey, int? count = null); + #endregion + + #region String + /// + /// https://redis.io/commands/incrby + /// + /// + /// + /// + long IncrBy(string cacheKey, long value = 1); + /// + /// https://redis.io/commands/incrby + /// + /// + /// + /// + Task IncrByAsync(string cacheKey, long value = 1); + /// + /// https://redis.io/commands/incrbyfloat + /// + /// + /// + /// + double IncrByFloat(string cacheKey, double value = 1); + /// + /// https://redis.io/commands/incrbyfloat + /// + /// + /// + /// + Task IncrByFloatAsync(string cacheKey, double value = 1); + /// + /// https://redis.io/commands/set + /// + /// + /// + /// + /// nx,xx, + /// + bool StringSet(string cacheKey, string cacheValue, System.TimeSpan? expiration = null, string when = ""); + /// + /// https://redis.io/commands/set + /// + /// + /// + /// + /// nx,xx, + /// + Task StringSetAsync(string cacheKey, string cacheValue, System.TimeSpan? expiration = null, string when = ""); + /// + /// https://redis.io/commands/get + /// + /// + /// + string StringGet(string cacheKey); + /// + /// https://redis.io/commands/get + /// + /// + /// + Task StringGetAsync(string cacheKey); + /// + /// https://redis.io/commands/strlen + /// + /// + /// + long StringLen(string cacheKey); + /// + /// https://redis.io/commands/strlen + /// + /// + /// + Task StringLenAsync(string cacheKey); + /// + /// https://redis.io/commands/setrange + /// + /// + /// + /// + /// + long StringSetRange(string cacheKey, long offest, string value); + /// + /// https://redis.io/commands/setrange + /// + /// + /// + /// + /// + Task StringSetRangeAsync(string cacheKey, long offest, string value); + /// + /// https://redis.io/commands/getrange + /// + /// + /// + /// + /// + string StringGetRange(string cacheKey, long start, long end); + /// + /// https://redis.io/commands/getrange + /// + /// + /// + /// + /// + Task StringGetRangeAsync(string cacheKey, long start, long end); + #endregion + + #region Hashes + /// + /// https://redis.io/commands/hmset + /// + /// + /// + /// + /// + bool HMSet(string cacheKey, Dictionary vals, TimeSpan? expiration = null); + /// + /// https://redis.io/commands/hset + /// + /// + /// + /// + /// + bool HSet(string cacheKey, string field, string cacheValue); + /// + /// https://redis.io/commands/hexists + /// + /// + /// + /// + bool HExists(string cacheKey, string field); + /// + /// https://redis.io/commands/hdel + /// + /// + /// + /// + long HDel(string cacheKey, IList fields = null); + /// + /// https://redis.io/commands/hget + /// + /// + /// + /// + string HGet(string cacheKey, string field); + /// + /// https://redis.io/commands/hgetall + /// + /// + /// + Dictionary HGetAll(string cacheKey); + /// + /// https://redis.io/commands/hincrby + /// + /// + /// + /// + /// + long HIncrBy(string cacheKey, string field, long val = 1); + /// + /// https://redis.io/commands/hkeys + /// + /// + /// + List HKeys(string cacheKey); + /// + /// https://redis.io/commands/hlen + /// + /// + /// + long HLen(string cacheKey); + /// + /// https://redis.io/commands/hvals + /// + /// + /// + List HVals(string cacheKey); + /// + /// https://redis.io/commands/hmget + /// + /// + /// + /// + Dictionary HMGet(string cacheKey, IList fields); + /// + /// https://redis.io/commands/hset + /// + /// + /// + /// + /// + Task HMSetAsync(string cacheKey, Dictionary vals, TimeSpan? expiration = null); + /// + /// https://redis.io/commands/hset + /// + /// + /// + /// + /// + Task HSetAsync(string cacheKey, string field, string cacheValue); + /// + /// https://redis.io/commands/hexists + /// + /// + /// + /// + Task HExistsAsync(string cacheKey, string field); + /// + /// https://redis.io/commands/hdel + /// + /// + /// + /// + Task HDelAsync(string cacheKey, IList fields = null); + /// + /// https://redis.io/commands/hget + /// + /// + /// + /// + Task HGetAsync(string cacheKey, string field); + /// + /// https://redis.io/commands/hgetall + /// + /// + /// + Task> HGetAllAsync(string cacheKey); + /// + /// https://redis.io/commands/hincrby + /// + /// + /// + /// + /// + Task HIncrByAsync(string cacheKey, string field, long val = 1); + /// + /// https://redis.io/commands/hkeys + /// + /// + /// + Task> HKeysAsync(string cacheKey); + /// + /// https://redis.io/commands/hlen + /// + /// + /// + Task HLenAsync(string cacheKey); + /// + /// https://redis.io/commands/hvals + /// + /// + /// + Task> HValsAsync(string cacheKey); + /// + /// https://redis.io/commands/hmget + /// + /// + /// + /// + Task> HMGetAsync(string cacheKey, IList fields); + #endregion + + #region List + /// + /// https://redis.io/commands/lindex + /// + /// + /// + /// + /// + T LIndex(string cacheKey, long index); + /// + /// https://redis.io/commands/llen + /// + /// + /// + long LLen(string cacheKey); + /// + /// https://redis.io/commands/lpop + /// + /// + /// + /// + T LPop(string cacheKey); + /// + /// https://redis.io/commands/lpush + /// + /// + /// + /// + /// + long LPush(string cacheKey, IList cacheValues); + /// + /// https://redis.io/commands/lrange + /// + /// + /// + /// + /// + /// + List LRange(string cacheKey, long start, long stop); + /// + /// https://redis.io/commands/lrem + /// + /// + /// + /// + /// + /// + long LRem(string cacheKey, long count, T cacheValue); + /// + /// https://redis.io/commands/lset + /// + /// + /// + /// + /// + /// + bool LSet(string cacheKey, long index, T cacheValue); + /// + /// https://redis.io/commands/ltrim + /// + /// + /// + /// + /// + bool LTrim(string cacheKey, long start, long stop); + /// + /// https://redis.io/commands/lpushx + /// + /// + /// + /// + /// + long LPushX(string cacheKey, T cacheValue); + /// + /// https://redis.io/commands/linsert + /// + /// + /// + /// + /// + /// + long LInsertBefore(string cacheKey, T pivot, T cacheValue); + /// + /// https://redis.io/commands/linsert + /// + /// + /// + /// + /// + /// + long LInsertAfter(string cacheKey, T pivot, T cacheValue); + /// + /// https://redis.io/commands/rpushx + /// + /// + /// + /// + /// + long RPushX(string cacheKey, T cacheValue); + /// + /// https://redis.io/commands/rpush + /// + /// + /// + /// + /// + long RPush(string cacheKey, IList cacheValues); + /// + /// https://redis.io/commands/rpop + /// + /// + /// + /// + T RPop(string cacheKey); + /// + /// https://redis.io/commands/lindex + /// + /// + /// + /// + /// + Task LIndexAsync(string cacheKey, long index); + /// + /// https://redis.io/commands/llen + /// + /// + /// + Task LLenAsync(string cacheKey); + /// + /// https://redis.io/commands/lpop + /// + /// + /// + /// + Task LPopAsync(string cacheKey); + /// + /// https://redis.io/commands/lpush + /// + /// + /// + /// + /// + Task LPushAsync(string cacheKey, IList cacheValues); + /// + /// https://redis.io/commands/lrange + /// + /// + /// + /// + /// + /// + Task> LRangeAsync(string cacheKey, long start, long stop); + /// + /// https://redis.io/commands/lrem + /// + /// + /// + /// + /// + /// + Task LRemAsync(string cacheKey, long count, T cacheValue); + /// + /// https://redis.io/commands/lset + /// + /// + /// + /// + /// + /// + Task LSetAsync(string cacheKey, long index, T cacheValue); + /// + /// https://redis.io/commands/ltrim + /// + /// + /// + /// + /// + Task LTrimAsync(string cacheKey, long start, long stop); + /// + /// https://redis.io/commands/lpushx + /// + /// + /// + /// + /// + Task LPushXAsync(string cacheKey, T cacheValue); + /// + /// https://redis.io/commands/linsert + /// + /// + /// + /// + /// + /// + Task LInsertBeforeAsync(string cacheKey, T pivot, T cacheValue); + /// + /// https://redis.io/commands/linsert + /// + /// + /// + /// + /// + /// + Task LInsertAfterAsync(string cacheKey, T pivot, T cacheValue); + /// + /// https://redis.io/commands/rpushx + /// + /// + /// + /// + /// + Task RPushXAsync(string cacheKey, T cacheValue); + /// + /// https://redis.io/commands/rpush + /// + /// + /// + /// + /// + Task RPushAsync(string cacheKey, IList cacheValues); + /// + /// https://redis.io/commands/rpop + /// + /// + /// + /// + Task RPopAsync(string cacheKey); + #endregion + + #region Set + /// + /// https://redis.io/commands/sadd + /// + /// + /// + /// + /// + /// + long SAdd(string cacheKey, IList cacheValues, TimeSpan? expiration = null); + /// + /// https://redis.io/commands/scard + /// + /// + /// + long SCard(string cacheKey); + /// + /// https://redis.io/commands/sismember + /// + /// + /// + /// + /// + bool SIsMember(string cacheKey, T cacheValue); + /// + /// https://redis.io/commands/smembers + /// + /// + /// + /// + List SMembers(string cacheKey); + /// + /// https://redis.io/commands/spop + /// + /// + /// + /// + T SPop(string cacheKey); + /// + /// https://redis.io/commands/srandmember + /// + /// + /// + /// + /// + List SRandMember(string cacheKey, int count = 1); + /// + /// https://redis.io/commands/srem + /// + /// + /// + /// + /// + long SRem(string cacheKey, IList cacheValues = null); + /// + /// https://redis.io/commands/sadd + /// + /// + /// + /// + /// + /// + Task SAddAsync(string cacheKey, IList cacheValues, TimeSpan? expiration = null); + /// + /// https://redis.io/commands/scard + /// + /// + /// + Task SCardAsync(string cacheKey); + /// + /// https://redis.io/commands/sismember + /// + /// + /// + /// + /// + Task SIsMemberAsync(string cacheKey, T cacheValue); + /// + /// https://redis.io/commands/smembers + /// + /// + /// + /// + Task> SMembersAsync(string cacheKey); + /// + /// https://redis.io/commands/spop + /// + /// + /// + /// + Task SPopAsync(string cacheKey); + /// + /// https://redis.io/commands/srandmember + /// + /// + /// + /// + /// + Task> SRandMemberAsync(string cacheKey, int count = 1); + /// + /// https://redis.io/commands/srem + /// + /// + /// + /// + /// + Task SRemAsync(string cacheKey, IList cacheValues = null); + #endregion + + #region Sorted Set + /// + /// https://redis.io/commands/zadd + /// + /// + /// + /// + /// + long ZAdd(string cacheKey, Dictionary cacheValues); + /// + /// https://redis.io/commands/zcard + /// + /// + /// + long ZCard(string cacheKey); + /// + /// https://redis.io/commands/zcount + /// + /// + /// + /// + /// long ZCount(string cacheKey, double min, double max); - /// - /// https://redis.io/commands/zincrby - /// - /// - /// - /// - /// - double ZIncrBy(string cacheKey, string field, double val = 1); - /// - /// https://redis.io/commands/zlexcount - /// - /// - /// - /// - /// - long ZLexCount(string cacheKey, string min, string max); - /// - /// https://redis.io/commands/zrange - /// - /// - /// - /// - /// - /// - List ZRange(string cacheKey, long start, long stop); - /// - /// https://redis.io/commands/zrank - /// - /// - /// - /// - /// - long? ZRank(string cacheKey, T cacheValue); - /// - /// https://redis.io/commands/zrem - /// - /// - /// - /// - /// - long ZRem(string cacheKey, IList cacheValues); - /// - /// https://redis.io/commands/zscore - /// - /// - /// - /// - /// - double? ZScore(string cacheKey, T cacheValue); - /// - /// https://redis.io/commands/zadd - /// - /// - /// - /// - /// - Task ZAddAsync(string cacheKey, Dictionary cacheValues); - /// - /// https://redis.io/commands/zcard - /// - /// - /// - Task ZCardAsync(string cacheKey); - /// - /// https://redis.io/commands/zcount - /// - /// - /// - /// - /// + /// + /// https://redis.io/commands/zincrby + /// + /// + /// + /// + /// + double ZIncrBy(string cacheKey, string field, double val = 1); + /// + /// https://redis.io/commands/zlexcount + /// + /// + /// + /// + /// + long ZLexCount(string cacheKey, string min, string max); + /// + /// https://redis.io/commands/zrange + /// + /// + /// + /// + /// + /// + List ZRange(string cacheKey, long start, long stop); + /// + /// https://redis.io/commands/zrangebyscore/ + /// + /// + /// + /// + /// + /// + /// + /// + List ZRangeByScore(string cacheKey, double min, double max, long? count = null, long offset = 0); + /// + /// https://redis.io/commands/zrank + /// + /// + /// + /// + /// + long? ZRank(string cacheKey, T cacheValue); + /// + /// https://redis.io/commands/zrem + /// + /// + /// + /// + /// + long ZRem(string cacheKey, IList cacheValues); + /// + /// https://redis.io/commands/zscore + /// + /// + /// + /// + /// + double? ZScore(string cacheKey, T cacheValue); + /// + /// https://redis.io/commands/zadd + /// + /// + /// + /// + /// + Task ZAddAsync(string cacheKey, Dictionary cacheValues); + /// + /// https://redis.io/commands/zcard + /// + /// + /// + Task ZCardAsync(string cacheKey); + /// + /// https://redis.io/commands/zcount + /// + /// + /// + /// + /// Task ZCountAsync(string cacheKey, double min, double max); - /// - /// https://redis.io/commands/zincrby - /// - /// - /// - /// - /// - Task ZIncrByAsync(string cacheKey, string field, double val = 1); - /// - /// https://redis.io/commands/zlexcount - /// - /// - /// - /// - /// - Task ZLexCountAsync(string cacheKey, string min, string max); - /// - /// https://redis.io/commands/zrange - /// - /// - /// - /// - /// - /// - Task> ZRangeAsync(string cacheKey, long start, long stop); - /// - /// https://redis.io/commands/zrank - /// - /// - /// - /// - /// - Task ZRankAsync(string cacheKey, T cacheValue); - /// - /// https://redis.io/commands/zrem - /// - /// - /// - /// - /// - Task ZRemAsync(string cacheKey, IList cacheValues); - /// - /// https://redis.io/commands/zscore - /// - /// - /// - /// - /// + /// + /// https://redis.io/commands/zincrby + /// + /// + /// + /// + /// + Task ZIncrByAsync(string cacheKey, string field, double val = 1); + /// + /// https://redis.io/commands/zlexcount + /// + /// + /// + /// + /// + Task ZLexCountAsync(string cacheKey, string min, string max); + /// + /// https://redis.io/commands/zrange + /// + /// + /// + /// + /// + /// + Task> ZRangeAsync(string cacheKey, long start, long stop); + /// + /// https://redis.io/commands/zrangebyscore/ + /// + /// + /// + /// + /// + /// + /// + /// + Task> ZRangeByScoreAsync(string cacheKey, double min, double max, long? count = null, long offset = 0); + /// + /// https://redis.io/commands/zrank + /// + /// + /// + /// + /// + Task ZRankAsync(string cacheKey, T cacheValue); + /// + /// https://redis.io/commands/zrem + /// + /// + /// + /// + /// + Task ZRemAsync(string cacheKey, IList cacheValues); + /// + /// https://redis.io/commands/zscore + /// + /// + /// + /// + /// Task ZScoreAsync(string cacheKey, T cacheValue); - #endregion + #endregion #region Hyperloglog /// @@ -861,7 +891,7 @@ public interface IRedisCachingProvider /// /// Task PfMergeAsync(string destKey, List sourceKeys); - #endregion + #endregion #region Geo /// @@ -924,10 +954,10 @@ public interface IRedisCachingProvider /// /// Task> GeoPosAsync(string cacheKey, List members); - #endregion - - object Eval(string script, string cacheKey, List args); - - Task EvalAsync(string script, string cacheKey, List args); - } -} + #endregion + + object Eval(string script, string cacheKey, List args); + + Task EvalAsync(string script, string cacheKey, List args); + } +} diff --git a/src/EasyCaching.Core/Internal/EasyCachingConstValue.cs b/src/EasyCaching.Core/Internal/EasyCachingConstValue.cs index a58b7564..04fb045c 100644 --- a/src/EasyCaching.Core/Internal/EasyCachingConstValue.cs +++ b/src/EasyCaching.Core/Internal/EasyCachingConstValue.cs @@ -103,5 +103,9 @@ public class EasyCachingConstValue /// The LiteDB Bus section. /// public const string LiteDBSection= "easycaching:litedb"; + + public const string NotFoundCliExceptionMessage = "Can not find the matched client instance, client name is {0}"; + + public const string NotFoundSerExceptionMessage = "Can not find the matched serializer instance, serializer name is {0}"; } } diff --git a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs index f8f19409..eeea279d 100644 --- a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs +++ b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs @@ -52,7 +52,9 @@ public DefaultLiteDBCachingProvider( LiteDBOptions options, ILoggerFactory loggerFactory = null) { - this._dbProvider = dbProviders.Single(x => x.DBProviderName.Equals(name)); + this._dbProvider = dbProviders.FirstOrDefault(x => x.DBProviderName.Equals(name)); + if (this._dbProvider == null) throw new EasyCachingNotFoundException(string.Format(EasyCachingConstValue.NotFoundCliExceptionMessage, _name)); + this._options = options; this._logger = loggerFactory?.CreateLogger(); this._litedb = _dbProvider.GetConnection(); diff --git a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs index 9de69f45..600ace27 100644 --- a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs +++ b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs @@ -78,6 +78,7 @@ public override async Task> BaseGetAsync(string cacheKey) else { OnCacheMiss(cacheKey); + CheckResult(result); return CacheValue.NoValue; } } @@ -132,7 +133,8 @@ public override async Task BaseRemoveAsync(string cacheKey) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - await _memcachedClient.RemoveAsync(this.HandleCacheKey(cacheKey)); + var data = await _memcachedClient.ExecuteRemoveAsync(this.HandleCacheKey(cacheKey)); + CheckResult(data); } /// @@ -199,6 +201,7 @@ public override async Task BaseRemoveByPrefixAsync(string prefix) { newValue = string.Concat(newValue, new Random().Next(9).ToString()); } + await _memcachedClient.StoreAsync( Enyim.Caching.Memcached.StoreMode.Set, this.HandleCacheKey(prefix), diff --git a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs index 633f612c..97455267 100644 --- a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs +++ b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs @@ -2,7 +2,7 @@ { using EasyCaching.Core; using EasyCaching.Core.DistributedLock; -using EasyCaching.Memcached.DistributedLock; + using EasyCaching.Memcached.DistributedLock; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -77,7 +77,9 @@ public DefaultMemcachedCachingProvider( : base(factory, options) { this._name = name; - this._memcachedClient = memcachedClients.Single(x => x.Name.Equals(this._name)); + this._memcachedClient = memcachedClients.FirstOrDefault(x => x.Name.Equals(this._name)); + if (this._memcachedClient == null) throw new EasyCachingNotFoundException(string.Format(EasyCachingConstValue.NotFoundCliExceptionMessage, _name)); + this._options = options; this._logger = loggerFactory?.CreateLogger(); this._cacheStats = new CacheStats(); @@ -155,7 +157,11 @@ public override CacheValue BaseGet(string cacheKey) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var result = ConvertFromStoredValue(_memcachedClient.Get(this.HandleCacheKey(cacheKey))); + var data = _memcachedClient.PerformGet(this.HandleCacheKey(cacheKey)); + + CheckResult(data); + + var result = ConvertFromStoredValue(data.Value); if (result.HasValue) { @@ -178,7 +184,9 @@ public override void BaseRemove(string cacheKey) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - _memcachedClient.Remove(this.HandleCacheKey(cacheKey)); + var data = _memcachedClient.ExecuteRemove(this.HandleCacheKey(cacheKey)); + + CheckResult(data); } /// @@ -201,11 +209,13 @@ public override void BaseSet(string cacheKey, T cacheValue, TimeSpan expirati expiration = expiration.Add(TimeSpan.FromSeconds(addSec)); } - _memcachedClient.Store( + var data = _memcachedClient.ExecuteStore( Enyim.Caching.Memcached.StoreMode.Set, this.HandleCacheKey(cacheKey), this.ConvertToStoredValue(cacheValue), expiration); + + CheckResult(data); } /// @@ -244,11 +254,14 @@ public override void BaseRemoveByPrefix(string prefix) { newValue = string.Concat(newValue, new Random().Next(9).ToString()); } - _memcachedClient.Store( - Enyim.Caching.Memcached.StoreMode.Set, - this.HandleCacheKey(prefix), - newValue, - new TimeSpan(0, 0, 0)); + + var data = _memcachedClient.ExecuteStore( + Enyim.Caching.Memcached.StoreMode.Set, + this.HandleCacheKey(prefix), + newValue, + new TimeSpan(0, 0, 0)); + + CheckResult(data); } /// @@ -429,5 +442,13 @@ private void OnCacheMiss(string cacheKey) if (_options.EnableLogging) _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); } + + private void CheckResult(Enyim.Caching.Memcached.Results.IOperationResult data) + { + if (!data.Success + && (!data.InnerResult?.Success ?? false) + && (data.InnerResult?.Message?.Contains("Failed to create socket") ?? false)) + throw new EasyCachingException($"opereation fail, {data.InnerResult?.Message ?? ""}", data.Exception); + } } } \ No newline at end of file diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Keys.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Keys.cs index 0539ab65..5179078c 100755 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Keys.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Keys.cs @@ -132,19 +132,29 @@ public async Task EvalAsync(string script, string cacheKey, List public List SearchKeys(string cacheKey, int? count) { - var keys = new List(); + var data = new List(); + var server = _servers.ToArray()[0]; + var keys = server.Keys(_cache.Database, pattern: cacheKey, count ?? 250).ToList(); + foreach (var item in keys) + { + data.Add(item.ToString()); + } - keys = server.Keys(_cache.Database, pattern: cacheKey, count.HasValue?250: count.Value).ToList(); + return data; + } + public async Task> SearchKeysAsync(string cacheKey, int? count) + { var data = new List(); - if (keys.Count <= 0) - return data; - - foreach (var item in keys) + + var server = _servers.ToArray()[0]; + var keys = server.KeysAsync(_cache.Database, pattern: cacheKey, count ?? 250); + await foreach (var item in keys) { data.Add(item.ToString()); } + return data; } } diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.SortedSet.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.SortedSet.cs index 24de8810..4196330f 100755 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.SortedSet.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.SortedSet.cs @@ -1,16 +1,16 @@ -namespace EasyCaching.Redis -{ - using System.Collections.Generic; - using System.Threading.Tasks; - using EasyCaching.Core; - using EasyCaching.Core.Internal; - using StackExchange.Redis; - - /// - /// Default redis caching provider. - /// - public partial class DefaultRedisCachingProvider : IEasyCachingProvider - { +namespace EasyCaching.Redis +{ + using System.Collections.Generic; + using System.Threading.Tasks; + using EasyCaching.Core; + using EasyCaching.Core.Internal; + using StackExchange.Redis; + + /// + /// Default redis caching provider. + /// + public partial class DefaultRedisCachingProvider : IEasyCachingProvider + { public long ZAdd(string cacheKey, Dictionary cacheValues) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -29,50 +29,66 @@ public long ZAdd(string cacheKey, Dictionary cacheValues) public long ZCard(string cacheKey) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = _cache.SortedSetLength(cacheKey); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = _cache.SortedSetLength(cacheKey); return len; } public long ZCount(string cacheKey, double min, double max) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = _cache.SortedSetLengthByValue(cacheKey, min, max); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = _cache.SortedSetLengthByValue(cacheKey, min, max); return len; } public double ZIncrBy(string cacheKey, string field, double val = 1) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field)); - + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field)); + var value = _cache.SortedSetIncrement(cacheKey, field, val); return value; } public long ZLexCount(string cacheKey, string min, string max) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = _cache.SortedSetLengthByValue(cacheKey, min, max); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = _cache.SortedSetLengthByValue(cacheKey, min, max); return len; } public List ZRange(string cacheKey, long start, long stop) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var list = new List(); - - var bytes = _cache.SortedSetRangeByRank(cacheKey, start, stop); - + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var list = new List(); + + var bytes = _cache.SortedSetRangeByRank(cacheKey, start, stop); + foreach (var item in bytes) { list.Add(_serializer.Deserialize(item)); - } - + } + + return list; + } + + public List ZRangeByScore(string cacheKey, double min, double max, long? count = null, long offset = 0) + { + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var list = new List(); + + var bytes = _cache.SortedSetRangeByScore(cacheKey, min, max, skip: offset, take: count ?? -1); + + foreach (var item in bytes) + { + list.Add(_serializer.Deserialize(item)); + } + return list; } @@ -80,8 +96,8 @@ public List ZRange(string cacheKey, long start, long stop) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var bytes = _serializer.Serialize(cacheValue); - + var bytes = _serializer.Serialize(cacheValue); + var index = _cache.SortedSetRank(cacheKey, bytes); return index; @@ -92,27 +108,27 @@ public long ZRem(string cacheKey, IList cacheValues) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var bytes = new List(); - + var bytes = new List(); + foreach (var item in cacheValues) { bytes.Add(_serializer.Serialize(item)); - } - + } + var len = _cache.SortedSetRemove(cacheKey, bytes.ToArray()); return len; - } - - public double? ZScore(string cacheKey, T cacheValue) - { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var bytes = _serializer.Serialize(cacheValue); - - var score = _cache.SortedSetScore(cacheKey, bytes); - - return score; + } + + public double? ZScore(string cacheKey, T cacheValue) + { + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var bytes = _serializer.Serialize(cacheValue); + + var score = _cache.SortedSetScore(cacheKey, bytes); + + return score; } public async Task ZAddAsync(string cacheKey, Dictionary cacheValues) @@ -134,50 +150,66 @@ public async Task ZAddAsync(string cacheKey, Dictionary cach public async Task ZCardAsync(string cacheKey) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = await _cache.SortedSetLengthAsync(cacheKey); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = await _cache.SortedSetLengthAsync(cacheKey); return len; - } - + } + public async Task ZCountAsync(string cacheKey, double min, double max) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = await _cache.SortedSetLengthByValueAsync(cacheKey, min, max); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = await _cache.SortedSetLengthByValueAsync(cacheKey, min, max); return len; } public async Task ZIncrByAsync(string cacheKey, string field, double val = 1) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field)); - + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + ArgumentCheck.NotNullOrWhiteSpace(field, nameof(field)); + var value = await _cache.SortedSetIncrementAsync(cacheKey, field, val); return value; - } - + } + public async Task ZLexCountAsync(string cacheKey, string min, string max) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var len = await _cache.SortedSetLengthByValueAsync(cacheKey, min, max); + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var len = await _cache.SortedSetLengthByValueAsync(cacheKey, min, max); return len; } public async Task> ZRangeAsync(string cacheKey, long start, long stop) { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var list = new List(); - - var bytes = await _cache.SortedSetRangeByRankAsync(cacheKey, start, stop); - + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var list = new List(); + + var bytes = await _cache.SortedSetRangeByRankAsync(cacheKey, start, stop); + + foreach (var item in bytes) + { + list.Add(_serializer.Deserialize(item)); + } + + return list; + } + + public async Task> ZRangeByScoreAsync(string cacheKey, double min, double max, long? count = null, long offset = 0) + { + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var list = new List(); + + var bytes = await _cache.SortedSetRangeByScoreAsync(cacheKey, min, max, skip: offset, take: count ?? -1); + foreach (var item in bytes) { list.Add(_serializer.Deserialize(item)); - } - + } + return list; } @@ -185,8 +217,8 @@ public async Task> ZRangeAsync(string cacheKey, long start, long stop { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var bytes = _serializer.Serialize(cacheValue); - + var bytes = _serializer.Serialize(cacheValue); + var index = await _cache.SortedSetRankAsync(cacheKey, bytes); return index; @@ -196,27 +228,27 @@ public async Task ZRemAsync(string cacheKey, IList cacheValues) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var bytes = new List(); - + var bytes = new List(); + foreach (var item in cacheValues) { bytes.Add(_serializer.Serialize(item)); - } - + } + var len = await _cache.SortedSetRemoveAsync(cacheKey, bytes.ToArray()); return len; - } - - public async Task ZScoreAsync(string cacheKey, T cacheValue) - { - ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - - var bytes = _serializer.Serialize(cacheValue); - - var score = await _cache.SortedSetScoreAsync(cacheKey, bytes); - - return score; - } - } -} + } + + public async Task ZScoreAsync(string cacheKey, T cacheValue) + { + ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); + + var bytes = _serializer.Serialize(cacheValue); + + var score = await _cache.SortedSetScoreAsync(cacheKey, bytes); + + return score; + } + } +} diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs index 215d39e6..fcf9795c 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs @@ -98,16 +98,19 @@ public DefaultRedisCachingProvider( ArgumentCheck.NotNullAndCountGTZero(serializers, nameof(serializers)); this._name = name; - this._dbProvider = dbProviders.Single(x => x.DBProviderName.Equals(name)); + this._dbProvider = dbProviders.FirstOrDefault(x => x.DBProviderName.Equals(name)); + if (this._dbProvider == null) throw new EasyCachingNotFoundException(string.Format(EasyCachingConstValue.NotFoundCliExceptionMessage, _name)); + this._options = options; this._logger = loggerFactory?.CreateLogger(); this._cache = _dbProvider.GetDatabase(); this._servers = _dbProvider.GetServerList(); this._cacheStats = new CacheStats(); - this._serializer = !string.IsNullOrWhiteSpace(options.SerializerName) - ? serializers.Single(x => x.Name.Equals(options.SerializerName)) - : serializers.FirstOrDefault(x => x.Name.Equals(_name)) ?? serializers.Single(x => x.Name.Equals(EasyCachingConstValue.DefaultSerializerName)); + var serName = !string.IsNullOrWhiteSpace(options.SerializerName) ? options.SerializerName : _name; + + this._serializer = serializers.FirstOrDefault(x => x.Name.Equals(serName)); + if (this._serializer == null) throw new EasyCachingNotFoundException(string.Format(EasyCachingConstValue.NotFoundSerExceptionMessage, serName)); this.ProviderName = this._name; this.ProviderType = CachingProviderType.Redis; diff --git a/src/EasyCaching.Redis/EasyCaching.Redis.csproj b/src/EasyCaching.Redis/EasyCaching.Redis.csproj index 292d88e9..3c526a83 100644 --- a/src/EasyCaching.Redis/EasyCaching.Redis.csproj +++ b/src/EasyCaching.Redis/EasyCaching.Redis.csproj @@ -16,6 +16,7 @@ https://github.com/dotnetcore/EasyCaching https://github.com/dotnetcore/EasyCaching nuget-icon.png + latest $(EasyCachingRedisPackageNotes) @@ -32,7 +33,7 @@ - + diff --git a/test/EasyCaching.UnitTests/CachingTests/BaseRedisFeatureCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/BaseRedisFeatureCachingProviderTest.cs index 90b7d039..f6de3a9d 100644 --- a/test/EasyCaching.UnitTests/CachingTests/BaseRedisFeatureCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/BaseRedisFeatureCachingProviderTest.cs @@ -1,1813 +1,1865 @@ -namespace EasyCaching.UnitTests -{ - using EasyCaching.Core; - using System; +namespace EasyCaching.UnitTests +{ + using EasyCaching.Core; + using System; using System.Collections.Generic; using System.Linq; - using System.Threading.Tasks; - using Xunit; - - public abstract class BaseRedisFeatureCachingProviderTest - { - protected IRedisCachingProvider _provider; - protected IEasyCachingProvider _baseProvider; - protected string _nameSpace = string.Empty; - - #region Keys - [Fact] - protected virtual void KeyDel_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.StringSet(cacheKey, "123"); - - Assert.True(res); - - var flag = _provider.KeyDel(cacheKey); - - Assert.True(flag); - } - - [Fact] - protected virtual async Task KeyDelAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.StringSet(cacheKey, "123"); - - Assert.True(res); - - var flag = await _provider.KeyDelAsync(cacheKey); - - Assert.True(flag); - } - - [Fact] - protected virtual void StringSet_And_KeyExpire_And_TTL_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.StringSet(cacheKey, "123"); - - Assert.True(res); - - var flag = _provider.KeyExpire(cacheKey, 10); - - Assert.True(flag); - - var ttl = _provider.TTL(cacheKey); - - Assert.InRange(ttl, 1, 10); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSet_And_KeyExpire_And_TTL_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.StringSetAsync(cacheKey, "123"); - - Assert.True(res); - - var flag = await _provider.KeyExpireAsync(cacheKey, 10); - - Assert.True(flag); - - var ttl = await _provider.TTLAsync(cacheKey); - - Assert.InRange(ttl, 1, 10); - - await _provider.KeyDelAsync(cacheKey); - } - #endregion - - #region String - - [Fact] - protected virtual void StringSet_With_Nx_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = _provider.StringSet(cacheKey, cacheValue, when: "nx"); - Assert.True(res); - - var res2 = _provider.StringSet(cacheKey, cacheValue, when: "nx"); + using System.Threading.Tasks; + using Xunit; + + public abstract class BaseRedisFeatureCachingProviderTest + { + protected IRedisCachingProvider _provider; + protected IEasyCachingProvider _baseProvider; + protected string _nameSpace = string.Empty; + + #region Keys + [Fact] + protected virtual void KeyDel_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.StringSet(cacheKey, "123"); + + Assert.True(res); + + var flag = _provider.KeyDel(cacheKey); + + Assert.True(flag); + } + + [Fact] + protected virtual async Task KeyDelAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.StringSet(cacheKey, "123"); + + Assert.True(res); + + var flag = await _provider.KeyDelAsync(cacheKey); + + Assert.True(flag); + } + + [Fact] + protected virtual void StringSet_And_KeyExpire_And_TTL_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.StringSet(cacheKey, "123"); + + Assert.True(res); + + var flag = _provider.KeyExpire(cacheKey, 10); + + Assert.True(flag); + + var ttl = _provider.TTL(cacheKey); + + Assert.InRange(ttl, 1, 10); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task StringSet_And_KeyExpire_And_TTL_Async_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.StringSetAsync(cacheKey, "123"); + + Assert.True(res); + + var flag = await _provider.KeyExpireAsync(cacheKey, 10); + + Assert.True(flag); + + var ttl = await _provider.TTLAsync(cacheKey); + + Assert.InRange(ttl, 1, 10); + + await _provider.KeyDelAsync(cacheKey); + } + #endregion + + #region String + + [Fact] + protected virtual void StringSet_With_Nx_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = Guid.NewGuid().ToString(); + + var res = _provider.StringSet(cacheKey, cacheValue, when: "nx"); + Assert.True(res); + + var res2 = _provider.StringSet(cacheKey, cacheValue, when: "nx"); + Assert.False(res2); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task StringSetAsync_With_Nx_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = Guid.NewGuid().ToString(); + + var res = await _provider.StringSetAsync(cacheKey, cacheValue, when: "nx"); + Assert.True(res); + + var res2 = await _provider.StringSetAsync(cacheKey, cacheValue, when: "nx"); Assert.False(res2); - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSetAsync_With_Nx_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = await _provider.StringSetAsync(cacheKey, cacheValue, when: "nx"); - Assert.True(res); - - var res2 = await _provider.StringSetAsync(cacheKey, cacheValue, when: "nx"); - Assert.False(res2); + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual void StringSet_With_Xx_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = Guid.NewGuid().ToString(); + + var res = _provider.StringSet(cacheKey, cacheValue, when: "xx"); + Assert.False(res); + + var res2 = _provider.StringSet(cacheKey, cacheValue); + Assert.True(res2); + var res3 = _provider.StringSet(cacheKey, cacheValue, when: "xx"); + Assert.True(res3); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task StringSetAsync_With_Xx_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = Guid.NewGuid().ToString(); + + var res = await _provider.StringSetAsync(cacheKey, cacheValue, when: "xx"); + Assert.False(res); + + var res2 = await _provider.StringSetAsync(cacheKey, cacheValue); + Assert.True(res2); + var res3 = await _provider.StringSetAsync(cacheKey, cacheValue, when: "xx"); + Assert.True(res3); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual void StringSet_And_StringGet_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = Guid.NewGuid().ToString(); + + var res = _provider.StringSet(cacheKey, cacheValue); + + Assert.True(res); + + var val = _provider.StringGet(cacheKey); + + Assert.Equal(cacheValue, val); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task StringSet_And_StringGet_Async_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = Guid.NewGuid().ToString(); + + var res = await _provider.StringSetAsync(cacheKey, cacheValue); + + Assert.True(res); + + var val = await _provider.StringGetAsync(cacheKey); + + Assert.Equal(cacheValue, val); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual void IncrBy_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.IncrBy(cacheKey); + + Assert.Equal(1, res); + + var val = _provider.IncrBy(cacheKey, 3); + + Assert.Equal(4, val); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task IncrByAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.IncrByAsync(cacheKey); + + Assert.Equal(1, res); + + var val = await _provider.IncrByAsync(cacheKey, 3); + + Assert.Equal(4, val); + + await _provider.KeyDelAsync(cacheKey); + } + + [Fact] + protected virtual void IncrByFloat_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.IncrByFloat(cacheKey); + + Assert.Equal(1d, res); + + var val = _provider.IncrByFloat(cacheKey, 3d); + + Assert.Equal(4d, val); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task IncrByFloatAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.IncrByFloatAsync(cacheKey); + + Assert.Equal(1d, res); + + var val = await _provider.IncrByFloatAsync(cacheKey, 3d); + + Assert.Equal(4d, val); + + await _provider.KeyDelAsync(cacheKey); + } + + [Fact] + protected virtual void StringLen_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = "catcherwong"; + + var flag = _provider.StringSet(cacheKey, cacheValue); + + Assert.True(flag); + + var len = _provider.StringLen(cacheKey); + + Assert.Equal(11, len); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task StringLenAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = "catcherwong"; + + var flag = await _provider.StringSetAsync(cacheKey, cacheValue); + + Assert.True(flag); + + var len = await _provider.StringLenAsync(cacheKey); + + Assert.Equal(11, len); + + await _provider.KeyDelAsync(cacheKey); + } + + [Fact] + protected virtual void StringSetRange_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = "Hello World"; + + var flag = _provider.StringSet(cacheKey, cacheValue); + + Assert.True(flag); + + var len = _provider.StringSetRange(cacheKey, 6, "Redis"); + + Assert.Equal(11, len); + + var val = _provider.StringGet(cacheKey); + + Assert.Equal("Hello Redis", val); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task StringSetRangeAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = "Hello World"; + + var flag = await _provider.StringSetAsync(cacheKey, cacheValue); + + Assert.True(flag); + + var len = await _provider.StringSetRangeAsync(cacheKey, 6, "Redis"); + + Assert.Equal(11, len); + + var val = await _provider.StringGetAsync(cacheKey); + + Assert.Equal("Hello Redis", val); + + await _provider.KeyDelAsync(cacheKey); + } + + [Fact] + protected virtual void StringGetRange_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = "This is a string"; + + var flag = _provider.StringSet(cacheKey, cacheValue); + + Assert.True(flag); + + var res1 = _provider.StringGetRange(cacheKey, 0, 3); + + Assert.Equal("This", res1); + + var res2 = _provider.StringGetRange(cacheKey, -3, -1); + + Assert.Equal("ing", res2); + + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task StringGetRangeAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + var cacheValue = "This is a string"; + + var flag = await _provider.StringSetAsync(cacheKey, cacheValue); + + Assert.True(flag); + + var res1 = await _provider.StringGetRangeAsync(cacheKey, 0, 3); + + Assert.Equal("This", res1); + + var res2 = await _provider.StringGetRangeAsync(cacheKey, -3, -1); + + Assert.Equal("ing", res2); + + await _provider.KeyDelAsync(cacheKey); + } + + + #endregion + + #region Hash + [Fact] + protected virtual void HMSet_And_HMGet_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var dict = _provider.HMGet(cacheKey, new List { "a1", "a2" }); + + Assert.Contains("v1", dict.Values); + Assert.Contains("v2", dict.Values); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HMSetAsync_And_HMGetAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var dict = await _provider.HMGetAsync(cacheKey, new List { "a1", "a2" }); + + Assert.Contains("v1", dict.Values); + Assert.Contains("v2", dict.Values); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HKeys_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"k1","v1"},{"k2","v2"} + }); + + var keys = _provider.HKeys(cacheKey); + + Assert.Contains("k1", keys); + Assert.Contains("k2", keys); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HKeysAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"k1","v1"},{"k2","v2"} + }); + + var keys = await _provider.HKeysAsync(cacheKey); + + Assert.Contains("k1", keys); + Assert.Contains("k2", keys); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HVals_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"k1","v1"},{"k2","v2"} + }); + + var vals = _provider.HVals(cacheKey); + + Assert.Contains("v1", vals); + Assert.Contains("v2", vals); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HValsAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"k1","v1"},{"k2","v2"} + }); + + var vals = await _provider.HValsAsync(cacheKey); + + Assert.Contains("v1", vals); + Assert.Contains("v2", vals); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HGetAll_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + var all = _provider.HGetAll(cacheKey); + + Assert.Equal(2, all.Count); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HGetAllAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + var all = await _provider.HGetAllAsync(cacheKey); + + Assert.Equal(2, all.Count); + + await _provider.HDelAsync(cacheKey); + } + + + [Fact] + protected virtual void HDel_With_Fields_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"},{"a3","v3"} + }); + + var del = _provider.HDel(cacheKey, new List { "a1", "a3" }); + + Assert.Equal(2, del); + + var len = _provider.HLen(cacheKey); + + Assert.Equal(1, len); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HDelAsync_With_Fields_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"},{"a3","v3"} + }); + + var del = await _provider.HDelAsync(cacheKey, new List { "a1", "a3" }); + + Assert.Equal(2, del); + + var len = await _provider.HLenAsync(cacheKey); + + Assert.Equal(1, len); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HDel_WithOut_Fields_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"},{"a3","v3"} + }); + + var del = _provider.HDel(cacheKey); + + Assert.Equal(1, del); + + var len = _provider.HLen(cacheKey); + + Assert.Equal(0, len); + } + + [Fact] + protected virtual async Task HDelAsync_WithOut_Fields_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"},{"a3","v3"} + }); + + var del = await _provider.HDelAsync(cacheKey); + + Assert.Equal(1, del); + + var len = await _provider.HLenAsync(cacheKey); + + Assert.Equal(0, len); + } + + [Fact] + protected virtual void HIncrBy_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; + + var res = _provider.HIncrBy(cacheKey, "a1"); + + Assert.Equal(1, res); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HIncrByAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HIncrByAsync(cacheKey, "a1"); + + Assert.Equal(1, res); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HIncrBy_With_Val_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; + + var res = _provider.HIncrBy(cacheKey, "a1", 3); + + Assert.Equal(3, res); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HIncrByAsync_With_Val_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HIncrByAsync(cacheKey, "a1", 3); + + Assert.Equal(3, res); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HExists_With_Other_CacheKey_Should_Return_False() + { + var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; + + var flag = _provider.HExists(cacheKey, "field"); + + Assert.False(flag); + } + + [Fact] + protected virtual async Task HExistsAsync_With_Other_CacheKey_Should_Return_False() + { + var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; + + var flag = await _provider.HExistsAsync(cacheKey, "field"); + + Assert.False(flag); + } + + + [Fact] + protected virtual void HExists_With_Other_Field_Should_Return_False() + { + var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; + + var res = _provider.HSet(cacheKey, "a1", "v1"); + + Assert.True(res); + + var flag = _provider.HExists(cacheKey, "field"); + + Assert.False(flag); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HExistsAsync_With_Other_Field_Should_Return_False() + { + var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HSetAsync(cacheKey, "a1", "v1"); + + Assert.True(res); + + var flag = await _provider.HExistsAsync(cacheKey, "field"); + + Assert.False(flag); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HExists_With_Field_Should_Return_True() + { + var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; + + var res = _provider.HSet(cacheKey, "a1", "v1"); + Assert.True(res); + + var flag = _provider.HExists(cacheKey, "a1"); + Assert.True(flag); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HExistsAsync_With_Field_Should_Return_True() + { + var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HSetAsync(cacheKey, "a1", "v1"); + Assert.True(res); + + var flag = await _provider.HExistsAsync(cacheKey, "a1"); + Assert.True(flag); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HSet_With_Field_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var flag = _provider.HSet(cacheKey, "a3", "v3"); + + Assert.True(flag); + + var len = _provider.HLen(cacheKey); + + Assert.Equal(3, len); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HSetAsync_With_Field_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var flag = await _provider.HSetAsync(cacheKey, "a3", "v3"); + + Assert.True(flag); + + var len = await _provider.HLenAsync(cacheKey); + + Assert.Equal(3, len); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HGet_With_Field_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var val = _provider.HGet(cacheKey, "a1"); + + Assert.NotNull(val); + Assert.Equal("v1", val); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HGetAsync_With_Field_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var val = await _provider.HGetAsync(cacheKey, "a1"); + + Assert.NotNull(val); + Assert.Equal("v1", val); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HGet_With_Other_Field_Should_Return_Null() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var val = _provider.HGet(cacheKey, "a3"); + + Assert.Null(val); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HGetAsync_With_Other_Field_Should_Return_Null() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var val = await _provider.HGetAsync(cacheKey, "a3"); + + Assert.Null(val); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HLen_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var len = _provider.HLen(cacheKey); + + Assert.Equal(2, len); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HLenAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }); + + Assert.True(res); + + var len = await _provider.HLenAsync(cacheKey); + + Assert.Equal(2, len); + + await _provider.HDelAsync(cacheKey); + } + + [Fact] + protected virtual void HMSet_With_Expiration_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.HMSet(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }, TimeSpan.FromSeconds(1)); + + Assert.True(res); + + System.Threading.Thread.Sleep(1050); + + var val = _provider.HGet(cacheKey, "a1"); + Assert.Null(val); + + _provider.HDel(cacheKey); + } + + [Fact] + protected virtual async Task HMSetAsync_With_Expiration_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.HMSetAsync(cacheKey, new Dictionary + { + {"a1","v1"},{"a2","v2"} + }, TimeSpan.FromSeconds(1)); + + Assert.True(res); + + await Task.Delay(1050); + + var val = await _provider.HGetAsync(cacheKey, "a1"); + Assert.Null(val); + + await _provider.HDelAsync(cacheKey); + } + #endregion + + #region List + [Fact] + protected virtual void LPush_And_LPop_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = _provider.LPop(cacheKey); + Assert.Equal("p2", val); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task LPushAsync_And_LPopAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = await _provider.LPopAsync(cacheKey); + Assert.Equal("p2", val); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void LPushX_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = _provider.LPushX(cacheKey, "p4"); + Assert.Equal(3, val); + + var pop = _provider.LPop(cacheKey); + Assert.Equal("p4", pop); + + _baseProvider.Remove(cacheKey); + } + + + [Fact] + protected virtual async Task LPushXAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = await _provider.LPushXAsync(cacheKey, "p4"); + Assert.Equal(3, val); + + var pop = await _provider.LPopAsync(cacheKey); + Assert.Equal("p4", pop); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void LPushX_Not_Exist_CacheKey_Should_Return_Zero() + { + var cacheKey = $"{_nameSpace}-nex-{Guid.NewGuid().ToString()}"; + + var val = _provider.LPushX(cacheKey, "p4"); + + Assert.Equal(0, val); + } + + [Fact] + protected virtual async Task LPushXAsync_Not_Exist_CacheKey_Should_Return_Zero() + { + var cacheKey = $"{_nameSpace}-nex-{Guid.NewGuid().ToString()}"; + + var val = await _provider.LPushXAsync(cacheKey, "p4"); + + Assert.Equal(0, val); + } + + [Fact] + protected virtual void LSet_And_LRange_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1" }); + Assert.Equal(1, res); + + var val = _provider.LSet(cacheKey, 0, "p3"); + Assert.True(val); + + var list = _provider.LRange(cacheKey, 0, -1); + + Assert.Single(list); + Assert.Equal("p3", list[0]); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task LSetAsync_And_LRangeAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1" }); + Assert.Equal(1, res); + + var val = await _provider.LSetAsync(cacheKey, 0, "p3"); + Assert.True(val); + + var list = await _provider.LRangeAsync(cacheKey, 0, -1); + + Assert.Single(list); + Assert.Equal("p3", list[0]); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void LIndex_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); + + Assert.Equal(2, res); + + var val0 = _provider.LIndex(cacheKey, 0); + var val1 = _provider.LIndex(cacheKey, 1); + + Assert.Equal("p2", val0); + Assert.Equal("p1", val1); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task LIndexAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); + + Assert.Equal(2, res); + + var val0 = await _provider.LIndexAsync(cacheKey, 0); + var val1 = await _provider.LIndexAsync(cacheKey, 1); + + Assert.Equal("p2", val0); + Assert.Equal("p1", val1); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void LRem_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1", "p2", "p1", "p2", "p1" }); + Assert.Equal(5, res); + + _provider.LRem(cacheKey, 0, "p1"); + + var len1 = _provider.LLen(cacheKey); + Assert.Equal(2, len1); + + _provider.LRem(cacheKey, 1, "p2"); + + var len2 = _provider.LLen(cacheKey); + Assert.Equal(1, len2); + + _baseProvider.Remove(cacheKey); + } + + + [Fact] + protected virtual async Task LRemAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2", "p1", "p2", "p1" }); + Assert.Equal(5, res); + + await _provider.LRemAsync(cacheKey, 0, "p1"); + + var len1 = await _provider.LLenAsync(cacheKey); + Assert.Equal(2, len1); + + await _provider.LRemAsync(cacheKey, 1, "p2"); + + var len2 = await _provider.LLenAsync(cacheKey); + Assert.Equal(1, len2); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void LTrim_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1", "p2", "p3" }); + Assert.Equal(3, res); + + var flag = _provider.LTrim(cacheKey, 2, -1); + Assert.True(flag); + + var vals = _provider.LRange(cacheKey, 0, -1); + Assert.Single(vals); + Assert.Equal("p1", vals[0]); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task LTrimAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2", "p3" }); + Assert.Equal(3, res); + + var flag = await _provider.LTrimAsync(cacheKey, 2, -1); + Assert.True(flag); + + var vals = await _provider.LRangeAsync(cacheKey, 0, -1); + Assert.Single(vals); + Assert.Equal("p1", vals[0]); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void LLen_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var len = _provider.LLen(cacheKey); + Assert.Equal(2, len); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task LLenAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var len = await _provider.LLenAsync(cacheKey); + Assert.Equal(2, len); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void RPush_And_RPop_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.RPush(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = _provider.RPop(cacheKey); + Assert.Equal("p2", val); + + _baseProvider.Remove(cacheKey); + } + - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual void StringSet_With_Xx_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = _provider.StringSet(cacheKey, cacheValue, when: "xx"); - Assert.False(res); - - var res2 = _provider.StringSet(cacheKey, cacheValue); - Assert.True(res2); - var res3 = _provider.StringSet(cacheKey, cacheValue, when: "xx"); - Assert.True(res3); + [Fact] + protected virtual async Task RPushAsync_And_RPopAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSetAsync_With_Xx_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); + var res = await _provider.RPushAsync(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); - var res = await _provider.StringSetAsync(cacheKey, cacheValue, when: "xx"); - Assert.False(res); - - var res2 = await _provider.StringSetAsync(cacheKey, cacheValue); - Assert.True(res2); - var res3 = await _provider.StringSetAsync(cacheKey, cacheValue, when: "xx"); - Assert.True(res3); + var val = await _provider.RPopAsync(cacheKey); + Assert.Equal("p2", val); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void RPushX_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = _provider.RPushX(cacheKey, "p4"); + Assert.Equal(3, val); + + var pop = _provider.RPop(cacheKey); + Assert.Equal("p4", pop); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task RPushXAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = await _provider.RPushXAsync(cacheKey, "p4"); + Assert.Equal(3, val); + + var pop = await _provider.RPopAsync(cacheKey); + Assert.Equal("p4", pop); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void LInsertBefore_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = _provider.LInsertBefore(cacheKey, "p1", "p4"); + Assert.Equal(3, val); + + var list = _provider.LRange(cacheKey, 0, -1); + Assert.Equal("p4", list[1]); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task LInsertBeforeAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = await _provider.LInsertBeforeAsync(cacheKey, "p1", "p4"); + Assert.Equal(3, val); + + var list = await _provider.LRangeAsync(cacheKey, 0, -1); + Assert.Equal("p4", list[1]); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual void LInsertAfter_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = _provider.LInsertAfter(cacheKey, "p1", "p4"); + Assert.Equal(3, val); + + var list = _provider.LRange(cacheKey, 0, -1); + Assert.Equal("p4", list[2]); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task LInsertAfterAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); + Assert.Equal(2, res); + + var val = await _provider.LInsertAfterAsync(cacheKey, "p1", "p4"); + Assert.Equal(3, val); + + var list = await _provider.LRangeAsync(cacheKey, 0, -1); + Assert.Equal("p4", list[2]); + + await _baseProvider.RemoveAsync(cacheKey); + } + #endregion + + #region Set + [Fact] + protected virtual void SAdd_And_SCard_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); + + Assert.Equal(2, res); + + var len = _provider.SCard(cacheKey); + + Assert.Equal(2, len); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual void SAdd_With_Expiration_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }, TimeSpan.FromSeconds(1)); + + Assert.Equal(2, res); + + System.Threading.Thread.Sleep(1050); + + var flag = _baseProvider.Exists(cacheKey); + + Assert.False(flag); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual void SIsMember_With_Existed_Should_Return_True() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); + + Assert.Equal(2, res); + + var i1 = _provider.SIsMember(cacheKey, "s1"); + var i2 = _provider.SIsMember(cacheKey, "s2"); + + Assert.True(i1); + Assert.True(i2); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual void SIsMember_With_Not_Existed_Should_Return_False() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); + + Assert.Equal(2, res); + + var i1 = _provider.SIsMember(cacheKey, "s3"); + + Assert.False(i1); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual void SMembers_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); + + var vals = _provider.SMembers(cacheKey); + + Assert.Equal(2, vals.Count); + Assert.Contains("s1", vals); + Assert.Contains("s2", vals); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual void SRem_With_Values_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); + + var len = _provider.SRem(cacheKey, new List { "s1" }); + + Assert.Equal(1, len); + + var flag = _provider.SIsMember(cacheKey, "s1"); + Assert.False(flag); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual void SRem_Without_Values_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); + + var len = _provider.SRem(cacheKey); + + Assert.Equal(1, len); + + var flag = _baseProvider.Exists(cacheKey); + Assert.False(flag); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual void SRandMember_With_Not_Exist_Should_Return_EmptyList() + { + var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; + + var len = _provider.SRandMember(cacheKey); + + Assert.Empty(len); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual void SRandMember_With_Exist_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; + + var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); + + var vals = _provider.SRandMember(cacheKey, 2); + + Assert.Equal(2, vals.Count); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task SAddAsync_And_SCardAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); + + Assert.Equal(2, res); + + var len = await _provider.SCardAsync(cacheKey); + + Assert.Equal(2, len); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual async Task SAddAsync_With_Expiration_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }, TimeSpan.FromSeconds(1)); + + Assert.Equal(2, res); + + await Task.Delay(1050); + + var len = await _provider.SCardAsync(cacheKey); + + Assert.Equal(0, len); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async Task SIsMemberAsync_With_Existed_Should_Return_True() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); + + Assert.Equal(2, res); + + var i1 = await _provider.SIsMemberAsync(cacheKey, "s1"); + var i2 = await _provider.SIsMemberAsync(cacheKey, "s2"); + + Assert.True(i1); + Assert.True(i2); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual async Task SIsMemberAsync_With_Not_Existed_Should_Return_False() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); + + Assert.Equal(2, res); + + var i1 = await _provider.SIsMemberAsync(cacheKey, "s3"); + + Assert.False(i1); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual async Task SMembersAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); + + var vals = await _provider.SMembersAsync(cacheKey); + + Assert.Equal(2, vals.Count); + Assert.Contains("s1", vals); + Assert.Contains("s2", vals); + + await _baseProvider.RemoveAsync(cacheKey); + } + + + [Fact] + protected virtual async Task SRemAsync_With_Values_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); + + var len = await _provider.SRemAsync(cacheKey, new List { "s1" }); + + Assert.Equal(1, len); + + var flag = await _provider.SIsMemberAsync(cacheKey, "s1"); + Assert.False(flag); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual async Task SRemAsync_Without_Values_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); + + var len = await _provider.SRemAsync(cacheKey); + + Assert.Equal(1, len); + + var flag = await _baseProvider.ExistsAsync(cacheKey); + Assert.False(flag); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual async Task SRandMemberAsync_With_Not_Exist_Should_Return_EmptyList() + { + var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; + + var len = await _provider.SRandMemberAsync(cacheKey); + + Assert.Empty(len); + + await _baseProvider.RemoveAsync(cacheKey); + } + + [Fact] + protected virtual async Task SRandMemberAsync_With_Exist_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; + + var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); + + var vals = await _provider.SRandMemberAsync(cacheKey, 2); + + Assert.Equal(2, vals.Count); + + await _baseProvider.RemoveAsync(cacheKey); + } + #endregion + + #region Sorted Set + [Fact] + protected virtual void ZRangeByScore_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var dict = new Dictionary() + { + { "one", 1}, + { "two", 2}, + { "three", 3}, + }; + + var res = _provider.ZAdd(cacheKey, dict); + + Assert.Equal(3, res); + + var items = _provider.ZRangeByScore(cacheKey, 1, 2, 10, 0); + + Assert.Equal(2, items.Count); + Assert.Contains("one", items); + Assert.Contains("two", items); + + _baseProvider.Remove(cacheKey); + } + + [Fact] + protected virtual async void ZRangeByScoreAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; + + var dict = new Dictionary() + { + { "one", 1}, + { "two", 2}, + { "three", 3}, + }; + + var res = await _provider.ZAddAsync(cacheKey, dict); + + Assert.Equal(3, res); + + var items = await _provider.ZRangeByScoreAsync(cacheKey, 1, 2); + + Assert.Equal(2, items.Count); + Assert.Contains("one", items); + Assert.Contains("two", items); + + _baseProvider.Remove(cacheKey); + } + #endregion + + #region Hyperloglog + [Fact] + protected virtual void PfAdd_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-pfadd-{Guid.NewGuid().ToString()}"; + + var res1 = _provider.PfAdd(cacheKey, new List { "foo", "bar", "zap" }); + var res2 = _provider.PfAdd(cacheKey, new List { "zap", "zap", "zap" }); + Assert.True(res1); + Assert.False(res2); + + var count = _provider.PfCount(new List { cacheKey }); - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual void StringSet_And_StringGet_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = _provider.StringSet(cacheKey, cacheValue); - - Assert.True(res); - - var val = _provider.StringGet(cacheKey); - - Assert.Equal(cacheValue, val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSet_And_StringGet_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = await _provider.StringSetAsync(cacheKey, cacheValue); - - Assert.True(res); - - var val = await _provider.StringGetAsync(cacheKey); - - Assert.Equal(cacheValue, val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual void IncrBy_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.IncrBy(cacheKey); - - Assert.Equal(1, res); - - var val = _provider.IncrBy(cacheKey, 3); - - Assert.Equal(4, val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task IncrByAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.IncrByAsync(cacheKey); - - Assert.Equal(1, res); - - var val = await _provider.IncrByAsync(cacheKey, 3); - - Assert.Equal(4, val); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void IncrByFloat_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.IncrByFloat(cacheKey); - - Assert.Equal(1d, res); - - var val = _provider.IncrByFloat(cacheKey, 3d); - - Assert.Equal(4d, val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task IncrByFloatAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.IncrByFloatAsync(cacheKey); - - Assert.Equal(1d, res); - - var val = await _provider.IncrByFloatAsync(cacheKey, 3d); - - Assert.Equal(4d, val); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void StringLen_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "catcherwong"; - - var flag = _provider.StringSet(cacheKey, cacheValue); - - Assert.True(flag); - - var len = _provider.StringLen(cacheKey); - - Assert.Equal(11, len); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringLenAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "catcherwong"; - - var flag = await _provider.StringSetAsync(cacheKey, cacheValue); - - Assert.True(flag); - - var len = await _provider.StringLenAsync(cacheKey); - - Assert.Equal(11, len); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void StringSetRange_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "Hello World"; - - var flag = _provider.StringSet(cacheKey, cacheValue); - - Assert.True(flag); - - var len = _provider.StringSetRange(cacheKey, 6, "Redis"); - - Assert.Equal(11, len); - - var val = _provider.StringGet(cacheKey); - - Assert.Equal("Hello Redis", val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSetRangeAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "Hello World"; - - var flag = await _provider.StringSetAsync(cacheKey, cacheValue); - - Assert.True(flag); - - var len = await _provider.StringSetRangeAsync(cacheKey, 6, "Redis"); - - Assert.Equal(11, len); - - var val = await _provider.StringGetAsync(cacheKey); - - Assert.Equal("Hello Redis", val); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void StringGetRange_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "This is a string"; - - var flag = _provider.StringSet(cacheKey, cacheValue); - - Assert.True(flag); - - var res1 = _provider.StringGetRange(cacheKey, 0, 3); - - Assert.Equal("This", res1); - - var res2 = _provider.StringGetRange(cacheKey, -3, -1); - - Assert.Equal("ing", res2); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringGetRangeAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "This is a string"; - - var flag = await _provider.StringSetAsync(cacheKey, cacheValue); - - Assert.True(flag); - - var res1 = await _provider.StringGetRangeAsync(cacheKey, 0, 3); - - Assert.Equal("This", res1); - - var res2 = await _provider.StringGetRangeAsync(cacheKey, -3, -1); - - Assert.Equal("ing", res2); - - await _provider.KeyDelAsync(cacheKey); - } - - - #endregion - - #region Hash - [Fact] - protected virtual void HMSet_And_HMGet_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var dict = _provider.HMGet(cacheKey, new List { "a1", "a2" }); - - Assert.Contains("v1", dict.Values); - Assert.Contains("v2", dict.Values); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HMSetAsync_And_HMGetAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var dict = await _provider.HMGetAsync(cacheKey, new List { "a1", "a2" }); - - Assert.Contains("v1", dict.Values); - Assert.Contains("v2", dict.Values); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HKeys_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"k1","v1"},{"k2","v2"} - }); - - var keys = _provider.HKeys(cacheKey); - - Assert.Contains("k1", keys); - Assert.Contains("k2", keys); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HKeysAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"k1","v1"},{"k2","v2"} - }); - - var keys = await _provider.HKeysAsync(cacheKey); - - Assert.Contains("k1", keys); - Assert.Contains("k2", keys); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HVals_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"k1","v1"},{"k2","v2"} - }); - - var vals = _provider.HVals(cacheKey); - - Assert.Contains("v1", vals); - Assert.Contains("v2", vals); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HValsAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"k1","v1"},{"k2","v2"} - }); - - var vals = await _provider.HValsAsync(cacheKey); - - Assert.Contains("v1", vals); - Assert.Contains("v2", vals); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HGetAll_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - var all = _provider.HGetAll(cacheKey); - - Assert.Equal(2, all.Count); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HGetAllAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - var all = await _provider.HGetAllAsync(cacheKey); - - Assert.Equal(2, all.Count); - - await _provider.HDelAsync(cacheKey); - } - - - [Fact] - protected virtual void HDel_With_Fields_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"},{"a3","v3"} - }); - - var del = _provider.HDel(cacheKey, new List { "a1", "a3" }); - - Assert.Equal(2, del); - - var len = _provider.HLen(cacheKey); - - Assert.Equal(1, len); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HDelAsync_With_Fields_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"},{"a3","v3"} - }); - - var del = await _provider.HDelAsync(cacheKey, new List { "a1", "a3" }); - - Assert.Equal(2, del); - - var len = await _provider.HLenAsync(cacheKey); - - Assert.Equal(1, len); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HDel_WithOut_Fields_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"},{"a3","v3"} - }); - - var del = _provider.HDel(cacheKey); - - Assert.Equal(1, del); - - var len = _provider.HLen(cacheKey); - - Assert.Equal(0, len); - } - - [Fact] - protected virtual async Task HDelAsync_WithOut_Fields_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"},{"a3","v3"} - }); - - var del = await _provider.HDelAsync(cacheKey); - - Assert.Equal(1, del); - - var len = await _provider.HLenAsync(cacheKey); - - Assert.Equal(0, len); - } - - [Fact] - protected virtual void HIncrBy_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; - - var res = _provider.HIncrBy(cacheKey, "a1"); - - Assert.Equal(1, res); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HIncrByAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HIncrByAsync(cacheKey, "a1"); - - Assert.Equal(1, res); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HIncrBy_With_Val_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; - - var res = _provider.HIncrBy(cacheKey, "a1", 3); - - Assert.Equal(3, res); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HIncrByAsync_With_Val_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HIncrByAsync(cacheKey, "a1", 3); - - Assert.Equal(3, res); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HExists_With_Other_CacheKey_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var flag = _provider.HExists(cacheKey, "field"); - - Assert.False(flag); - } - - [Fact] - protected virtual async Task HExistsAsync_With_Other_CacheKey_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var flag = await _provider.HExistsAsync(cacheKey, "field"); - - Assert.False(flag); - } - - - [Fact] - protected virtual void HExists_With_Other_Field_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var res = _provider.HSet(cacheKey, "a1", "v1"); - - Assert.True(res); - - var flag = _provider.HExists(cacheKey, "field"); - - Assert.False(flag); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HExistsAsync_With_Other_Field_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HSetAsync(cacheKey, "a1", "v1"); - - Assert.True(res); - - var flag = await _provider.HExistsAsync(cacheKey, "field"); - - Assert.False(flag); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HExists_With_Field_Should_Return_True() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var res = _provider.HSet(cacheKey, "a1", "v1"); - Assert.True(res); - - var flag = _provider.HExists(cacheKey, "a1"); - Assert.True(flag); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HExistsAsync_With_Field_Should_Return_True() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HSetAsync(cacheKey, "a1", "v1"); - Assert.True(res); - - var flag = await _provider.HExistsAsync(cacheKey, "a1"); - Assert.True(flag); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HSet_With_Field_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var flag = _provider.HSet(cacheKey, "a3", "v3"); - - Assert.True(flag); - - var len = _provider.HLen(cacheKey); - - Assert.Equal(3, len); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HSetAsync_With_Field_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var flag = await _provider.HSetAsync(cacheKey, "a3", "v3"); - - Assert.True(flag); - - var len = await _provider.HLenAsync(cacheKey); - - Assert.Equal(3, len); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HGet_With_Field_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var val = _provider.HGet(cacheKey, "a1"); - - Assert.NotNull(val); - Assert.Equal("v1", val); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HGetAsync_With_Field_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var val = await _provider.HGetAsync(cacheKey, "a1"); - - Assert.NotNull(val); - Assert.Equal("v1", val); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HGet_With_Other_Field_Should_Return_Null() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var val = _provider.HGet(cacheKey, "a3"); - - Assert.Null(val); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HGetAsync_With_Other_Field_Should_Return_Null() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var val = await _provider.HGetAsync(cacheKey, "a3"); - - Assert.Null(val); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HLen_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var len = _provider.HLen(cacheKey); - - Assert.Equal(2, len); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HLenAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var len = await _provider.HLenAsync(cacheKey); - - Assert.Equal(2, len); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HMSet_With_Expiration_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }, TimeSpan.FromSeconds(1)); - - Assert.True(res); - - System.Threading.Thread.Sleep(1050); - - var val = _provider.HGet(cacheKey, "a1"); - Assert.Null(val); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HMSetAsync_With_Expiration_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }, TimeSpan.FromSeconds(1)); - - Assert.True(res); - - await Task.Delay(1050); - - var val = await _provider.HGetAsync(cacheKey, "a1"); - Assert.Null(val); - - await _provider.HDelAsync(cacheKey); - } - #endregion - - #region List - [Fact] - protected virtual void LPush_And_LPop_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.LPop(cacheKey); - Assert.Equal("p2", val); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LPushAsync_And_LPopAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.LPopAsync(cacheKey); - Assert.Equal("p2", val); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LPushX_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.LPushX(cacheKey, "p4"); - Assert.Equal(3, val); - - var pop = _provider.LPop(cacheKey); - Assert.Equal("p4", pop); - - _baseProvider.Remove(cacheKey); - } - - - [Fact] - protected virtual async Task LPushXAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.LPushXAsync(cacheKey, "p4"); - Assert.Equal(3, val); - - var pop = await _provider.LPopAsync(cacheKey); - Assert.Equal("p4", pop); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LPushX_Not_Exist_CacheKey_Should_Return_Zero() - { - var cacheKey = $"{_nameSpace}-nex-{Guid.NewGuid().ToString()}"; - - var val = _provider.LPushX(cacheKey, "p4"); - - Assert.Equal(0, val); - } - - [Fact] - protected virtual async Task LPushXAsync_Not_Exist_CacheKey_Should_Return_Zero() - { - var cacheKey = $"{_nameSpace}-nex-{Guid.NewGuid().ToString()}"; - - var val = await _provider.LPushXAsync(cacheKey, "p4"); - - Assert.Equal(0, val); - } - - [Fact] - protected virtual void LSet_And_LRange_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1" }); - Assert.Equal(1, res); - - var val = _provider.LSet(cacheKey, 0, "p3"); - Assert.True(val); - - var list = _provider.LRange(cacheKey, 0, -1); - - Assert.Single(list); - Assert.Equal("p3", list[0]); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LSetAsync_And_LRangeAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1" }); - Assert.Equal(1, res); - - var val = await _provider.LSetAsync(cacheKey, 0, "p3"); - Assert.True(val); - - var list = await _provider.LRangeAsync(cacheKey, 0, -1); - - Assert.Single(list); - Assert.Equal("p3", list[0]); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LIndex_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - - Assert.Equal(2, res); - - var val0 = _provider.LIndex(cacheKey, 0); - var val1 = _provider.LIndex(cacheKey, 1); - - Assert.Equal("p2", val0); - Assert.Equal("p1", val1); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LIndexAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - - Assert.Equal(2, res); - - var val0 = await _provider.LIndexAsync(cacheKey, 0); - var val1 = await _provider.LIndexAsync(cacheKey, 1); - - Assert.Equal("p2", val0); - Assert.Equal("p1", val1); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LRem_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2", "p1", "p2", "p1" }); - Assert.Equal(5, res); - - _provider.LRem(cacheKey, 0, "p1"); - - var len1 = _provider.LLen(cacheKey); - Assert.Equal(2, len1); - - _provider.LRem(cacheKey, 1, "p2"); - - var len2 = _provider.LLen(cacheKey); - Assert.Equal(1, len2); - - _baseProvider.Remove(cacheKey); - } - - - [Fact] - protected virtual async Task LRemAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2", "p1", "p2", "p1" }); - Assert.Equal(5, res); - - await _provider.LRemAsync(cacheKey, 0, "p1"); - - var len1 = await _provider.LLenAsync(cacheKey); - Assert.Equal(2, len1); - - await _provider.LRemAsync(cacheKey, 1, "p2"); - - var len2 = await _provider.LLenAsync(cacheKey); - Assert.Equal(1, len2); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LTrim_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2", "p3" }); - Assert.Equal(3, res); - - var flag = _provider.LTrim(cacheKey, 2, -1); - Assert.True(flag); - - var vals = _provider.LRange(cacheKey, 0, -1); - Assert.Single(vals); - Assert.Equal("p1", vals[0]); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LTrimAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2", "p3" }); - Assert.Equal(3, res); - - var flag = await _provider.LTrimAsync(cacheKey, 2, -1); - Assert.True(flag); - - var vals = await _provider.LRangeAsync(cacheKey, 0, -1); - Assert.Single(vals); - Assert.Equal("p1", vals[0]); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LLen_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var len = _provider.LLen(cacheKey); - Assert.Equal(2, len); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LLenAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var len = await _provider.LLenAsync(cacheKey); - Assert.Equal(2, len); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void RPush_And_RPop_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.RPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.RPop(cacheKey); - Assert.Equal("p2", val); - - _baseProvider.Remove(cacheKey); - } - - - [Fact] - protected virtual async Task RPushAsync_And_RPopAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.RPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.RPopAsync(cacheKey); - Assert.Equal("p2", val); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void RPushX_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.RPushX(cacheKey, "p4"); - Assert.Equal(3, val); - - var pop = _provider.RPop(cacheKey); - Assert.Equal("p4", pop); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task RPushXAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.RPushXAsync(cacheKey, "p4"); - Assert.Equal(3, val); - - var pop = await _provider.RPopAsync(cacheKey); - Assert.Equal("p4", pop); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LInsertBefore_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.LInsertBefore(cacheKey, "p1", "p4"); - Assert.Equal(3, val); - - var list = _provider.LRange(cacheKey, 0, -1); - Assert.Equal("p4", list[1]); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LInsertBeforeAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.LInsertBeforeAsync(cacheKey, "p1", "p4"); - Assert.Equal(3, val); - - var list = await _provider.LRangeAsync(cacheKey, 0, -1); - Assert.Equal("p4", list[1]); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LInsertAfter_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.LInsertAfter(cacheKey, "p1", "p4"); - Assert.Equal(3, val); - - var list = _provider.LRange(cacheKey, 0, -1); - Assert.Equal("p4", list[2]); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LInsertAfterAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.LInsertAfterAsync(cacheKey, "p1", "p4"); - Assert.Equal(3, val); - - var list = await _provider.LRangeAsync(cacheKey, 0, -1); - Assert.Equal("p4", list[2]); - - await _baseProvider.RemoveAsync(cacheKey); - } - #endregion - - #region Set - [Fact] - protected virtual void SAdd_And_SCard_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var len = _provider.SCard(cacheKey); - - Assert.Equal(2, len); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SAdd_With_Expiration_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }, TimeSpan.FromSeconds(1)); - - Assert.Equal(2, res); - - System.Threading.Thread.Sleep(1050); - - var flag = _baseProvider.Exists(cacheKey); - - Assert.False(flag); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SIsMember_With_Existed_Should_Return_True() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var i1 = _provider.SIsMember(cacheKey, "s1"); - var i2 = _provider.SIsMember(cacheKey, "s2"); - - Assert.True(i1); - Assert.True(i2); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SIsMember_With_Not_Existed_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var i1 = _provider.SIsMember(cacheKey, "s3"); - - Assert.False(i1); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SMembers_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - var vals = _provider.SMembers(cacheKey); - - Assert.Equal(2, vals.Count); - Assert.Contains("s1", vals); - Assert.Contains("s2", vals); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SRem_With_Values_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - var len = _provider.SRem(cacheKey, new List { "s1" }); - - Assert.Equal(1, len); - - var flag = _provider.SIsMember(cacheKey, "s1"); - Assert.False(flag); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SRem_Without_Values_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - var len = _provider.SRem(cacheKey); - - Assert.Equal(1, len); - - var flag = _baseProvider.Exists(cacheKey); - Assert.False(flag); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SRandMember_With_Not_Exist_Should_Return_EmptyList() - { - var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; - - var len = _provider.SRandMember(cacheKey); - - Assert.Empty(len); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SRandMember_With_Exist_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - var vals = _provider.SRandMember(cacheKey, 2); - - Assert.Equal(2, vals.Count); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task SAddAsync_And_SCardAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var len = await _provider.SCardAsync(cacheKey); - - Assert.Equal(2, len); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SAddAsync_With_Expiration_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }, TimeSpan.FromSeconds(1)); - - Assert.Equal(2, res); - - await Task.Delay(1050); - - var len = await _provider.SCardAsync(cacheKey); - - Assert.Equal(0, len); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task SIsMemberAsync_With_Existed_Should_Return_True() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var i1 = await _provider.SIsMemberAsync(cacheKey, "s1"); - var i2 = await _provider.SIsMemberAsync(cacheKey, "s2"); - - Assert.True(i1); - Assert.True(i2); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SIsMemberAsync_With_Not_Existed_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var i1 = await _provider.SIsMemberAsync(cacheKey, "s3"); - - Assert.False(i1); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SMembersAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - var vals = await _provider.SMembersAsync(cacheKey); - - Assert.Equal(2, vals.Count); - Assert.Contains("s1", vals); - Assert.Contains("s2", vals); - - await _baseProvider.RemoveAsync(cacheKey); - } - - - [Fact] - protected virtual async Task SRemAsync_With_Values_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - var len = await _provider.SRemAsync(cacheKey, new List { "s1" }); - - Assert.Equal(1, len); - - var flag = await _provider.SIsMemberAsync(cacheKey, "s1"); - Assert.False(flag); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SRemAsync_Without_Values_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - var len = await _provider.SRemAsync(cacheKey); - - Assert.Equal(1, len); - - var flag = await _baseProvider.ExistsAsync(cacheKey); - Assert.False(flag); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SRandMemberAsync_With_Not_Exist_Should_Return_EmptyList() - { - var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; - - var len = await _provider.SRandMemberAsync(cacheKey); - - Assert.Empty(len); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SRandMemberAsync_With_Exist_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - var vals = await _provider.SRandMemberAsync(cacheKey, 2); - - Assert.Equal(2, vals.Count); - - await _baseProvider.RemoveAsync(cacheKey); - } - #endregion - - #region Hyperloglog - [Fact] - protected virtual void PfAdd_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-pfadd-{Guid.NewGuid().ToString()}"; - - var res1 = _provider.PfAdd(cacheKey, new List { "foo", "bar", "zap" }); - var res2 = _provider.PfAdd(cacheKey, new List { "zap", "zap", "zap" }); - Assert.True(res1); - Assert.False(res2); - - var count = _provider.PfCount(new List { cacheKey }); - Assert.Equal(3, count); - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task PfAddAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-pfaddasync-{Guid.NewGuid().ToString()}"; - - var res1 = await _provider.PfAddAsync(cacheKey, new List { "foo", "bar", "zap" }); - var res2 = await _provider.PfAddAsync(cacheKey, new List { "zap", "zap", "zap" }); - Assert.True(res1); - Assert.False(res2); - - var count = await _provider.PfCountAsync(new List { cacheKey }); - + _provider.KeyDel(cacheKey); + } + + [Fact] + protected virtual async Task PfAddAsync_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-pfaddasync-{Guid.NewGuid().ToString()}"; + + var res1 = await _provider.PfAddAsync(cacheKey, new List { "foo", "bar", "zap" }); + var res2 = await _provider.PfAddAsync(cacheKey, new List { "zap", "zap", "zap" }); + Assert.True(res1); + Assert.False(res2); + + var count = await _provider.PfCountAsync(new List { cacheKey }); + Assert.Equal(3, count); - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void PfMerge_Should_Succeed() - { - var cacheKey0 = $"{_nameSpace}-pfmerge-{Guid.NewGuid().ToString()}"; - var cacheKey1 = $"{_nameSpace}-pfmerge-{Guid.NewGuid().ToString()}"; - var cacheKey2 = $"{_nameSpace}-pfmerge-{Guid.NewGuid().ToString()}"; - - var res1 = _provider.PfAdd(cacheKey1, new List { "foo", "bar", "zap", "a" }); - var res2 = _provider.PfAdd(cacheKey2, new List { "a", "b", "c", "foo" }); - Assert.True(res1); - Assert.True(res2); - - - var flag = _provider.PfMerge(cacheKey0, new List { cacheKey1, cacheKey2 }); - Assert.True(flag); - - var count = _provider.PfCount(new List { cacheKey0 }); - - Assert.Equal(6, count); - - _provider.KeyDel(cacheKey0); - _provider.KeyDel(cacheKey1); - _provider.KeyDel(cacheKey2); - } - - [Fact] - protected virtual async Task PfMergeAsync_Should_Succeed() - { - var cacheKey0 = $"{_nameSpace}-pfmergeasync-{Guid.NewGuid().ToString()}"; - var cacheKey1 = $"{_nameSpace}-pfmergeasync-{Guid.NewGuid().ToString()}"; - var cacheKey2 = $"{_nameSpace}-pfmergeasync-{Guid.NewGuid().ToString()}"; - - var res1 = await _provider.PfAddAsync(cacheKey1, new List { "foo", "bar", "zap", "a" }); - var res2 = await _provider.PfAddAsync(cacheKey2, new List { "a", "b", "c", "foo" }); - Assert.True(res1); - Assert.True(res2); - - - var flag = await _provider.PfMergeAsync(cacheKey0, new List { cacheKey1, cacheKey2 }); - Assert.True(flag); - - var count = await _provider.PfCountAsync(new List { cacheKey0 }); - - Assert.Equal(6, count); - + await _provider.KeyDelAsync(cacheKey); + } + + [Fact] + protected virtual void PfMerge_Should_Succeed() + { + var cacheKey0 = $"{_nameSpace}-pfmerge-{Guid.NewGuid().ToString()}"; + var cacheKey1 = $"{_nameSpace}-pfmerge-{Guid.NewGuid().ToString()}"; + var cacheKey2 = $"{_nameSpace}-pfmerge-{Guid.NewGuid().ToString()}"; + + var res1 = _provider.PfAdd(cacheKey1, new List { "foo", "bar", "zap", "a" }); + var res2 = _provider.PfAdd(cacheKey2, new List { "a", "b", "c", "foo" }); + Assert.True(res1); + Assert.True(res2); + + + var flag = _provider.PfMerge(cacheKey0, new List { cacheKey1, cacheKey2 }); + Assert.True(flag); + + var count = _provider.PfCount(new List { cacheKey0 }); + + Assert.Equal(6, count); + + _provider.KeyDel(cacheKey0); + _provider.KeyDel(cacheKey1); + _provider.KeyDel(cacheKey2); + } + + [Fact] + protected virtual async Task PfMergeAsync_Should_Succeed() + { + var cacheKey0 = $"{_nameSpace}-pfmergeasync-{Guid.NewGuid().ToString()}"; + var cacheKey1 = $"{_nameSpace}-pfmergeasync-{Guid.NewGuid().ToString()}"; + var cacheKey2 = $"{_nameSpace}-pfmergeasync-{Guid.NewGuid().ToString()}"; + + var res1 = await _provider.PfAddAsync(cacheKey1, new List { "foo", "bar", "zap", "a" }); + var res2 = await _provider.PfAddAsync(cacheKey2, new List { "a", "b", "c", "foo" }); + Assert.True(res1); + Assert.True(res2); + + + var flag = await _provider.PfMergeAsync(cacheKey0, new List { cacheKey1, cacheKey2 }); + Assert.True(flag); + + var count = await _provider.PfCountAsync(new List { cacheKey0 }); + + Assert.Equal(6, count); + await _provider.KeyDelAsync(cacheKey0); - await _provider.KeyDelAsync(cacheKey1); - await _provider.KeyDelAsync(cacheKey2); + await _provider.KeyDelAsync(cacheKey1); + await _provider.KeyDelAsync(cacheKey2); } - #endregion + #endregion #region Geo - [Fact] - protected virtual void GeoAdd_And_GeoDist_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-geoadd-{Guid.NewGuid().ToString()}"; - + [Fact] + protected virtual void GeoAdd_And_GeoDist_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-geoadd-{Guid.NewGuid().ToString()}"; + var res = _provider.GeoAdd(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - Assert.Equal(2, res); - - var dist = _provider.GeoDist(cacheKey, "Palermo", "Catania"); - - // precision? + Assert.Equal(2, res); + + var dist = _provider.GeoDist(cacheKey, "Palermo", "Catania"); + + // precision? Assert.Equal(166274.1516, dist); - _provider.KeyDel(cacheKey); + _provider.KeyDel(cacheKey); } - [Fact] - protected virtual async Task GeoAddAsync_And_GeoDistAsync_Should_Succeed() - { + [Fact] + protected virtual async Task GeoAddAsync_And_GeoDistAsync_Should_Succeed() + { var cacheKey = $"{_nameSpace}-geoaddasync-{Guid.NewGuid().ToString()}"; var res = await _provider.GeoAddAsync(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - Assert.Equal(2, res); - - var dist = await _provider.GeoDistAsync(cacheKey, "Palermo", "Catania"); - - // precision? + Assert.Equal(2, res); + + var dist = await _provider.GeoDistAsync(cacheKey, "Palermo", "Catania"); + + // precision? Assert.Equal(166274.1516, dist); - await _provider.KeyDelAsync(cacheKey); + await _provider.KeyDelAsync(cacheKey); } - [Fact] - protected virtual void GeoAdd_And_GeoHash_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-geohash-{Guid.NewGuid().ToString()}"; - + [Fact] + protected virtual void GeoAdd_And_GeoHash_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-geohash-{Guid.NewGuid().ToString()}"; + var res = _provider.GeoAdd(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - Assert.Equal(2, res); - - var hash = _provider.GeoHash(cacheKey, new List { "Palermo", "Catania" }); - - Assert.Equal(2, hash.Count); - Assert.Contains("sqc8b49rny0", hash); + Assert.Equal(2, res); + + var hash = _provider.GeoHash(cacheKey, new List { "Palermo", "Catania" }); + + Assert.Equal(2, hash.Count); + Assert.Contains("sqc8b49rny0", hash); Assert.Contains("sqdtr74hyu0", hash); - _provider.KeyDel(cacheKey); + _provider.KeyDel(cacheKey); } - [Fact] - protected virtual async Task GeoAddAsync_And_GeoHashAsync_Should_Succeed() - { + [Fact] + protected virtual async Task GeoAddAsync_And_GeoHashAsync_Should_Succeed() + { var cacheKey = $"{_nameSpace}-geohashasync-{Guid.NewGuid().ToString()}"; var res = await _provider.GeoAddAsync(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - Assert.Equal(2, res); - - var hash = await _provider.GeoHashAsync(cacheKey, new List { "Palermo", "Catania" }); - - Assert.Equal(2, hash.Count); - Assert.Contains("sqc8b49rny0", hash); + Assert.Equal(2, res); + + var hash = await _provider.GeoHashAsync(cacheKey, new List { "Palermo", "Catania" }); + + Assert.Equal(2, hash.Count); + Assert.Contains("sqc8b49rny0", hash); Assert.Contains("sqdtr74hyu0", hash); - await _provider.KeyDelAsync(cacheKey); + await _provider.KeyDelAsync(cacheKey); } - [Fact] - protected virtual void GeoAdd_And_GeoPos_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-geohash-{Guid.NewGuid().ToString()}"; - + [Fact] + protected virtual void GeoAdd_And_GeoPos_Should_Succeed() + { + var cacheKey = $"{_nameSpace}-geohash-{Guid.NewGuid().ToString()}"; + var res = _provider.GeoAdd(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - Assert.Equal(2, res); - - var pos = _provider.GeoPos(cacheKey, new List { "Palermo", "Catania", "NonExisting" }); - - Assert.Equal(3, pos.Count); - Assert.Contains(13.36138933897018433m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); + Assert.Equal(2, res); + + var pos = _provider.GeoPos(cacheKey, new List { "Palermo", "Catania", "NonExisting" }); + + Assert.Equal(3, pos.Count); + Assert.Contains(13.36138933897018433m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); Assert.Contains(15.08726745843887329m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); Assert.Contains(null, pos); - _provider.KeyDel(cacheKey); + _provider.KeyDel(cacheKey); } - [Fact] - protected virtual async Task GeoAddAsync_And_GeoPosAsync_Should_Succeed() - { + [Fact] + protected virtual async Task GeoAddAsync_And_GeoPosAsync_Should_Succeed() + { var cacheKey = $"{_nameSpace}-geohashasync-{Guid.NewGuid().ToString()}"; var res = await _provider.GeoAddAsync(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - Assert.Equal(2, res); - - var pos = await _provider.GeoPosAsync(cacheKey, new List { "Palermo", "Catania", "NonExisting" }); - - Assert.Equal(3, pos.Count); - Assert.Contains(13.36138933897018433m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); + Assert.Equal(2, res); + + var pos = await _provider.GeoPosAsync(cacheKey, new List { "Palermo", "Catania", "NonExisting" }); + + Assert.Equal(3, pos.Count); + Assert.Contains(13.36138933897018433m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); Assert.Contains(15.08726745843887329m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); Assert.Contains(null, pos); - await _provider.KeyDelAsync(cacheKey); + await _provider.KeyDelAsync(cacheKey); } - #endregion - } -} + #endregion + } +} diff --git a/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs index ee327dec..8e132070 100644 --- a/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs @@ -33,7 +33,7 @@ protected override IEasyCachingProvider CreateCachingProvider(Action(); @@ -91,7 +91,37 @@ public CSRedisCachingProviderWithNamedSerTest() }, "cs2"); - option.UseCSRedis(config => + option.WithJson("json").WithMessagePack("cs11").WithJson("cs2"); + }); + + IServiceProvider serviceProvider = services.BuildServiceProvider(); + _providerFactory = serviceProvider.GetService(); + } + + [Fact] + public void NamedSerializerTest() + { + var cs1 = _providerFactory.GetCachingProvider("cs1"); + var cs2 = _providerFactory.GetCachingProvider("cs2"); + + var info1 = cs1.GetProviderInfo(); + var info2 = cs2.GetProviderInfo(); + + Assert.Equal("cs11", info1.Serializer.Name); + Assert.Equal("cs2", info2.Serializer.Name); + } + } + + public class CSRedisCachingProviderWithKeyPrefixTest + { + private readonly IEasyCachingProviderFactory _providerFactory; + + public CSRedisCachingProviderWithKeyPrefixTest() + { + IServiceCollection services = new ServiceCollection(); + services.AddEasyCaching(x => + { + x.UseCSRedis(config => { config.DBConfig = new CSRedisDBOptions { @@ -101,9 +131,24 @@ public CSRedisCachingProviderWithNamedSerTest() } }; - }, "cs3"); + config.SerializerName = "json"; + }, "NotKeyPrefix"); - option.WithJson("json").WithMessagePack("cs11").WithJson("cs2"); + x.UseCSRedis(config => + { + config.DBConfig = new CSRedisDBOptions + { + ConnectionStrings = new System.Collections.Generic.List + { + "127.0.0.1:6388,defaultDatabase=3,poolsize=10,prefix=foo:" + } + }; + config.SerializerName = "json"; + + }, "WithKeyPrefix"); + + + x.WithJson("json"); }); IServiceProvider serviceProvider = services.BuildServiceProvider(); @@ -111,19 +156,42 @@ public CSRedisCachingProviderWithNamedSerTest() } [Fact] - public void NamedSerializerTest() + public void KeyPrefixTest() { - var cs1 = _providerFactory.GetCachingProvider("cs1"); - var cs2 = _providerFactory.GetCachingProvider("cs2"); - var cs3 = _providerFactory.GetCachingProvider("cs3"); + var NotKeyPrefix = _providerFactory.GetCachingProvider("NotKeyPrefix"); + var WithKeyPrefix = _providerFactory.GetCachingProvider("WithKeyPrefix"); - var info1 = cs1.GetProviderInfo(); - var info2 = cs2.GetProviderInfo(); - var info3 = cs3.GetProviderInfo(); + WithKeyPrefix.Set("KeyPrefix", "ok", TimeSpan.FromSeconds(10)); - Assert.Equal("cs11", info1.Serializer.Name); - Assert.Equal("cs2", info2.Serializer.Name); - Assert.Equal(EasyCachingConstValue.DefaultSerializerName, info3.Serializer.Name); + var val1 = NotKeyPrefix.Get("foo:" + "KeyPrefix"); + var val2 = WithKeyPrefix.Get("foo:" + "KeyPrefix"); + Assert.NotEqual(val1.Value, val2.Value); + + var val3 = WithKeyPrefix.Get("KeyPrefix"); + Assert.Equal(val1.Value, val3.Value); + } + + [Fact] + public void RemoveByPrefixTest() + { + var WithKeyPrefix = _providerFactory.GetCachingProvider("WithKeyPrefix"); + + WithKeyPrefix.Set("KeyPrefix1", "ok", TimeSpan.FromSeconds(10)); + WithKeyPrefix.Set("KeyPrefix2", "ok", TimeSpan.FromSeconds(10)); + + var val1 = WithKeyPrefix.Get("KeyPrefix1"); + var val2 = WithKeyPrefix.Get("KeyPrefix2"); + + Assert.True(val1.HasValue); + Assert.True(val2.HasValue); + Assert.Equal(val1.Value, val2.Value); + + WithKeyPrefix.RemoveByPrefix("Key"); + + var val3 = WithKeyPrefix.Get("KeyPrefix1"); + var val4 = WithKeyPrefix.Get("KeyPrefix2"); + Assert.False(val3.HasValue); + Assert.False(val4.HasValue); } } } diff --git a/test/EasyCaching.UnitTests/CachingTests/CSRedisFeatureCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/CSRedisFeatureCachingProviderTest.cs index 2ed82cf3..2ac395f6 100644 --- a/test/EasyCaching.UnitTests/CachingTests/CSRedisFeatureCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/CSRedisFeatureCachingProviderTest.cs @@ -12,6 +12,7 @@ public CSRedisFeatureCachingProviderTest() IServiceCollection services = new ServiceCollection(); services.AddEasyCaching(option => { + option.WithJson("ser"); option.UseCSRedis(config => { config.DBConfig = new CSRedisDBOptions @@ -21,6 +22,7 @@ public CSRedisFeatureCachingProviderTest() "127.0.0.1:6388,defaultDatabase=10,poolsize=10" } }; + config.SerializerName = "ser"; }); }); diff --git a/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs b/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs index f63936fa..1cb389ff 100644 --- a/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs @@ -49,7 +49,7 @@ public HybridCachingTest() { config.DBConfig.Endpoints.Add(new Core.Configurations.ServerEndPoint("127.0.0.1", 6379)); config.DBConfig.Database = 5; - }, "myredis"); + }, "myredis").WithJson("myredis"); option.UseHybrid(config => { @@ -63,6 +63,7 @@ public HybridCachingTest() { config.Endpoints.Add(new Core.Configurations.ServerEndPoint("127.0.0.1", 6379)); config.Database = 6; + config.SerializerName = "myredis"; }); }); diff --git a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs index 02a9fdc1..035efdb3 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs @@ -386,4 +386,42 @@ public void Provider_Information_Should_Be_Correct() Assert.Equal("mName", _provider.Name); } } + + public class MemcachedProviderNoConnectionTest + { + [Fact] + public async void NoConnectionTest() + { + IServiceCollection services = new ServiceCollection(); + services.AddLogging(); + services.AddEasyCaching(option => + { + option.UseMemcached(config => + { + config.DBConfig = new EasyCachingMemcachedClientOptions + { + Servers = new System.Collections.Generic.List + { + new Enyim.Caching.Configuration.Server() {Address = "123.123.123.123", Port = 45678 } + }, + SocketPool = new Enyim.Caching.Configuration.SocketPoolOptions + { + ConnectionTimeout = TimeSpan.FromSeconds(2), + DeadTimeout = TimeSpan.FromSeconds(2), + ReceiveTimeout = TimeSpan.FromSeconds(2), + } + }; + }, EasyCachingConstValue.DefaultMemcachedName); + }); + + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var factory = serviceProvider.GetService(); + var provider = factory.GetCachingProvider(EasyCachingConstValue.DefaultMemcachedName); + + Assert.Throws(() => provider.Get("123123")); + await Assert.ThrowsAnyAsync(() => provider.GetAsync("123123")); + Assert.Throws(() => provider.Remove("123123")); + await Assert.ThrowsAnyAsync(() => provider.RemoveAsync("123123")); + } + } } \ No newline at end of file diff --git a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs index 6fe66aef..1cb92953 100644 --- a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs @@ -32,7 +32,7 @@ protected override IEasyCachingProvider CreateCachingProvider(Action(); } @@ -49,7 +49,7 @@ public void Fulsh_Should_Fail_When_AllowAdmin_Is_False() IServiceCollection services = new ServiceCollection(); services.AddEasyCaching(x => x.UseRedis(options => { options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); }, - ProviderName) + ProviderName).WithJson(ProviderName) ); IServiceProvider serviceProvider = services.BuildServiceProvider(); var factory = serviceProvider.GetRequiredService(); @@ -88,7 +88,7 @@ public void Use_Configuration_String_Should_Succeed() x.UseRedis(options => { options.DBConfig.Configuration = "127.0.0.1:6380,allowAdmin=false,defaultdatabase=8"; - })); + }, ProviderName).WithJson(ProviderName)); IServiceProvider serviceProvider = services.BuildServiceProvider(); var dbProvider = serviceProvider.GetService(); Assert.NotNull(dbProvider); @@ -122,6 +122,8 @@ public RedisCachingProviderWithFactoryTest() IServiceCollection services = new ServiceCollection(); services.AddEasyCaching(x => { + x.WithJson("ser"); + x.UseRedis(options => { options.DBConfig = new RedisDBOptions @@ -130,6 +132,7 @@ public RedisCachingProviderWithFactoryTest() }; options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); options.DBConfig.Database = 3; + options.SerializerName = "ser"; }); @@ -141,6 +144,7 @@ public RedisCachingProviderWithFactoryTest() }; options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); options.DBConfig.Database = 4; + options.SerializerName = "ser"; }, SECOND_PROVIDER_NAME); }); IServiceProvider serviceProvider = services.BuildServiceProvider(); @@ -183,16 +187,6 @@ public RedisCachingProviderWithNamedSerTest() options.DBConfig.Database = 14; }, "se2"); - x.UseRedis(options => - { - options.DBConfig = new RedisDBOptions - { - AllowAdmin = true - }; - options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); - options.DBConfig.Database = 11; - }, "se3"); - x.WithJson("json").WithMessagePack("cs11").WithJson("se2"); }); @@ -205,15 +199,12 @@ public void NamedSerializerTest() { var se1 = _providerFactory.GetCachingProvider("se1"); var se2 = _providerFactory.GetCachingProvider("se2"); - var se3 = _providerFactory.GetCachingProvider("se3"); var info1 = se1.GetProviderInfo(); var info2 = se2.GetProviderInfo(); - var info3 = se3.GetProviderInfo(); Assert.Equal("cs11", info1.Serializer.Name); Assert.Equal("se2", info2.Serializer.Name); - Assert.Equal(EasyCachingConstValue.DefaultSerializerName, info3.Serializer.Name); } } diff --git a/test/EasyCaching.UnitTests/CachingTests/SERedisFeatureCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/SERedisFeatureCachingProviderTest.cs index e817a1bc..c14e3722 100644 --- a/test/EasyCaching.UnitTests/CachingTests/SERedisFeatureCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/SERedisFeatureCachingProviderTest.cs @@ -25,7 +25,7 @@ public SERedisFeatureCachingProviderTest() }; config.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); config.DBConfig.Database = 10; - }); + }).WithJson(EasyCachingConstValue.DefaultRedisName); }); IServiceProvider serviceProvider = services.BuildServiceProvider(); diff --git a/test/EasyCaching.UnitTests/DistributedLock/CSRedisLockTest.cs b/test/EasyCaching.UnitTests/DistributedLock/CSRedisLockTest.cs index 1677b209..71f0da31 100644 --- a/test/EasyCaching.UnitTests/DistributedLock/CSRedisLockTest.cs +++ b/test/EasyCaching.UnitTests/DistributedLock/CSRedisLockTest.cs @@ -21,7 +21,7 @@ public class CSRedisLockTest : DistributedLockTest } }; }) - .UseCSRedisLock()) + .UseCSRedisLock().WithJson(EasyCachingConstValue.DefaultCSRedisName)) .BuildServiceProvider() .GetService(); diff --git a/test/EasyCaching.UnitTests/DistributedLock/RedisLockTest.cs b/test/EasyCaching.UnitTests/DistributedLock/RedisLockTest.cs index 0f3d23dd..8eecc21f 100644 --- a/test/EasyCaching.UnitTests/DistributedLock/RedisLockTest.cs +++ b/test/EasyCaching.UnitTests/DistributedLock/RedisLockTest.cs @@ -22,7 +22,7 @@ public class RedisLockTest : DistributedLockTest } }; }) - .UseRedisLock()) + .UseRedisLock().WithJson(EasyCachingConstValue.DefaultRedisName)) .BuildServiceProvider() .GetService(); diff --git a/test/EasyCaching.UnitTests/ProviderFactoryTests.cs b/test/EasyCaching.UnitTests/ProviderFactoryTests.cs index d40f4ab0..21504788 100644 --- a/test/EasyCaching.UnitTests/ProviderFactoryTests.cs +++ b/test/EasyCaching.UnitTests/ProviderFactoryTests.cs @@ -25,6 +25,8 @@ public ProviderFactoryTests() IServiceCollection services = new ServiceCollection(); services.AddEasyCaching(option => { + option.WithJson("redis1"); + option.WithJson("redis2"); option.UseRedis(config => { config.DBConfig = new RedisDBOptions