Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization: more clear exception message for not found #362

Merged
merged 2 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions build/version.props
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<Project>
<PropertyGroup>
<EasyCachingCorePackageVersion>1.5.2</EasyCachingCorePackageVersion>
<EasyCachingMemcachedPackageVersion>1.5.2</EasyCachingMemcachedPackageVersion>
<EasyCachingRedisPackageVersion>1.5.2</EasyCachingRedisPackageVersion>
<EasyCachingSQLitePackageVersion>1.5.2</EasyCachingSQLitePackageVersion>
<EasyCachingInMemoryPackageVersion>1.5.2</EasyCachingInMemoryPackageVersion>
<EasyCachingHybridPackageVersion>1.5.2</EasyCachingHybridPackageVersion>
<EasyCachingAspectCorePackageVersion>1.5.2</EasyCachingAspectCorePackageVersion>
<EasyCachingCastlePackageVersion>1.5.2</EasyCachingCastlePackageVersion>
<EasyCachingResponseCachingPackageVersion>1.5.2</EasyCachingResponseCachingPackageVersion>
<EasyCachingJsonPackageVersion>1.5.2</EasyCachingJsonPackageVersion>
<EasyCachingMessagePackPackageVersion>1.5.2</EasyCachingMessagePackPackageVersion>
<EasyCachingProtobufPackageVersion>1.5.2</EasyCachingProtobufPackageVersion>
<EasyCachingCSRedisPackageVersion>1.5.3</EasyCachingCSRedisPackageVersion>
<EasyCachingRedisBusPackageVersion>1.5.2</EasyCachingRedisBusPackageVersion>
<EasyCachingCSRedisBusPackageVersion>1.5.2</EasyCachingCSRedisBusPackageVersion>
<EasyCachingRabbitBusPackageVersion>1.5.2</EasyCachingRabbitBusPackageVersion>
<EasyCachingDiskPackageVersion>1.5.2</EasyCachingDiskPackageVersion>
<EasyCachingMsExtPackageVersion>1.5.2</EasyCachingMsExtPackageVersion>
<EasyCachingLiteDBPackageVersion>1.5.2</EasyCachingLiteDBPackageVersion>
<EasyCachingSTJsonPackageVersion>1.5.2</EasyCachingSTJsonPackageVersion>
<EasyCachingCorePackageVersion>1.6.0</EasyCachingCorePackageVersion>
<EasyCachingMemcachedPackageVersion>1.6.0</EasyCachingMemcachedPackageVersion>
<EasyCachingRedisPackageVersion>1.6.0</EasyCachingRedisPackageVersion>
<EasyCachingSQLitePackageVersion>1.6.0</EasyCachingSQLitePackageVersion>
<EasyCachingInMemoryPackageVersion>1.6.0</EasyCachingInMemoryPackageVersion>
<EasyCachingHybridPackageVersion>1.6.0</EasyCachingHybridPackageVersion>
<EasyCachingAspectCorePackageVersion>1.6.0</EasyCachingAspectCorePackageVersion>
<EasyCachingCastlePackageVersion>1.6.0</EasyCachingCastlePackageVersion>
<EasyCachingResponseCachingPackageVersion>1.6.0</EasyCachingResponseCachingPackageVersion>
<EasyCachingJsonPackageVersion>1.6.0</EasyCachingJsonPackageVersion>
<EasyCachingMessagePackPackageVersion>1.6.0</EasyCachingMessagePackPackageVersion>
<EasyCachingProtobufPackageVersion>1.6.0</EasyCachingProtobufPackageVersion>
<EasyCachingCSRedisPackageVersion>1.6.0</EasyCachingCSRedisPackageVersion>
<EasyCachingRedisBusPackageVersion>1.6.0</EasyCachingRedisBusPackageVersion>
<EasyCachingCSRedisBusPackageVersion>1.6.0</EasyCachingCSRedisBusPackageVersion>
<EasyCachingRabbitBusPackageVersion>1.6.0</EasyCachingRabbitBusPackageVersion>
<EasyCachingDiskPackageVersion>1.6.0</EasyCachingDiskPackageVersion>
<EasyCachingMsExtPackageVersion>1.6.0</EasyCachingMsExtPackageVersion>
<EasyCachingLiteDBPackageVersion>1.6.0</EasyCachingLiteDBPackageVersion>
<EasyCachingSTJsonPackageVersion>1.6.0</EasyCachingSTJsonPackageVersion>
</PropertyGroup>
</Project>
11 changes: 7 additions & 4 deletions src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ public DefaultCSRedisCachingProvider(
this._name = name;
this._options = options;
this._logger = loggerFactory?.CreateLogger<DefaultCSRedisCachingProvider>();
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;
Expand Down
20 changes: 20 additions & 0 deletions src/EasyCaching.Core/EasyCachingException.cs
Original file line number Diff line number Diff line change
@@ -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)
{
}
}
}
4 changes: 4 additions & 0 deletions src/EasyCaching.Core/Internal/EasyCachingConstValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,9 @@ public class EasyCachingConstValue
/// The LiteDB Bus section.
/// </summary>
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}";
}
}
4 changes: 3 additions & 1 deletion src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultLiteDBCachingProvider>();
this._litedb = _dbProvider.GetConnection();
Expand Down
4 changes: 3 additions & 1 deletion src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultMemcachedCachingProvider>();
this._cacheStats = new CacheStats();
Expand Down
11 changes: 7 additions & 4 deletions src/EasyCaching.Redis/DefaultRedisCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultRedisCachingProvider>();
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override IEasyCachingProvider CreateCachingProvider(Action<BaseProvide
}
};
additionalSetup(options);
}).UseCSRedisLock());
}).UseCSRedisLock().WithJson(EasyCachingConstValue.DefaultCSRedisName));

IServiceProvider serviceProvider = services.BuildServiceProvider();
return serviceProvider.GetService<IEasyCachingProvider>();
Expand Down Expand Up @@ -91,18 +91,6 @@ public CSRedisCachingProviderWithNamedSerTest()

}, "cs2");

option.UseCSRedis(config =>
{
config.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,defaultDatabase=3,poolsize=10"
}
};

}, "cs3");

option.WithJson("json").WithMessagePack("cs11").WithJson("cs2");
});

Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public CSRedisFeatureCachingProviderTest()
IServiceCollection services = new ServiceCollection();
services.AddEasyCaching(option =>
{
option.WithJson("ser");
option.UseCSRedis(config =>
{
config.DBConfig = new CSRedisDBOptions
Expand All @@ -21,6 +22,7 @@ public CSRedisFeatureCachingProviderTest()
"127.0.0.1:6388,defaultDatabase=10,poolsize=10"
}
};
config.SerializerName = "ser";
});
});

Expand Down
3 changes: 2 additions & 1 deletion test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
{
Expand All @@ -63,6 +63,7 @@ public HybridCachingTest()
{
config.Endpoints.Add(new Core.Configurations.ServerEndPoint("127.0.0.1", 6379));
config.Database = 6;
config.SerializerName = "myredis";
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override IEasyCachingProvider CreateCachingProvider(Action<BaseProvide
options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380));
options.DBConfig.Database = 5;
additionalSetup(options);
}, ProviderName).UseRedisLock());
}, ProviderName).UseRedisLock().WithJson(ProviderName));
IServiceProvider serviceProvider = services.BuildServiceProvider();
return serviceProvider.GetService<IEasyCachingProvider>();
}
Expand All @@ -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<IEasyCachingProviderFactory>();
Expand Down Expand Up @@ -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<IRedisDatabaseProvider>();
Assert.NotNull(dbProvider);
Expand Down Expand Up @@ -122,6 +122,8 @@ public RedisCachingProviderWithFactoryTest()
IServiceCollection services = new ServiceCollection();
services.AddEasyCaching(x =>
{
x.WithJson("ser");

x.UseRedis(options =>
{
options.DBConfig = new RedisDBOptions
Expand All @@ -130,6 +132,7 @@ public RedisCachingProviderWithFactoryTest()
};
options.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380));
options.DBConfig.Database = 3;
options.SerializerName = "ser";
});


Expand All @@ -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();
Expand Down Expand Up @@ -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");
});

Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CSRedisLockTest : DistributedLockTest
}
};
})
.UseCSRedisLock())
.UseCSRedisLock().WithJson(EasyCachingConstValue.DefaultCSRedisName))
.BuildServiceProvider()
.GetService<CSRedisLockFactory>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class RedisLockTest : DistributedLockTest
}
};
})
.UseRedisLock())
.UseRedisLock().WithJson(EasyCachingConstValue.DefaultRedisName))
.BuildServiceProvider()
.GetService<RedisLockFactory>();

Expand Down
2 changes: 2 additions & 0 deletions test/EasyCaching.UnitTests/ProviderFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down