From 64fb861b71f44637e9bd9d80edebeec4575f44f9 Mon Sep 17 00:00:00 2001 From: catcherwong Date: Sat, 30 Apr 2022 10:18:56 +0800 Subject: [PATCH 1/2] optimization: more clear exception message for not found (#359) --- build/version.props | 40 +++++++++---------- .../DefaultCSRedisCachingProvider.cs | 11 +++-- src/EasyCaching.Core/EasyCachingException.cs | 20 ++++++++++ .../Internal/EasyCachingConstValue.cs | 4 ++ .../DefaultLiteDBCachingProvider.cs | 4 +- .../DefaultMemcachedCachingProvider.cs | 4 +- .../DefaultRedisCachingProvider.cs | 11 +++-- 7 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 src/EasyCaching.Core/EasyCachingException.cs diff --git a/build/version.props b/build/version.props index e3ea48dd..8516dbfe 100644 --- a/build/version.props +++ b/build/version.props @@ -1,24 +1,24 @@ - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.3 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 - 1.5.2 + 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/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs index e9fa37b6..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; diff --git a/src/EasyCaching.Core/EasyCachingException.cs b/src/EasyCaching.Core/EasyCachingException.cs new file mode 100644 index 00000000..5b3a8b62 --- /dev/null +++ b/src/EasyCaching.Core/EasyCachingException.cs @@ -0,0 +1,20 @@ +namespace EasyCaching.Core +{ + using System; + + public class EasyCachingException : Exception + { + public EasyCachingException(string message) + : base(message) + { + } + } + + public class EasyCachingNotFoundException : Exception + { + public EasyCachingNotFoundException(string message) + : base(message) + { + } + } +} 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.cs b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs index 633f612c..244eeaab 100644 --- a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs +++ b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs @@ -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(); 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; From 2a3e49e91fc9fdbff04106035661c5eafe5ec9d4 Mon Sep 17 00:00:00 2001 From: catcherwong Date: Sat, 30 Apr 2022 11:02:33 +0800 Subject: [PATCH 2/2] test: fix serializer ut issue --- .../CSRedisCachingProviderTest.cs | 17 +------------- .../CSRedisFeatureCachingProviderTest.cs | 2 ++ .../CachingTests/HybridCachingTest.cs | 3 ++- .../CachingTests/RedisCachingProviderTest.cs | 23 ++++++------------- .../SERedisFeatureCachingProviderTest.cs | 2 +- .../DistributedLock/CSRedisLockTest.cs | 2 +- .../DistributedLock/RedisLockTest.cs | 2 +- .../ProviderFactoryTests.cs | 2 ++ 8 files changed, 17 insertions(+), 36 deletions(-) diff --git a/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/CSRedisCachingProviderTest.cs index 43eeed33..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,18 +91,6 @@ public CSRedisCachingProviderWithNamedSerTest() }, "cs2"); - option.UseCSRedis(config => - { - config.DBConfig = new CSRedisDBOptions - { - ConnectionStrings = new System.Collections.Generic.List - { - "127.0.0.1:6388,defaultDatabase=3,poolsize=10" - } - }; - - }, "cs3"); - option.WithJson("json").WithMessagePack("cs11").WithJson("cs2"); }); @@ -115,15 +103,12 @@ public void NamedSerializerTest() { var cs1 = _providerFactory.GetCachingProvider("cs1"); var cs2 = _providerFactory.GetCachingProvider("cs2"); - var cs3 = _providerFactory.GetCachingProvider("cs3"); var info1 = cs1.GetProviderInfo(); var info2 = cs2.GetProviderInfo(); - var info3 = cs3.GetProviderInfo(); Assert.Equal("cs11", info1.Serializer.Name); Assert.Equal("cs2", info2.Serializer.Name); - Assert.Equal(EasyCachingConstValue.DefaultSerializerName, info3.Serializer.Name); } } 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/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