From f657a7e2545a9e170a23b5f3e692fb17c930aa21 Mon Sep 17 00:00:00 2001 From: catcherwong Date: Wed, 2 Feb 2022 22:28:13 +0800 Subject: [PATCH 1/7] docs: update hybird usage --- docs/Hybrid.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/Hybrid.md b/docs/Hybrid.md index 17fb1fd0..3552a2eb 100644 --- a/docs/Hybrid.md +++ b/docs/Hybrid.md @@ -34,6 +34,8 @@ public class Startup services.AddEasyCaching(option => { + option.WithJson("myjson"); + // local option.UseInMemory("m1"); // distributed @@ -41,6 +43,7 @@ public class Startup { config.DBConfig.Endpoints.Add(new Core.Configurations.ServerEndPoint("127.0.0.1", 6379)); config.DBConfig.Database = 5; + config.SerializerName = "myjson"; }, "myredis"); // combine local and distributed @@ -58,6 +61,9 @@ public class Startup .WithRedisBus(busConf => { busConf.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); + + // do not forget to set the SerializerName for the bus here !! + busConf.SerializerName = "myjson"; }); }); } From 96058ac7902763678f89ef4091cd63610343f177 Mon Sep 17 00:00:00 2001 From: "t.sarmis" Date: Sat, 5 Mar 2022 08:53:07 +0200 Subject: [PATCH 2/7] feat: allow user to define client provided name --- bus/EasyCaching.Bus.RabbitMQ/DefaultRabbitMQBus.cs | 3 ++- src/EasyCaching.Core/Configurations/BaseRabbitMQOptions.cs | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/bus/EasyCaching.Bus.RabbitMQ/DefaultRabbitMQBus.cs b/bus/EasyCaching.Bus.RabbitMQ/DefaultRabbitMQBus.cs index cc8eec84..72e6ad84 100644 --- a/bus/EasyCaching.Bus.RabbitMQ/DefaultRabbitMQBus.cs +++ b/bus/EasyCaching.Bus.RabbitMQ/DefaultRabbitMQBus.cs @@ -64,7 +64,8 @@ IPooledObjectPolicy _objectPolicy VirtualHost = _options.VirtualHost, RequestedConnectionTimeout = System.TimeSpan.FromMilliseconds(_options.RequestedConnectionTimeout), SocketReadTimeout = System.TimeSpan.FromMilliseconds(_options.SocketReadTimeout), - SocketWriteTimeout = System.TimeSpan.FromMilliseconds(_options.SocketWriteTimeout) + SocketWriteTimeout = System.TimeSpan.FromMilliseconds(_options.SocketWriteTimeout), + ClientProvidedName = _options.ClientProvidedName }; _subConnection = factory.CreateConnection(); diff --git a/src/EasyCaching.Core/Configurations/BaseRabbitMQOptions.cs b/src/EasyCaching.Core/Configurations/BaseRabbitMQOptions.cs index aae147f1..eec55588 100644 --- a/src/EasyCaching.Core/Configurations/BaseRabbitMQOptions.cs +++ b/src/EasyCaching.Core/Configurations/BaseRabbitMQOptions.cs @@ -51,5 +51,10 @@ public class BaseRabbitMQOptions /// Gets or sets queue message automatic deletion time (in milliseconds). Default 864000000 ms (10 days). /// public int QueueMessageExpires { get; set; } = 864000000; + + /// + /// Gets or sets the client-provided name for the rabbit connection. Default null (handled by rabbit client) + /// + public string ClientProvidedName { get; set; } } } From 5bc7b2cbf49ce7eedc3770ae770004e3d821caab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mark=20B=C3=B8g=20L=C3=B8nquist?= Date: Sun, 6 Mar 2022 12:13:03 +0100 Subject: [PATCH 3/7] handle keyPrefix --- .../DefaultRedisCachingProvider.Async.cs | 20 ++++++++++-------- .../DefaultRedisCachingProvider.cs | 21 +++++++++++++------ .../CachingTests/RedisCachingProviderTest.cs | 3 ++- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs index 89a7d137..57065d0b 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs @@ -53,7 +53,8 @@ public override async Task BaseGetAsync(string cacheKey, Type type) /// Data retriever. /// Expiration. /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, + TimeSpan expiration) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); @@ -75,7 +76,8 @@ public override async Task> BaseGetAsync(string cacheKey, Func< if (_options.EnableLogging) _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); - var flag = await _cache.StringSetAsync($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs), When.NotExists); + var flag = await _cache.StringSetAsync($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs), + When.NotExists); if (!flag) { @@ -144,7 +146,7 @@ public override Task BaseGetCountAsync(string prefix = "") var allCount = 0; foreach (var server in _servers) - allCount += (int)server.DatabaseSize(_cache.Database); + allCount += (int) server.DatabaseSize(_cache.Database); return Task.FromResult(allCount); } @@ -185,9 +187,9 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp } await _cache.StringSetAsync( - cacheKey, - _serializer.Serialize(cacheValue), - expiration); + cacheKey, + _serializer.Serialize(cacheValue), + expiration); } /// @@ -251,7 +253,7 @@ public override async Task>> BaseGetAllAsync (RedisKey)k).ToArray()); + var values = await _cache.StringGetAsync(keyArray.Select(k => (RedisKey) k).ToArray()); var result = new Dictionary>(); for (int i = 0; i < keyArray.Length; i++) @@ -304,7 +306,7 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); - var redisKeys = cacheKeys.Where(k => !string.IsNullOrEmpty(k)).Select(k => (RedisKey)k).ToArray(); + var redisKeys = cacheKeys.Where(k => !string.IsNullOrEmpty(k)).Select(k => (RedisKey) k).ToArray(); if (redisKeys.Length > 0) await _cache.KeyDeleteAsync(redisKeys); } @@ -353,7 +355,7 @@ public override Task BaseTrySetAsync(string cacheKey, T cacheValue, Tim _serializer.Serialize(cacheValue), expiration, When.NotExists - ); + ); } /// diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs index 50a9fb22..34b074b3 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs @@ -107,7 +107,8 @@ public DefaultRedisCachingProvider( 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)); + : serializers.FirstOrDefault(x => x.Name.Equals(_name)) ?? + serializers.Single(x => x.Name.Equals(EasyCachingConstValue.DefaultSerializerName)); this.ProviderName = this._name; this.ProviderType = CachingProviderType.Redis; @@ -304,6 +305,11 @@ private RedisKey[] SearchRedisKeys(string pattern) // from this redis dev specification, https://yq.aliyun.com/articles/531067 , maybe the appropriate scope is 100~500, using 200 here. keys.AddRange(server.Keys(pattern: pattern, database: _cache.Database, pageSize: 200)); + if (!string.IsNullOrWhiteSpace(_options.DBConfig.KeyPrefix)) + keys = keys.Select(x => new RedisKey( + x.ToString().Remove(0, _options.DBConfig.KeyPrefix.Length))) + .ToList(); + return keys.Distinct().ToArray(); //var keys = new HashSet(); @@ -343,6 +349,9 @@ private string HandlePrefix(string prefix) if (!prefix.EndsWith("*", StringComparison.OrdinalIgnoreCase)) prefix = string.Concat(prefix, "*"); + if (!string.IsNullOrWhiteSpace(_options.DBConfig.KeyPrefix)) + prefix = _options.DBConfig.KeyPrefix + prefix; + return prefix; } @@ -376,7 +385,7 @@ public override IDictionary> BaseGetAll(IEnumerable (RedisKey)k).ToArray()); + var values = _cache.StringGet(keyArray.Select(k => (RedisKey) k).ToArray()); var result = new Dictionary>(); for (int i = 0; i < keyArray.Length; i++) @@ -428,7 +437,7 @@ public override void BaseRemoveAll(IEnumerable cacheKeys) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); - var redisKeys = cacheKeys.Where(k => !string.IsNullOrEmpty(k)).Select(k => (RedisKey)k).ToArray(); + var redisKeys = cacheKeys.Where(k => !string.IsNullOrEmpty(k)).Select(k => (RedisKey) k).ToArray(); if (redisKeys.Length > 0) _cache.KeyDelete(redisKeys); } @@ -445,7 +454,7 @@ public override int BaseGetCount(string prefix = "") var allCount = 0; foreach (var server in _servers) - allCount += (int)server.DatabaseSize(_cache.Database); + allCount += (int) server.DatabaseSize(_cache.Database); return allCount; } @@ -492,7 +501,7 @@ public override bool BaseTrySet(string cacheKey, T cacheValue, TimeSpan expir _serializer.Serialize(cacheValue), expiration, When.NotExists - ); + ); } /// @@ -516,4 +525,4 @@ public override TimeSpan BaseGetExpiration(string cacheKey) public override object BaseGetDatabse() => _cache; } -} +} \ No newline at end of file diff --git a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs index 4c963a57..83d226dc 100644 --- a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs @@ -27,7 +27,8 @@ protected override IEasyCachingProvider CreateCachingProvider(Action Date: Tue, 8 Mar 2022 20:33:37 +0100 Subject: [PATCH 4/7] added unit test for remove by prefix --- .../DefaultRedisCachingProvider.Async.cs | 6 ++--- .../CachingTests/RedisCachingProviderTest.cs | 26 +++++++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs index 57065d0b..c915449f 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs @@ -53,8 +53,7 @@ public override async Task BaseGetAsync(string cacheKey, Type type) /// Data retriever. /// Expiration. /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, - TimeSpan expiration) + public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); @@ -76,8 +75,7 @@ public override async Task> BaseGetAsync(string cacheKey, Func< if (_options.EnableLogging) _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); - var flag = await _cache.StringSetAsync($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs), - When.NotExists); + var flag = await _cache.StringSetAsync($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs), When.NotExists); if (!flag) { diff --git a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs index 83d226dc..6fe66aef 100644 --- a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs @@ -27,8 +27,7 @@ protected override IEasyCachingProvider CreateCachingProvider(Action("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); + } } } \ No newline at end of file From 2cea1d8ba5e111596c07fc279bf3f10ada3b34bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mark=20B=C3=B8g=20L=C3=B8nquist?= Date: Tue, 8 Mar 2022 20:36:02 +0100 Subject: [PATCH 5/7] revert some auto-formatting --- .../DefaultRedisCachingProvider.Async.cs | 14 +++++++------- .../DefaultRedisCachingProvider.cs | 11 +++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs index c915449f..89a7d137 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs @@ -144,7 +144,7 @@ public override Task BaseGetCountAsync(string prefix = "") var allCount = 0; foreach (var server in _servers) - allCount += (int) server.DatabaseSize(_cache.Database); + allCount += (int)server.DatabaseSize(_cache.Database); return Task.FromResult(allCount); } @@ -185,9 +185,9 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp } await _cache.StringSetAsync( - cacheKey, - _serializer.Serialize(cacheValue), - expiration); + cacheKey, + _serializer.Serialize(cacheValue), + expiration); } /// @@ -251,7 +251,7 @@ public override async Task>> BaseGetAllAsync (RedisKey) k).ToArray()); + var values = await _cache.StringGetAsync(keyArray.Select(k => (RedisKey)k).ToArray()); var result = new Dictionary>(); for (int i = 0; i < keyArray.Length; i++) @@ -304,7 +304,7 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); - var redisKeys = cacheKeys.Where(k => !string.IsNullOrEmpty(k)).Select(k => (RedisKey) k).ToArray(); + var redisKeys = cacheKeys.Where(k => !string.IsNullOrEmpty(k)).Select(k => (RedisKey)k).ToArray(); if (redisKeys.Length > 0) await _cache.KeyDeleteAsync(redisKeys); } @@ -353,7 +353,7 @@ public override Task BaseTrySetAsync(string cacheKey, T cacheValue, Tim _serializer.Serialize(cacheValue), expiration, When.NotExists - ); + ); } /// diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs index 34b074b3..857a9d44 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs @@ -107,8 +107,7 @@ public DefaultRedisCachingProvider( 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)); + : serializers.FirstOrDefault(x => x.Name.Equals(_name)) ?? serializers.Single(x => x.Name.Equals(EasyCachingConstValue.DefaultSerializerName)); this.ProviderName = this._name; this.ProviderType = CachingProviderType.Redis; @@ -385,7 +384,7 @@ public override IDictionary> BaseGetAll(IEnumerable (RedisKey) k).ToArray()); + var values = _cache.StringGet(keyArray.Select(k => (RedisKey)k).ToArray()); var result = new Dictionary>(); for (int i = 0; i < keyArray.Length; i++) @@ -437,7 +436,7 @@ public override void BaseRemoveAll(IEnumerable cacheKeys) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); - var redisKeys = cacheKeys.Where(k => !string.IsNullOrEmpty(k)).Select(k => (RedisKey) k).ToArray(); + var redisKeys = cacheKeys.Where(k => !string.IsNullOrEmpty(k)).Select(k => (RedisKey)k).ToArray(); if (redisKeys.Length > 0) _cache.KeyDelete(redisKeys); } @@ -454,7 +453,7 @@ public override int BaseGetCount(string prefix = "") var allCount = 0; foreach (var server in _servers) - allCount += (int) server.DatabaseSize(_cache.Database); + allCount += (int)server.DatabaseSize(_cache.Database); return allCount; } @@ -501,7 +500,7 @@ public override bool BaseTrySet(string cacheKey, T cacheValue, TimeSpan expir _serializer.Serialize(cacheValue), expiration, When.NotExists - ); + ); } /// From d8731082811b4b1228e51e351d2cf621ffd8b70b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mark=20B=C3=B8g=20L=C3=B8nquist?= Date: Tue, 8 Mar 2022 20:36:59 +0100 Subject: [PATCH 6/7] revert some auto-formatting --- src/EasyCaching.Redis/DefaultRedisCachingProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs index 857a9d44..215d39e6 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs @@ -524,4 +524,4 @@ public override TimeSpan BaseGetExpiration(string cacheKey) public override object BaseGetDatabse() => _cache; } -} \ No newline at end of file +} From aa7185100b2c3f515f51d5fa132e8a55c6be8ad9 Mon Sep 17 00:00:00 2001 From: catcherwong Date: Wed, 9 Mar 2022 08:51:46 +0800 Subject: [PATCH 7/7] docs: version++ --- build/releasenotes.props | 19 +++++++++---------- build/version.props | 40 ++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/build/releasenotes.props b/build/releasenotes.props index 4f57e2a4..93f2c552 100644 --- a/build/releasenotes.props +++ b/build/releasenotes.props @@ -1,20 +1,19 @@ - 1. Expose raw cache database. + 1. Upgrading dependencies. - 1. Expose raw cache database. + 1. Upgrading dependencies. - 1. Expose raw cache database. - 2. Support KeyPrefix. + 1. Fix removebyprefix when enable keyprefix. - 1. Expose raw cache database. + 1. Upgrading dependencies. - 1. Expose raw cache database. + 1. Upgrading dependencies. 1. Upgrading dependencies. @@ -38,7 +37,7 @@ 1. Upgrading dependencies. - 1. Support distributed lock. + 1. Upgrading dependencies. 1. Upgrading dependencies. @@ -47,13 +46,13 @@ 1. Upgrading dependencies. - 1. Upgrading dependencies. - + 1. Allow user to define client provided name. + 1. Upgrading dependencies. - 1. Expose raw cache database. + 1. Upgrading dependencies. 1. Upgrading dependencies. diff --git a/build/version.props b/build/version.props index 05037d68..2ad801b0 100644 --- a/build/version.props +++ b/build/version.props @@ -1,24 +1,24 @@ - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 - 1.5.0 + 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