diff --git a/build/releasenotes.props b/build/releasenotes.props index 0f080438..1469ec70 100644 --- a/build/releasenotes.props +++ b/build/releasenotes.props @@ -1,22 +1,22 @@ - 1. TrySet/TrySetAsync. + 1. Improve Configuration. - 1. TrySet/TrySetAsync. + 1. Improve Configuration. - 1. TrySet/TrySetAsync. + 1. Improve Configuration. - 1. TrySet/TrySetAsync. + 1. Improve Configuration. - 1. Fix bug of TrySet/TrySetAsync. + 1. Improve Configuration. - 1. TrySet/TrySetAsync. + 1. Improve Configuration. 1. Remove Dependency of IEasyCaching. @@ -28,13 +28,13 @@ 1. Support .NET Core 2.1 - 1. Configurable JSON serializer + 1. Improve Configuration. - 1. Support .NET Core 2.1 + 1. Improve Configuration. - 1. Support .NET Core 2.1 + 1. Improve Configuration. diff --git a/build/version.props b/build/version.props index 193e739d..18259723 100644 --- a/build/version.props +++ b/build/version.props @@ -1,16 +1,16 @@ - 0.4.2 - 0.4.2 - 0.4.2 - 0.4.2 - 0.4.3 - 0.4.2 + 0.4.5 + 0.4.5 + 0.4.5 + 0.4.5 + 0.4.5 + 0.4.5 0.3.2 0.3.2 0.3.0 - 0.3.1 - 0.3.0 - 0.3.0 + 0.3.5 + 0.3.5 + 0.3.5 diff --git a/sample/EasyCaching.Demo.Providers/Startup.cs b/sample/EasyCaching.Demo.Providers/Startup.cs index e4a6562b..123ce059 100644 --- a/sample/EasyCaching.Demo.Providers/Startup.cs +++ b/sample/EasyCaching.Demo.Providers/Startup.cs @@ -7,6 +7,7 @@ using EasyCaching.Memcached; using EasyCaching.Redis; using EasyCaching.SQLite; + using EasyCaching.Serialization.MessagePack; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; @@ -28,11 +29,50 @@ public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + //new configuration + services.AddEasyCaching(option=> + { + //use memory cache + option.UseInMemory("default"); + + //use memory cache + option.UseInMemory("cus"); + + //use redis cache + option.UseRedis(config => + { + config.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379)); + }, "redis1") + .WithMessagePack()//with messagepack serialization + ; + + //use redis cache + option.UseRedis(config => + { + config.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); + }, "redis2"); + + ////use sqlite cache + //option.UseSQLite(config => + //{ + // config.DBConfig = new SQLiteDBOptions { FileName = "my.db" }; + //}); + + ////use memcached cached + //option.UseMemcached(config => + //{ + // config.DBConfig.AddServer("127.0.0.1", 11211); + //}); + + //option.UseMemcached(Configuration); + + }); + //1. Important step for using InMemory Cache //services.AddDefaultInMemoryCache(); //services.AddDefaultInMemoryCacheWithFactory(); - services.AddDefaultInMemoryCacheWithFactory("cus"); + //services.AddDefaultInMemoryCacheWithFactory("cus"); //services.AddDefaultInMemoryCache(Configuration); @@ -51,17 +91,17 @@ public void ConfigureServices(IServiceCollection services) // option.DBConfig.Password = ""; //}); - services.AddDefaultRedisCacheWithFactory("redis1",option => - { - option.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379)); - option.DBConfig.Password = ""; - }); + //services.AddDefaultRedisCacheWithFactory("redis1",option => + //{ + // option.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379)); + // option.DBConfig.Password = ""; + //}); - services.AddDefaultRedisCacheWithFactory("redis2", option => - { - option.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); - option.DBConfig.Password = ""; - }); + //services.AddDefaultRedisCacheWithFactory("redis2", option => + //{ + // option.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6380)); + // option.DBConfig.Password = ""; + //}); //services.AddDefaultRedisCache(Configuration); @@ -105,6 +145,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF ////4. Important step for using SQLite Cache //app.UseSQLiteCache(); + //app.UseEasyCaching(); + app.UseMvc(); } } diff --git a/src/EasyCaching.Core/Configurations/EasyCachingApplicationBuliderExtensions.cs b/src/EasyCaching.Core/Configurations/EasyCachingApplicationBuliderExtensions.cs new file mode 100644 index 00000000..4929d1dd --- /dev/null +++ b/src/EasyCaching.Core/Configurations/EasyCachingApplicationBuliderExtensions.cs @@ -0,0 +1,33 @@ +namespace EasyCaching.Core +{ + using Microsoft.AspNetCore.Builder; + using System; + + public static class EasyCachingApplicationBuliderExtensions + { + /// + /// Uses the easy caching. + /// + /// The easy caching. + /// App. + public static IApplicationBuilder UseEasyCaching(this IApplicationBuilder app) + { + if (app == null) + { + throw new ArgumentNullException(nameof(app)); + } + + var options = app.ApplicationServices.GetService(typeof(EasyCachingOptions)); + + if (options != null && options is EasyCachingOptions) + { + foreach (var serviceExtension in ((EasyCachingOptions)options).Extensions) + { + serviceExtension.WithServices(app); + } + } + + return app; + } + } +} diff --git a/src/EasyCaching.Core/Configurations/EasyCachingOptions.cs b/src/EasyCaching.Core/Configurations/EasyCachingOptions.cs new file mode 100644 index 00000000..09bac17a --- /dev/null +++ b/src/EasyCaching.Core/Configurations/EasyCachingOptions.cs @@ -0,0 +1,35 @@ +namespace EasyCaching.Core +{ + using System.Collections.Generic; + + /// + /// EasyCaching options. + /// + public class EasyCachingOptions + { + /// + /// Initializes a new instance of the class. + /// + public EasyCachingOptions() + { + Extensions = new List(); + } + + /// + /// Gets the extensions. + /// + /// The extensions. + internal IList Extensions { get; } + + /// + /// Registers the extension. + /// + /// Extension. + public void RegisterExtension(IEasyCachingOptionsExtension extension) + { + Internal.ArgumentCheck.NotNull(extension, nameof(extension)); + + Extensions.Add(extension); + } + } +} diff --git a/src/EasyCaching.Core/Configurations/EasyCachingServiceCollectionExtensions.cs b/src/EasyCaching.Core/Configurations/EasyCachingServiceCollectionExtensions.cs new file mode 100644 index 00000000..b6c5f856 --- /dev/null +++ b/src/EasyCaching.Core/Configurations/EasyCachingServiceCollectionExtensions.cs @@ -0,0 +1,36 @@ +namespace EasyCaching.Core +{ + using Microsoft.Extensions.DependencyInjection; + using System; + + /// + /// EasyCaching service collection extensions. + /// + public static class EasyCachingServiceCollectionExtensions + { + /// + /// Adds the easycaching. + /// + /// The easy caching. + /// Services. + /// Setup action. + public static IServiceCollection AddEasyCaching(this IServiceCollection services, Action setupAction) + { + if (setupAction == null) + { + throw new ArgumentNullException(nameof(setupAction)); + } + + //Options and extension service + var options = new EasyCachingOptions(); + setupAction(options); + foreach (var serviceExtension in options.Extensions) + { + serviceExtension.AddServices(services); + } + services.AddSingleton(options); + + return services; + } + } +} diff --git a/src/EasyCaching.Core/Configurations/IEasyCachingOptionsExtension.cs b/src/EasyCaching.Core/Configurations/IEasyCachingOptionsExtension.cs new file mode 100644 index 00000000..e864df0e --- /dev/null +++ b/src/EasyCaching.Core/Configurations/IEasyCachingOptionsExtension.cs @@ -0,0 +1,23 @@ +namespace EasyCaching.Core +{ + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + + /// + /// EasyCaching options extension. + /// + public interface IEasyCachingOptionsExtension + { + /// + /// Adds the services. + /// + /// Services. + void AddServices(IServiceCollection services); + + /// + /// Withs the services. + /// + /// App. + void WithServices(IApplicationBuilder app); + } +} diff --git a/src/EasyCaching.Core/EasyCaching.Core.csproj b/src/EasyCaching.Core/EasyCaching.Core.csproj index 1a46560b..cafa2054 100644 --- a/src/EasyCaching.Core/EasyCaching.Core.csproj +++ b/src/EasyCaching.Core/EasyCaching.Core.csproj @@ -22,11 +22,10 @@ + - - - - + + diff --git a/src/EasyCaching.Core/IEasyCachingProviderFactory.cs b/src/EasyCaching.Core/IEasyCachingProviderFactory.cs index dd5094a9..1badf221 100644 --- a/src/EasyCaching.Core/IEasyCachingProviderFactory.cs +++ b/src/EasyCaching.Core/IEasyCachingProviderFactory.cs @@ -21,7 +21,7 @@ public IEasyCachingProvider GetCachingProvider(string name) { Internal.ArgumentCheck.NotNullOrWhiteSpace(name, nameof(name)); - var provider = _cachingProviders.FirstOrDefault(x => x.Name.Equals(name)); + var provider = _cachingProviders.FirstOrDefault(x => x.Name.Equals(name, System.StringComparison.OrdinalIgnoreCase)); if (provider == null) throw new System.ArgumentException("can not find a match caching provider!"); diff --git a/src/EasyCaching.HybridCache/Configurations/EasyCachingOptionsExtensions.cs b/src/EasyCaching.HybridCache/Configurations/EasyCachingOptionsExtensions.cs new file mode 100644 index 00000000..d66a64ed --- /dev/null +++ b/src/EasyCaching.HybridCache/Configurations/EasyCachingOptionsExtensions.cs @@ -0,0 +1,22 @@ +namespace EasyCaching.HybridCache +{ + using EasyCaching.Core; + + /// + /// EasyCaching options extensions. + /// + public static class EasyCachingOptionsExtensions + { + /// + /// Uses the hybrid. + /// + /// The hybrid. + /// Options. + public static EasyCachingOptions UseHybrid(this EasyCachingOptions options) + { + options.RegisterExtension(new HybridCacheOptionsExtension()); + + return options; + } + } +} diff --git a/src/EasyCaching.HybridCache/Configurations/HybridCacheOptionsExtension.cs b/src/EasyCaching.HybridCache/Configurations/HybridCacheOptionsExtension.cs new file mode 100644 index 00000000..cfb10801 --- /dev/null +++ b/src/EasyCaching.HybridCache/Configurations/HybridCacheOptionsExtension.cs @@ -0,0 +1,31 @@ +namespace EasyCaching.HybridCache +{ + using EasyCaching.Core; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.DependencyInjection.Extensions; + + /// + /// HybridCache options extension. + /// + internal sealed class HybridCacheOptionsExtension : IEasyCachingOptionsExtension + { + /// + /// Adds the services. + /// + /// Services. + public void AddServices(IServiceCollection services) + { + services.TryAddSingleton(); + } + + /// + /// Withs the services. + /// + /// Services. + public void WithServices(IApplicationBuilder services) + { + // Method intentionally left empty. + } + } +} diff --git a/src/EasyCaching.HybridCache/EasyCaching.HybridCache.csproj b/src/EasyCaching.HybridCache/EasyCaching.HybridCache.csproj index 165ef5f3..3eef1a2d 100644 --- a/src/EasyCaching.HybridCache/EasyCaching.HybridCache.csproj +++ b/src/EasyCaching.HybridCache/EasyCaching.HybridCache.csproj @@ -26,4 +26,7 @@ + + + diff --git a/src/EasyCaching.InMemory/Configurations/EasyCachingOptionsExtensions.cs b/src/EasyCaching.InMemory/Configurations/EasyCachingOptionsExtensions.cs new file mode 100644 index 00000000..2c490188 --- /dev/null +++ b/src/EasyCaching.InMemory/Configurations/EasyCachingOptionsExtensions.cs @@ -0,0 +1,80 @@ +namespace EasyCaching.InMemory +{ + using EasyCaching.Core; + using Microsoft.Extensions.Configuration; + using System; + + /// + /// EasyCaching options extensions of InMemory. + /// + public static class EasyCachingOptionsExtensions + { + /// + /// Uses the in memory. + /// + /// The in memory. + /// Options. + /// Name. + public static EasyCachingOptions UseInMemory(this EasyCachingOptions options, string name = "") + { + var option = new InMemoryOptions(); + + void configure(InMemoryOptions x) + { + x.CachingProviderType = option.CachingProviderType; + x.EnableLogging = option.EnableLogging; + x.MaxRdSecond = option.MaxRdSecond; + x.Order = option.Order; + } + + options.RegisterExtension(new InMemoryOptionsExtension(name, configure)); + + return options; + } + + /// + /// Uses the in memory. + /// + /// The in memory. + /// Options. + /// Configure. + /// Name. + public static EasyCachingOptions UseInMemory(this EasyCachingOptions options, Action configure, string name = "") + { + if (configure == null) + { + throw new ArgumentNullException(nameof(configure)); + } + + options.RegisterExtension(new InMemoryOptionsExtension(name, configure)); + + return options; + } + + /// + /// Uses the in memory. + /// + /// The in memory. + /// Options. + /// Configuration. + /// Name. + public static EasyCachingOptions UseInMemory(this EasyCachingOptions options, IConfiguration configuration, string name = "") + { + var dbConfig = configuration.GetSection(EasyCachingConstValue.InMemorySection); + var memoryOptions = new InMemoryOptions(); + dbConfig.Bind(memoryOptions); + + void configure(InMemoryOptions x) + { + x.CachingProviderType = memoryOptions.CachingProviderType; + x.EnableLogging = memoryOptions.EnableLogging; + x.MaxRdSecond = memoryOptions.MaxRdSecond; + x.Order = memoryOptions.Order; + } + + options.RegisterExtension(new InMemoryOptionsExtension(name, configure)); + + return options; + } + } +} diff --git a/src/EasyCaching.InMemory/InMemoryOptions.cs b/src/EasyCaching.InMemory/Configurations/InMemoryOptions.cs similarity index 100% rename from src/EasyCaching.InMemory/InMemoryOptions.cs rename to src/EasyCaching.InMemory/Configurations/InMemoryOptions.cs diff --git a/src/EasyCaching.InMemory/Configurations/InMemoryOptionsExtension.cs b/src/EasyCaching.InMemory/Configurations/InMemoryOptionsExtension.cs new file mode 100644 index 00000000..d65b2f25 --- /dev/null +++ b/src/EasyCaching.InMemory/Configurations/InMemoryOptionsExtension.cs @@ -0,0 +1,70 @@ +namespace EasyCaching.InMemory +{ + using EasyCaching.Core; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + using System; + + /// + /// InMemory options extension. + /// + internal sealed class InMemoryOptionsExtension : IEasyCachingOptionsExtension + { + /// + /// The name. + /// + private readonly string _name; + /// + /// The configure. + /// + private readonly Action configure; + + /// + /// Initializes a new instance of the class. + /// + /// Name. + /// Configure. + public InMemoryOptionsExtension(string name, Action configure) + { + this._name = name; + this.configure = configure; + } + + /// + /// Adds the services. + /// + /// Services. + public void AddServices(IServiceCollection services) + { + services.AddOptions(); + services.Configure(configure); + services.AddMemoryCache(); + + if (string.IsNullOrWhiteSpace(_name)) + { + services.AddSingleton(); + } + else + { + services.AddSingleton(); + services.AddSingleton(x => + { + var mCache = x.GetRequiredService(); + var options = x.GetRequiredService>(); + //ILoggerFactory can be null + var factory = x.GetService(); + return new DefaultInMemoryCachingProvider(_name, mCache, options, factory); + }); + } + } + + /// + /// Withs the services. + /// + /// Services. + public void WithServices(IApplicationBuilder services) + { + // Method intentionally left empty. + } + } +} \ No newline at end of file diff --git a/src/EasyCaching.InMemory/EasyCaching.InMemory.csproj b/src/EasyCaching.InMemory/EasyCaching.InMemory.csproj index 57b1acdb..41f398fe 100644 --- a/src/EasyCaching.InMemory/EasyCaching.InMemory.csproj +++ b/src/EasyCaching.InMemory/EasyCaching.InMemory.csproj @@ -28,4 +28,7 @@ + + + diff --git a/src/EasyCaching.Memcached/EasyCachingMemcachedClient.cs b/src/EasyCaching.Memcached/Configurations/EasyCachingMemcachedClient.cs similarity index 100% rename from src/EasyCaching.Memcached/EasyCachingMemcachedClient.cs rename to src/EasyCaching.Memcached/Configurations/EasyCachingMemcachedClient.cs diff --git a/src/EasyCaching.Memcached/EasyCachingMemcachedClientConfiguration.cs b/src/EasyCaching.Memcached/Configurations/EasyCachingMemcachedClientConfiguration.cs similarity index 100% rename from src/EasyCaching.Memcached/EasyCachingMemcachedClientConfiguration.cs rename to src/EasyCaching.Memcached/Configurations/EasyCachingMemcachedClientConfiguration.cs diff --git a/src/EasyCaching.Memcached/EasyCachingMemcachedClientOptions.cs b/src/EasyCaching.Memcached/Configurations/EasyCachingMemcachedClientOptions.cs similarity index 100% rename from src/EasyCaching.Memcached/EasyCachingMemcachedClientOptions.cs rename to src/EasyCaching.Memcached/Configurations/EasyCachingMemcachedClientOptions.cs diff --git a/src/EasyCaching.Memcached/Configurations/EasyCachingOptionsExtensions.cs b/src/EasyCaching.Memcached/Configurations/EasyCachingOptionsExtensions.cs new file mode 100644 index 00000000..075da45b --- /dev/null +++ b/src/EasyCaching.Memcached/Configurations/EasyCachingOptionsExtensions.cs @@ -0,0 +1,57 @@ +namespace EasyCaching.Memcached +{ + using EasyCaching.Core; + using Microsoft.Extensions.Configuration; + using System; + + /// + /// EasyCaching options extensions. + /// + public static class EasyCachingOptionsExtensions + { + /// + /// Uses the memcached. + /// + /// The memcached. + /// Options. + /// Configure. + /// Name. + public static EasyCachingOptions UseMemcached(this EasyCachingOptions options, Action configure, string name = "") + { + if (configure == null) + { + throw new ArgumentNullException(nameof(configure)); + } + + options.RegisterExtension(new MemcachedOptionsExtension(name, configure)); + return options; + } + + /// + /// Uses the memcached. + /// + /// The memcached. + /// Options. + /// Configuration. + /// Name. + /// Section name. + public static EasyCachingOptions UseMemcached(this EasyCachingOptions options, IConfiguration configuration, string name = "", string sectionName = EasyCachingConstValue.MemcachedSection) + { + var dbConfig = configuration.GetSection(sectionName); + var mOptions = new MemcachedOptions(); + dbConfig.Bind(mOptions); + + void configure(MemcachedOptions x) + { + x.CachingProviderType = mOptions.CachingProviderType; + x.EnableLogging = mOptions.EnableLogging; + x.MaxRdSecond = mOptions.MaxRdSecond; + x.Order = mOptions.Order; + x.DBConfig = mOptions.DBConfig; + } + + options.RegisterExtension(new MemcachedOptionsExtension(name, configure)); + return options; + } + } +} diff --git a/src/EasyCaching.Memcached/EasyCachingTranscoder.cs b/src/EasyCaching.Memcached/Configurations/EasyCachingTranscoder.cs similarity index 100% rename from src/EasyCaching.Memcached/EasyCachingTranscoder.cs rename to src/EasyCaching.Memcached/Configurations/EasyCachingTranscoder.cs diff --git a/src/EasyCaching.Memcached/MemcachedOptions.cs b/src/EasyCaching.Memcached/Configurations/MemcachedOptions.cs similarity index 100% rename from src/EasyCaching.Memcached/MemcachedOptions.cs rename to src/EasyCaching.Memcached/Configurations/MemcachedOptions.cs diff --git a/src/EasyCaching.Memcached/Configurations/MemcachedOptionsExtension.cs b/src/EasyCaching.Memcached/Configurations/MemcachedOptionsExtension.cs new file mode 100644 index 00000000..0e7d2b5b --- /dev/null +++ b/src/EasyCaching.Memcached/Configurations/MemcachedOptionsExtension.cs @@ -0,0 +1,134 @@ +namespace EasyCaching.Memcached +{ + using EasyCaching.Core; + using Enyim.Caching; + using Enyim.Caching.Configuration; + using Enyim.Caching.Memcached; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.DependencyInjection.Extensions; + using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; + using System; + using System.Linq; + + /// + /// Memcached options extension. + /// + internal sealed class MemcachedOptionsExtension : IEasyCachingOptionsExtension + { + /// + /// The name. + /// + private readonly string _name; + /// + /// The configure. + /// + private readonly Action configure; + + /// + /// Initializes a new instance of the class. + /// + /// Name. + /// Configure. + public MemcachedOptionsExtension(string name, Action configure) + { + this._name = name; + this.configure = configure; + } + + /// + /// Adds the services. + /// + /// Services. + public void AddServices(IServiceCollection services) + { + services.AddOptions(); + + if (string.IsNullOrWhiteSpace(_name)) + { + services.Configure(configure); + + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.AddSingleton(x => + { + var options = x.GetRequiredService>(); + var loggerFactory = x.GetRequiredService(); + var transcoder = x.GetRequiredService(); + var transformer = x.GetRequiredService(); + return new EasyCachingMemcachedClientConfiguration(loggerFactory, options, transcoder, transformer); + }); + + services.AddSingleton(x => + { + var loggerFactory = x.GetRequiredService(); + var config = x.GetRequiredService(); + return new EasyCachingMemcachedClient(EasyCachingConstValue.DefaultMemcachedName, loggerFactory, config); + }); + + services.AddSingleton(); + } + else + { + services.Configure(_name, configure); + + services.AddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.AddSingleton(x => + { + var optionsMon = x.GetRequiredService>(); + var options = optionsMon.Get(_name); + var loggerFactory = x.GetRequiredService(); + var transcoder = x.GetRequiredService(); + var transformer = x.GetRequiredService(); + return new EasyCachingMemcachedClientConfiguration(_name, loggerFactory, options, transcoder, transformer); + }); + + services.AddSingleton(x => + { + var loggerFactory = x.GetRequiredService(); + var configs = x.GetServices(); + var config = configs.FirstOrDefault(y => y.Name.Equals(_name)); + return new EasyCachingMemcachedClient(_name, loggerFactory, config); + }); + + services.AddSingleton(x => + { + var clients = x.GetServices(); + var optionsMon = x.GetRequiredService>(); + var options = optionsMon.Get(_name); + var factory = x.GetService(); + return new DefaultMemcachedCachingProvider(_name, clients, options, factory); + }); + } + } + + /// + /// Withs the services. + /// + /// App. + public void WithServices(IApplicationBuilder app) + { + try + { + var clients = app.ApplicationServices.GetServices(); + + foreach (var client in clients) + { + client.GetAsync("EnyimMemcached").Wait(); + } + + Console.WriteLine("EnyimMemcached Started."); + } + catch (Exception ex) + { + app.ApplicationServices.GetService>() + .LogError(new EventId(), ex, "EnyimMemcached Failed."); + } + } + } +} diff --git a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs index 9e14ed4f..00710dcf 100644 --- a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs +++ b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs @@ -106,12 +106,12 @@ public DefaultMemcachedCachingProvider( public DefaultMemcachedCachingProvider( string name, IEnumerable memcachedClients, - IOptionsMonitor options, + MemcachedOptions options, ILoggerFactory loggerFactory = null) { this._name = name; this._memcachedClient = memcachedClients.FirstOrDefault(x => x.Name.Equals(this._name)); - this._options = options.CurrentValue; + this._options = options; this._logger = loggerFactory?.CreateLogger(); this._cacheStats = new CacheStats(); diff --git a/src/EasyCaching.Memcached/EasyCaching.Memcached.csproj b/src/EasyCaching.Memcached/EasyCaching.Memcached.csproj index 8c42d049..c791faa0 100644 --- a/src/EasyCaching.Memcached/EasyCaching.Memcached.csproj +++ b/src/EasyCaching.Memcached/EasyCaching.Memcached.csproj @@ -26,4 +26,7 @@ + + + diff --git a/src/EasyCaching.Memcached/MemcachedServiceCollectionExtensions.cs b/src/EasyCaching.Memcached/MemcachedServiceCollectionExtensions.cs index dc3bfc22..9ce1f37d 100644 --- a/src/EasyCaching.Memcached/MemcachedServiceCollectionExtensions.cs +++ b/src/EasyCaching.Memcached/MemcachedServiceCollectionExtensions.cs @@ -166,7 +166,8 @@ public static IServiceCollection AddDefaultMemcachedWithFactory( services.AddSingleton(x => { var clients = x.GetServices(); - var options = x.GetRequiredService>(); + var optionsMon = x.GetRequiredService>(); + var options = optionsMon.Get(providerName); var factory = x.GetService(); return new DefaultMemcachedCachingProvider(providerName, clients, options, factory); }); @@ -220,7 +221,8 @@ public static IServiceCollection AddDefaultMemcachedWithFactory( services.AddSingleton(x => { var clients = x.GetServices(); - var options = x.GetRequiredService>(); + var optionsMon = x.GetRequiredService>(); + var options = optionsMon.Get(providerName); var factory = x.GetService(); return new DefaultMemcachedCachingProvider(providerName, clients, options, factory); }); diff --git a/src/EasyCaching.Redis/Configurations/EasyCachingOptionsExtensions.cs b/src/EasyCaching.Redis/Configurations/EasyCachingOptionsExtensions.cs new file mode 100644 index 00000000..ae2c966c --- /dev/null +++ b/src/EasyCaching.Redis/Configurations/EasyCachingOptionsExtensions.cs @@ -0,0 +1,56 @@ +namespace EasyCaching.Redis +{ + using EasyCaching.Core; + using Microsoft.Extensions.Configuration; + using System; + + /// + /// EasyCaching options extensions. + /// + public static class EasyCachingOptionsExtensions + { + /// + /// Uses the redis. + /// + /// The redis. + /// Options. + /// Configure. + /// Name. + public static EasyCachingOptions UseRedis(this EasyCachingOptions options, Action configure, string name = "") + { + if (configure == null) + { + throw new ArgumentNullException(nameof(configure)); + } + + options.RegisterExtension(new RedisOptionsExtension(name, configure)); + return options; + } + + /// + /// Uses the redis. + /// + /// The redis. + /// Options. + /// Configuration. + /// Name. + public static EasyCachingOptions UseRedis(this EasyCachingOptions options, IConfiguration configuration, string name = "") + { + var dbConfig = configuration.GetSection(EasyCachingConstValue.InMemorySection); + var redisOptions = new RedisOptions(); + dbConfig.Bind(redisOptions); + + void configure(RedisOptions x) + { + x.CachingProviderType = redisOptions.CachingProviderType; + x.EnableLogging = redisOptions.EnableLogging; + x.MaxRdSecond = redisOptions.MaxRdSecond; + x.Order = redisOptions.Order; + x.DBConfig = redisOptions.DBConfig; + } + + options.RegisterExtension(new RedisOptionsExtension(name, configure)); + return options; + } + } +} diff --git a/src/EasyCaching.Redis/IRedisDatabaseProvider.cs b/src/EasyCaching.Redis/Configurations/IRedisDatabaseProvider.cs similarity index 100% rename from src/EasyCaching.Redis/IRedisDatabaseProvider.cs rename to src/EasyCaching.Redis/Configurations/IRedisDatabaseProvider.cs diff --git a/src/EasyCaching.Redis/RedisDBOptions.cs b/src/EasyCaching.Redis/Configurations/RedisDBOptions.cs similarity index 100% rename from src/EasyCaching.Redis/RedisDBOptions.cs rename to src/EasyCaching.Redis/Configurations/RedisDBOptions.cs diff --git a/src/EasyCaching.Redis/RedisDatabaseProvider.cs b/src/EasyCaching.Redis/Configurations/RedisDatabaseProvider.cs similarity index 100% rename from src/EasyCaching.Redis/RedisDatabaseProvider.cs rename to src/EasyCaching.Redis/Configurations/RedisDatabaseProvider.cs diff --git a/src/EasyCaching.Redis/RedisOptions.cs b/src/EasyCaching.Redis/Configurations/RedisOptions.cs similarity index 100% rename from src/EasyCaching.Redis/RedisOptions.cs rename to src/EasyCaching.Redis/Configurations/RedisOptions.cs diff --git a/src/EasyCaching.Redis/Configurations/RedisOptionsExtension.cs b/src/EasyCaching.Redis/Configurations/RedisOptionsExtension.cs new file mode 100644 index 00000000..62d667ad --- /dev/null +++ b/src/EasyCaching.Redis/Configurations/RedisOptionsExtension.cs @@ -0,0 +1,85 @@ +namespace EasyCaching.Redis +{ + using EasyCaching.Core; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.DependencyInjection.Extensions; + using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; + using System; + + /// + /// Redis options extension. + /// + internal sealed class RedisOptionsExtension : IEasyCachingOptionsExtension + { + /// + /// The name. + /// + private readonly string _name; + /// + /// The configure. + /// + private readonly Action configure; + + /// + /// Initializes a new instance of the class. + /// + /// Name. + /// Configure. + public RedisOptionsExtension(string name, Action configure) + { + this._name = name; + this.configure = configure; + } + + /// + /// Adds the services. + /// + /// Services. + public void AddServices(IServiceCollection services) + { + services.AddOptions(); + + services.TryAddSingleton(); + + if (string.IsNullOrWhiteSpace(_name)) + { + services.Configure(configure); + + services.TryAddSingleton(); + services.AddSingleton(); + } + else + { + services.Configure(_name, configure); + + services.AddSingleton(); + services.AddSingleton(x => + { + var optionsMon = x.GetRequiredService>(); + var options = optionsMon.Get(_name); + return new RedisDatabaseProvider(_name, options); + }); + + services.AddSingleton(x => + { + var dbProviders = x.GetServices(); + var serializer = x.GetRequiredService(); + var options = x.GetRequiredService>(); + var factory = x.GetService(); + return new DefaultRedisCachingProvider(_name, dbProviders, serializer, options, factory); + }); + } + } + + /// + /// Withs the services. + /// + /// Services. + public void WithServices(IApplicationBuilder services) + { + // Method intentionally left empty. + } + } +} diff --git a/src/EasyCaching.Redis/EasyCaching.Redis.csproj b/src/EasyCaching.Redis/EasyCaching.Redis.csproj index 26d7c3c0..bdebd0e5 100644 --- a/src/EasyCaching.Redis/EasyCaching.Redis.csproj +++ b/src/EasyCaching.Redis/EasyCaching.Redis.csproj @@ -27,4 +27,7 @@ + + + diff --git a/src/EasyCaching.SQLite/ConstSQL.cs b/src/EasyCaching.SQLite/Configurations/ConstSQL.cs similarity index 100% rename from src/EasyCaching.SQLite/ConstSQL.cs rename to src/EasyCaching.SQLite/Configurations/ConstSQL.cs diff --git a/src/EasyCaching.SQLite/Configurations/EasyCachingOptionsExtensions.cs b/src/EasyCaching.SQLite/Configurations/EasyCachingOptionsExtensions.cs new file mode 100644 index 00000000..d033e6fa --- /dev/null +++ b/src/EasyCaching.SQLite/Configurations/EasyCachingOptionsExtensions.cs @@ -0,0 +1,57 @@ +namespace EasyCaching.SQLite +{ + using EasyCaching.Core; + using Microsoft.Extensions.Configuration; + using System; + + /// + /// Easy caching options extensions. + /// + public static class EasyCachingOptionsExtensions + { + /// + /// Uses the SQLite. + /// + /// The SQL ite. + /// Options. + /// Configure. + /// Name. + public static EasyCachingOptions UseSQLite(this EasyCachingOptions options, Action configure, string name = "") + { + if (configure == null) + { + throw new ArgumentNullException(nameof(configure)); + } + + options.RegisterExtension(new SQLiteOptionsExtension(name, configure)); + return options; + } + + /// + /// Uses the SQLite. + /// + /// The SQL ite. + /// Options. + /// Configuration. + /// Name. + /// Section name. + public static EasyCachingOptions UseSQLite(this EasyCachingOptions options, IConfiguration configuration, string name = "", string sectionName = EasyCachingConstValue.SQLiteSection) + { + var dbConfig = configuration.GetSection(sectionName); + var sqliteOptions = new SQLiteOptions(); + dbConfig.Bind(sqliteOptions); + + void configure(SQLiteOptions x) + { + x.CachingProviderType = sqliteOptions.CachingProviderType; + x.EnableLogging = sqliteOptions.EnableLogging; + x.MaxRdSecond = sqliteOptions.MaxRdSecond; + x.Order = sqliteOptions.Order; + x.DBConfig = sqliteOptions.DBConfig; + } + + options.RegisterExtension(new SQLiteOptionsExtension(name, configure)); + return options; + } + } +} diff --git a/src/EasyCaching.SQLite/ISQLiteDatabaseProvider.cs b/src/EasyCaching.SQLite/Configurations/ISQLiteDatabaseProvider.cs similarity index 100% rename from src/EasyCaching.SQLite/ISQLiteDatabaseProvider.cs rename to src/EasyCaching.SQLite/Configurations/ISQLiteDatabaseProvider.cs diff --git a/src/EasyCaching.SQLite/SQLiteDBOptions.cs b/src/EasyCaching.SQLite/Configurations/SQLiteDBOptions.cs similarity index 100% rename from src/EasyCaching.SQLite/SQLiteDBOptions.cs rename to src/EasyCaching.SQLite/Configurations/SQLiteDBOptions.cs diff --git a/src/EasyCaching.SQLite/SQLiteDatabaseProvider.cs b/src/EasyCaching.SQLite/Configurations/SQLiteDatabaseProvider.cs similarity index 100% rename from src/EasyCaching.SQLite/SQLiteDatabaseProvider.cs rename to src/EasyCaching.SQLite/Configurations/SQLiteDatabaseProvider.cs diff --git a/src/EasyCaching.SQLite/SQLiteOptions.cs b/src/EasyCaching.SQLite/Configurations/SQLiteOptions.cs similarity index 100% rename from src/EasyCaching.SQLite/SQLiteOptions.cs rename to src/EasyCaching.SQLite/Configurations/SQLiteOptions.cs diff --git a/src/EasyCaching.SQLite/Configurations/SQLiteOptionsExtension.cs b/src/EasyCaching.SQLite/Configurations/SQLiteOptionsExtension.cs new file mode 100644 index 00000000..ea664cc2 --- /dev/null +++ b/src/EasyCaching.SQLite/Configurations/SQLiteOptionsExtension.cs @@ -0,0 +1,103 @@ +namespace EasyCaching.SQLite +{ + using Dapper; + using EasyCaching.Core; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.DependencyInjection.Extensions; + using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; + using System; + + /// + /// SQLite options extension. + /// + internal sealed class SQLiteOptionsExtension : IEasyCachingOptionsExtension + { + /// + /// The name. + /// + private readonly string _name; + /// + /// The configure. + /// + private readonly Action configure; + + /// + /// Initializes a new instance of the class. + /// + /// Name. + /// Configure. + public SQLiteOptionsExtension(string name, Action configure) + { + this._name = name; + this.configure = configure; + } + + /// + /// Adds the services. + /// + /// Services. + public void AddServices(IServiceCollection services) + { + services.AddOptions(); + + if (string.IsNullOrWhiteSpace(_name)) + { + services.Configure(configure); + + services.TryAddSingleton(); + services.AddSingleton(); + } + else + { + services.Configure(_name, configure); + + services.AddSingleton(); + services.AddSingleton(x => + { + var optionsMon = x.GetRequiredService>(); + var options = optionsMon.Get(_name); + return new SQLiteDatabaseProvider(_name, options); + }); + + services.AddSingleton(x => + { + var dbProviders = x.GetServices(); + var optionsMon = x.GetRequiredService>(); + var options = optionsMon.Get(_name); + var factory = x.GetService(); + return new DefaultSQLiteCachingProvider(_name, dbProviders, options, factory); + }); + } + } + + /// + /// Withs the services. + /// + /// App. + public void WithServices(IApplicationBuilder app) + { + try + { + var dbProviders = app.ApplicationServices.GetServices(); + + foreach (var dbProvider in dbProviders) + { + var conn = dbProvider.GetConnection(); + + if (conn.State == System.Data.ConnectionState.Closed) + { + conn.Open(); + } + + conn.Execute(ConstSQL.CREATESQL); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + } +} diff --git a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs index 27ac85e9..45ffc180 100644 --- a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs +++ b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs @@ -66,11 +66,11 @@ public DefaultSQLiteCachingProvider( public DefaultSQLiteCachingProvider( string name, IEnumerable< ISQLiteDatabaseProvider> dbProviders, - IOptionsMonitor options, + SQLiteOptions options, ILoggerFactory loggerFactory = null) { this._dbProvider = dbProviders.FirstOrDefault(x => x.DBProviderName.Equals(name)); - this._options = options.CurrentValue; + this._options = options; this._logger = loggerFactory?.CreateLogger(); this._cache = _dbProvider.GetConnection(); this._cacheStats = new CacheStats(); diff --git a/src/EasyCaching.SQLite/EasyCaching.SQLite.csproj b/src/EasyCaching.SQLite/EasyCaching.SQLite.csproj index 2f9483a7..55eba3ee 100644 --- a/src/EasyCaching.SQLite/EasyCaching.SQLite.csproj +++ b/src/EasyCaching.SQLite/EasyCaching.SQLite.csproj @@ -31,4 +31,7 @@ + + + diff --git a/src/EasyCaching.SQLite/SQLiteCacheServiceCollectionExtensions.cs b/src/EasyCaching.SQLite/SQLiteCacheServiceCollectionExtensions.cs index d052bec0..f975b1a4 100644 --- a/src/EasyCaching.SQLite/SQLiteCacheServiceCollectionExtensions.cs +++ b/src/EasyCaching.SQLite/SQLiteCacheServiceCollectionExtensions.cs @@ -96,7 +96,8 @@ public static IServiceCollection AddSQLiteCacheWithFactory( services.AddSingleton(x => { var dbProviders = x.GetServices(); - var options = x.GetRequiredService>(); + var optionsMon = x.GetRequiredService>(); + var options = optionsMon.Get(providerName); var factory = x.GetService(); return new DefaultSQLiteCachingProvider(providerName, dbProviders, options, factory); }); @@ -135,7 +136,8 @@ public static IServiceCollection AddSQLiteCacheWithFactory( services.AddSingleton(x => { var dbProviders = x.GetServices(); - var options = x.GetRequiredService>(); + var optionsMon = x.GetRequiredService>(); + var options = optionsMon.Get(providerName); var factory = x.GetService(); return new DefaultSQLiteCachingProvider(providerName, dbProviders, options, factory); }); diff --git a/src/EasyCaching.Serialization.Json/EasyCachingJsonSerializerOptions.cs b/src/EasyCaching.Serialization.Json/Configurations/EasyCachingJsonSerializerOptions.cs similarity index 100% rename from src/EasyCaching.Serialization.Json/EasyCachingJsonSerializerOptions.cs rename to src/EasyCaching.Serialization.Json/Configurations/EasyCachingJsonSerializerOptions.cs diff --git a/src/EasyCaching.Serialization.Json/Configurations/EasyCachingOptionsExtensions.cs b/src/EasyCaching.Serialization.Json/Configurations/EasyCachingOptionsExtensions.cs new file mode 100644 index 00000000..e10965e8 --- /dev/null +++ b/src/EasyCaching.Serialization.Json/Configurations/EasyCachingOptionsExtensions.cs @@ -0,0 +1,24 @@ +namespace EasyCaching.Serialization.Json +{ + using EasyCaching.Core; + using System; + + /// + /// EasyCaching options extensions. + /// + public static class EasyCachingOptionsExtensions + { + /// + /// Withs the json. + /// + /// The json. + /// Options. + /// Configure. + public static EasyCachingOptions WithJson(this EasyCachingOptions options, Action configure) + { + options.RegisterExtension(new JsonOptionsExtension(configure)); + + return options; + } + } +} diff --git a/src/EasyCaching.Serialization.Json/Configurations/JsonOptionsExtension.cs b/src/EasyCaching.Serialization.Json/Configurations/JsonOptionsExtension.cs new file mode 100644 index 00000000..46eef900 --- /dev/null +++ b/src/EasyCaching.Serialization.Json/Configurations/JsonOptionsExtension.cs @@ -0,0 +1,51 @@ +namespace EasyCaching.Serialization.Json +{ + using EasyCaching.Core; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + using System; + + /// + /// Json options extension. + /// + internal sealed class JsonOptionsExtension : IEasyCachingOptionsExtension + { + /// + /// The configure. + /// + private readonly Action _configure; + + /// + /// Initializes a new instance of the class. + /// + /// Configure. + public JsonOptionsExtension(Action configure) + { + this._configure = configure; + } + + /// + /// Adds the services. + /// + /// Services. + public void AddServices(IServiceCollection services) + { + Action configure = x => { }; + + if (_configure != null) configure = _configure; + + services.AddOptions(); + services.Configure(configure); + services.AddSingleton(); + } + + /// + /// Withs the services. + /// + /// Services. + public void WithServices(IApplicationBuilder services) + { + // Method intentionally left empty. + } + } +} diff --git a/src/EasyCaching.Serialization.Json/EasyCaching.Serialization.Json.csproj b/src/EasyCaching.Serialization.Json/EasyCaching.Serialization.Json.csproj index 63897cde..d8716d53 100644 --- a/src/EasyCaching.Serialization.Json/EasyCaching.Serialization.Json.csproj +++ b/src/EasyCaching.Serialization.Json/EasyCaching.Serialization.Json.csproj @@ -28,4 +28,7 @@ + + + diff --git a/src/EasyCaching.Serialization.MessagePack/Configurations/EasyCachingOptionsExtensions.cs b/src/EasyCaching.Serialization.MessagePack/Configurations/EasyCachingOptionsExtensions.cs new file mode 100644 index 00000000..a891e164 --- /dev/null +++ b/src/EasyCaching.Serialization.MessagePack/Configurations/EasyCachingOptionsExtensions.cs @@ -0,0 +1,22 @@ +namespace EasyCaching.Serialization.MessagePack +{ + using EasyCaching.Core; + + /// + /// Easy caching options extensions. + /// + public static class EasyCachingOptionsExtensions + { + /// + /// Withs the message pack. + /// + /// The message pack. + /// Options. + public static EasyCachingOptions WithMessagePack(this EasyCachingOptions options) + { + options.RegisterExtension(new MessagePackOptionsExtension()); + + return options; + } + } +} diff --git a/src/EasyCaching.Serialization.MessagePack/Configurations/MessagePackOptionsExtension.cs b/src/EasyCaching.Serialization.MessagePack/Configurations/MessagePackOptionsExtension.cs new file mode 100644 index 00000000..0b6a286e --- /dev/null +++ b/src/EasyCaching.Serialization.MessagePack/Configurations/MessagePackOptionsExtension.cs @@ -0,0 +1,30 @@ +namespace EasyCaching.Serialization.MessagePack +{ + using EasyCaching.Core; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + + /// + /// Message pack options extension. + /// + internal sealed class MessagePackOptionsExtension : IEasyCachingOptionsExtension + { + /// + /// Adds the services. + /// + /// Services. + public void AddServices(IServiceCollection services) + { + services.AddSingleton(); + } + + /// + /// Withs the services. + /// + /// Services. + public void WithServices(IApplicationBuilder services) + { + // Method intentionally left empty. + } + } +} diff --git a/src/EasyCaching.Serialization.MessagePack/EasyCaching.Serialization.MessagePack.csproj b/src/EasyCaching.Serialization.MessagePack/EasyCaching.Serialization.MessagePack.csproj index 8e8b7a3c..80c33699 100644 --- a/src/EasyCaching.Serialization.MessagePack/EasyCaching.Serialization.MessagePack.csproj +++ b/src/EasyCaching.Serialization.MessagePack/EasyCaching.Serialization.MessagePack.csproj @@ -27,4 +27,7 @@ + + + diff --git a/src/EasyCaching.Serialization.Protobuf/Configurations/EasyCachingOptionsExtensions.cs b/src/EasyCaching.Serialization.Protobuf/Configurations/EasyCachingOptionsExtensions.cs new file mode 100644 index 00000000..ceeced95 --- /dev/null +++ b/src/EasyCaching.Serialization.Protobuf/Configurations/EasyCachingOptionsExtensions.cs @@ -0,0 +1,22 @@ +namespace EasyCaching.Serialization.Protobuf +{ + using EasyCaching.Core; + + /// + /// EasyCaching options extensions. + /// + public static class EasyCachingOptionsExtensions + { + /// + /// Withs the protobuf. + /// + /// The protobuf. + /// Options. + public static EasyCachingOptions WithProtobuf(this EasyCachingOptions options) + { + options.RegisterExtension(new ProtobufOptionsExtension()); + + return options; + } + } +} diff --git a/src/EasyCaching.Serialization.Protobuf/Configurations/ProtobufOptionsExtension.cs b/src/EasyCaching.Serialization.Protobuf/Configurations/ProtobufOptionsExtension.cs new file mode 100644 index 00000000..2c2eea87 --- /dev/null +++ b/src/EasyCaching.Serialization.Protobuf/Configurations/ProtobufOptionsExtension.cs @@ -0,0 +1,30 @@ +namespace EasyCaching.Serialization.Protobuf +{ + using EasyCaching.Core; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + + /// + /// Protobuf options extension. + /// + internal sealed class ProtobufOptionsExtension : IEasyCachingOptionsExtension + { + /// + /// Adds the services. + /// + /// Services. + public void AddServices(IServiceCollection services) + { + services.AddSingleton(); + } + + /// + /// Withs the services. + /// + /// Services. + public void WithServices(IApplicationBuilder services) + { + // Method intentionally left empty. + } + } +} diff --git a/src/EasyCaching.Serialization.Protobuf/EasyCaching.Serialization.Protobuf.csproj b/src/EasyCaching.Serialization.Protobuf/EasyCaching.Serialization.Protobuf.csproj index c9e162db..5d15b0b2 100644 --- a/src/EasyCaching.Serialization.Protobuf/EasyCaching.Serialization.Protobuf.csproj +++ b/src/EasyCaching.Serialization.Protobuf/EasyCaching.Serialization.Protobuf.csproj @@ -27,4 +27,7 @@ + + + diff --git a/test/EasyCaching.UnitTests/CachingTests/BaseCachingProviderWithFactoryTest.cs b/test/EasyCaching.UnitTests/CachingTests/BaseCachingProviderWithFactoryTest.cs new file mode 100644 index 00000000..526492e3 --- /dev/null +++ b/test/EasyCaching.UnitTests/CachingTests/BaseCachingProviderWithFactoryTest.cs @@ -0,0 +1,66 @@ +namespace EasyCaching.UnitTests +{ + using System; + using System.Threading.Tasks; + using EasyCaching.Core; + using Xunit; + + public abstract class BaseCachingProviderWithFactoryTest + { + protected IEasyCachingProvider _provider; + protected TimeSpan _defaultTs; + protected IEasyCachingProvider _secondProvider; + public const string SECOND_PROVIDER_NAME = "second"; + + [Fact] + protected virtual void Multi_Instance_Set_And_Get_Should_Succeed() + { + var cacheKey1 = "named-provider-1"; + var cacheKey2 = "named-provider-2"; + + var value1 = Guid.NewGuid().ToString(); + var value2 = Guid.NewGuid().ToString("N"); + + _provider.Set(cacheKey1, value1, _defaultTs); + _secondProvider.Set(cacheKey2, value2, _defaultTs); + + var p1 = _provider.Get(cacheKey1); + var p2 = _provider.Get(cacheKey2); + + var s1 = _secondProvider.Get(cacheKey1); + var s2 = _secondProvider.Get(cacheKey2); + + Assert.Equal(value1, p1.Value); + Assert.False(p2.HasValue); + + Assert.False(s1.HasValue); + Assert.Equal(value2, s2.Value); + } + + [Fact] + protected virtual void Set_Value_And_Get_Cached_Value_Should_Succeed() + { + var cacheKey = Guid.NewGuid().ToString(); + var cacheValue = "value"; + + _provider.Set(cacheKey, cacheValue, _defaultTs); + + var val = _provider.Get(cacheKey); + Assert.True(val.HasValue); + Assert.Equal(cacheValue, val.Value); + } + + [Fact] + protected virtual async Task Set_Value_And_Get_Cached_Value_Async_Should_Succeed() + { + var cacheKey = Guid.NewGuid().ToString(); + var cacheValue = "value"; + + await _provider.SetAsync(cacheKey, cacheValue, _defaultTs); + + var val = await _provider.GetAsync(cacheKey); + Assert.True(val.HasValue); + Assert.Equal(cacheValue, val.Value); + } + } +} diff --git a/test/EasyCaching.UnitTests/CachingTests/BaseUsingEasyCachingTest.cs b/test/EasyCaching.UnitTests/CachingTests/BaseUsingEasyCachingTest.cs new file mode 100644 index 00000000..30adffd9 --- /dev/null +++ b/test/EasyCaching.UnitTests/CachingTests/BaseUsingEasyCachingTest.cs @@ -0,0 +1,39 @@ +namespace EasyCaching.UnitTests +{ + using EasyCaching.Core; + using System; + using System.Threading.Tasks; + using Xunit; + + public abstract class BaseUsingEasyCachingTest + { + protected IEasyCachingProvider _provider; + protected TimeSpan _defaultTs; + + [Fact] + protected virtual void Set_Value_And_Get_Cached_Value_Should_Succeed() + { + var cacheKey = Guid.NewGuid().ToString(); + var cacheValue = "value"; + + _provider.Set(cacheKey, cacheValue, _defaultTs); + + var val = _provider.Get(cacheKey); + Assert.True(val.HasValue); + Assert.Equal(cacheValue, val.Value); + } + + [Fact] + protected virtual async Task Set_Value_And_Get_Cached_Value_Async_Should_Succeed() + { + var cacheKey = Guid.NewGuid().ToString(); + var cacheValue = "value"; + + await _provider.SetAsync(cacheKey, cacheValue, _defaultTs); + + var val = await _provider.GetAsync(cacheKey); + Assert.True(val.HasValue); + Assert.Equal(cacheValue, val.Value); + } + } +} diff --git a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs index 6f9fa5b9..e3c4c69a 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs @@ -6,6 +6,8 @@ using System; using System.Threading.Tasks; using Xunit; + using System.IO; + using Microsoft.Extensions.Configuration; public class MemcachedProviderTest : BaseCachingProviderTest { @@ -161,7 +163,7 @@ private void GetCacheItem(string cacheKey, string prefix) } - public class MemcachedProviderWithFactoryTest : BaseCachingProviderTest + public class MemcachedProviderWithFactoryTest : BaseCachingProviderWithFactoryTest { public MemcachedProviderWithFactoryTest() { @@ -170,16 +172,21 @@ public MemcachedProviderWithFactoryTest() { options.DBConfig.AddServer("127.0.0.1", 11212); }); + services.AddDefaultMemcachedWithFactory(SECOND_PROVIDER_NAME, options => + { + options.DBConfig.AddServer("127.0.0.1", 11211); + }); services.AddLogging(); IServiceProvider serviceProvider = services.BuildServiceProvider(); var factory = serviceProvider.GetService(); _provider = factory.GetCachingProvider("MyTest"); + _secondProvider = factory.GetCachingProvider(SECOND_PROVIDER_NAME); _defaultTs = TimeSpan.FromSeconds(50); } [Fact] - protected override void RemoveByPrefix_Should_Succeed() + public void RemoveByPrefix_Should_Succeed() { string prefixKey = "demowithfactory"; string prefixValue = "abcwithfactory"; @@ -209,7 +216,7 @@ protected override void RemoveByPrefix_Should_Succeed() } [Fact] - protected override async Task RemoveByPrefixAsync_Should_Succeed() + public async Task RemoveByPrefixAsync_Should_Succeed() { string prefixKey = "demowithfactoryasync"; string prefixValue = "abcwithfactoryasync"; @@ -255,65 +262,107 @@ public void CacheKey_Length_GT_250_Should_Call_SHA1() var val = _provider.Get(cacheKey); Assert.True(val.HasValue); } - - [Fact] - protected override void GetByPrefix_Should_Succeed() + + private void SetCacheItem(string cacheKey, string cacheValue, string prefix) { + var pre = _provider.Get(prefix); - } + cacheKey = string.Concat(pre, cacheKey); - [Fact] - protected override async Task GetByPrefixAsync_Should_Succeed() - { - await Task.FromResult(1); + _provider.Set(cacheKey, cacheValue, _defaultTs); + + var val = _provider.Get(cacheKey); + Assert.Equal(cacheValue, val.Value); } - [Fact] - protected override void GetByPrefix_With_Not_Existed_Prefix_Should_Return_Empty_Dict() + private void GetCacheItem(string cacheKey, string prefix) { + var pre = _provider.Get(prefix); - } + cacheKey = string.Concat(pre, cacheKey); - [Fact] - protected override async Task GetByPrefixAsync_With_Not_Existed_Prefix_Should_Return_Empty_Dict() - { - await Task.FromResult(1); + var val = _provider.Get(cacheKey); + Assert.False(val.HasValue); } + } - [Fact] - protected override void Get_Count_Without_Prefix_Should_Succeed() + public class MemcachedProviderUseEasyCachingTest : BaseUsingEasyCachingTest + { + public MemcachedProviderUseEasyCachingTest() { + 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="127.0.0.1", Port=11212} + } + }; + }, EasyCachingConstValue.DefaultMemcachedName); + }); - } - - [Fact] - protected override void Get_Count_With_Prefix_Should_Succeed() - { + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var factory = serviceProvider.GetService(); + _provider = factory.GetCachingProvider(EasyCachingConstValue.DefaultMemcachedName); + _defaultTs = TimeSpan.FromSeconds(30); } + } - private void SetCacheItem(string cacheKey, string cacheValue, string prefix) + public class MemcachedProviderUseEasyCachingWithConfigTest : BaseUsingEasyCachingTest + { + public MemcachedProviderUseEasyCachingWithConfigTest() { - var pre = _provider.Get(prefix); - - cacheKey = string.Concat(pre, cacheKey); + IServiceCollection services = new ServiceCollection(); - _provider.Set(cacheKey, cacheValue, _defaultTs); + var appsettings = @" +{ + 'easycaching': { + 'memcached': { + 'MaxRdSecond': 600, + 'Order': 99, + 'dbconfig': { + 'Servers': [ + { + 'Address': '127.0.0.1', + 'Port': 11211 + } + ] + } + } + } +}"; + var path = TestHelpers.CreateTempFile(appsettings); + var directory = Path.GetDirectoryName(path); + var fileName = Path.GetFileName(path); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.SetBasePath(directory); + configurationBuilder.AddJsonFile(fileName); + var config = configurationBuilder.Build(); + services.AddLogging(); + services.AddEasyCaching(option => + { + option.UseMemcached(config, "mName"); + }); - var val = _provider.Get(cacheKey); - Assert.Equal(cacheValue, val.Value); + IServiceProvider serviceProvider = services.BuildServiceProvider(); + _provider = serviceProvider.GetService(); + _defaultTs = TimeSpan.FromSeconds(30); } - private void GetCacheItem(string cacheKey, string prefix) + [Fact] + public void Provider_Information_Should_Be_Correct() { - var pre = _provider.Get(prefix); - - cacheKey = string.Concat(pre, cacheKey); - - var val = _provider.Get(cacheKey); - Assert.False(val.HasValue); + Assert.Equal(600, _provider.MaxRdSecond); + Assert.Equal(99, _provider.Order); + Assert.Equal("mName", _provider.Name); } } - } diff --git a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs index 7a25ed45..0a1725e3 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs @@ -2,9 +2,11 @@ namespace EasyCaching.UnitTests { using EasyCaching.Core; using EasyCaching.InMemory; + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; + using System.IO; using System.Linq; using System.Threading.Tasks; using Xunit; @@ -25,30 +27,10 @@ public void Deault_MaxRdSecond_Should_Be_120() { Assert.Equal(120, _provider.MaxRdSecond); } - - [Fact] - public void TrySet_With_Expiration() - { - var cacheKey = Guid.NewGuid().ToString(); - var cacheValue1 = "value1"; - var cacheValue2 = "value2"; - - var first = _provider.TrySet(cacheKey, cacheValue1, TimeSpan.FromSeconds(1)); - Assert.True(first); - - System.Threading.Thread.Sleep(1100); - - var second = _provider.TrySet(cacheKey, cacheValue2, _defaultTs); - Assert.True(second); - } } - public class MemoryCachingProviderWithFactoryTest : BaseCachingProviderTest + public class MemoryCachingProviderWithFactoryTest : BaseCachingProviderWithFactoryTest { - private readonly IEasyCachingProvider _secondProvider; - - private const string SECOND_PROVIDER_NAME = "second"; - public MemoryCachingProviderWithFactoryTest() { IServiceCollection services = new ServiceCollection(); @@ -61,38 +43,11 @@ public MemoryCachingProviderWithFactoryTest() _defaultTs = TimeSpan.FromSeconds(30); } - [Fact] - public void Multi_Instance_Set_And_Get_Should_Succeed() - { - var cacheKey1 = "named-provider-1"; - var cacheKey2 = "named-provider-2"; - - var value1 = Guid.NewGuid().ToString(); - var value2 = Guid.NewGuid().ToString("N"); - - _provider.Set(cacheKey1, value1, _defaultTs); - _secondProvider.Set(cacheKey2, value2, _defaultTs); - - var p1 = _provider.Get(cacheKey1); - var p2 = _provider.Get(cacheKey2); - - var s1 = _secondProvider.Get(cacheKey1); - var s2 = _secondProvider.Get(cacheKey2); - - Assert.Equal(value1, p1.Value); - Assert.False(p2.HasValue); - - Assert.False(s1.HasValue); - Assert.Equal(value2, s2.Value); - } - - - [Fact] - protected override void GetByPrefix_Should_Succeed() + public void GetByPrefix_Should_Succeed() { _provider.RemoveAll(new List { "getbyprefix:key:1", "getbyprefix:key:2" }); - var dict = GetMultiDict("getbyprefix:"); + var dict = TestHelpers.GetMultiDict("getbyprefix:"); _provider.SetAll(dict, _defaultTs); @@ -108,10 +63,10 @@ protected override void GetByPrefix_Should_Succeed() } [Fact] - protected override async Task GetByPrefixAsync_Should_Succeed() + public async Task GetByPrefixAsync_Should_Succeed() { _provider.RemoveAll(new List { "getbyprefixasync:key:1", "getbyprefixasync:key:2" }); - var dict = GetMultiDict("getbyprefixasync:"); + var dict = TestHelpers.GetMultiDict("getbyprefixasync:"); _provider.SetAll(dict, _defaultTs); @@ -126,4 +81,97 @@ protected override async Task GetByPrefixAsync_Should_Succeed() Assert.Equal("value2", res.Where(x => x.Key == $"{EasyCachingConstValue.DefaultInMemoryName}-getbyprefixasync:key:2").Select(x => x.Value).FirstOrDefault().Value); } } + + public class MemoryCachingProviderUseEasyCachingTest : BaseUsingEasyCachingTest + { + private readonly IEasyCachingProvider _secondProvider; + private const string SECOND_PROVIDER_NAME = "second"; + + public MemoryCachingProviderUseEasyCachingTest() + { + IServiceCollection services = new ServiceCollection(); + + services.AddEasyCaching(option => + { + option.UseInMemory(EasyCachingConstValue.DefaultInMemoryName); + option.UseInMemory(SECOND_PROVIDER_NAME); + }); + + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var factory = serviceProvider.GetService(); + _provider = factory.GetCachingProvider(EasyCachingConstValue.DefaultInMemoryName); + _secondProvider = factory.GetCachingProvider(SECOND_PROVIDER_NAME); + _defaultTs = TimeSpan.FromSeconds(30); + } + + [Fact] + public void Sec_Set_Value_And_Get_Cached_Value_Should_Succeed() + { + var cacheKey = Guid.NewGuid().ToString(); + var cacheValue = "value"; + + _secondProvider.Set(cacheKey, cacheValue, _defaultTs); + + var val = _secondProvider.Get(cacheKey); + Assert.True(val.HasValue); + Assert.Equal(cacheValue, val.Value); + } + + [Fact] + public async Task Sec_Set_Value_And_Get_Cached_Value_Async_Should_Succeed() + { + var cacheKey = Guid.NewGuid().ToString(); + var cacheValue = "value"; + + await _secondProvider.SetAsync(cacheKey, cacheValue, _defaultTs); + + var val = await _secondProvider.GetAsync(cacheKey); + Assert.True(val.HasValue); + Assert.Equal(cacheValue, val.Value); + } + } + + public class MemoryCachingProviderUseEasyCachingWithConfigTest : BaseUsingEasyCachingTest + { + public MemoryCachingProviderUseEasyCachingWithConfigTest() + { + IServiceCollection services = new ServiceCollection(); + + var appsettings = @" +{ + 'easycaching': { + 'inmemory': { + 'CachingProviderType': 1, + 'MaxRdSecond': 600, + 'Order': 99, + } + } +}"; + var path = TestHelpers.CreateTempFile(appsettings); + var directory = Path.GetDirectoryName(path); + var fileName = Path.GetFileName(path); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.SetBasePath(directory); + configurationBuilder.AddJsonFile(fileName); + var config = configurationBuilder.Build(); + + services.AddEasyCaching(option => + { + option.UseInMemory(config, "mName"); + }); + + IServiceProvider serviceProvider = services.BuildServiceProvider(); + _provider = serviceProvider.GetService(); + _defaultTs = TimeSpan.FromSeconds(30); + } + + [Fact] + public void Provider_Information_Should_Be_Correct() + { + Assert.Equal(600, _provider.MaxRdSecond); + Assert.Equal(99, _provider.Order); + Assert.Equal("mName", _provider.Name); + } + } } diff --git a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs index 901596b7..229a4034 100644 --- a/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/RedisCachingProviderTest.cs @@ -95,7 +95,7 @@ public class RedisCachingProviderWithFactoryTest : BaseCachingProviderTest public RedisCachingProviderWithFactoryTest() { IServiceCollection services = new ServiceCollection(); - services.AddDefaultRedisCacheWithFactory(EasyCachingConstValue.DefaultRedisName,options => + services.AddDefaultRedisCacheWithFactory(EasyCachingConstValue.DefaultRedisName, options => { options.DBConfig = new RedisDBOptions { diff --git a/test/EasyCaching.UnitTests/CachingTests/SQLiteCachingTest.cs b/test/EasyCaching.UnitTests/CachingTests/SQLiteCachingTest.cs index 30955bbb..96aaf7b0 100644 --- a/test/EasyCaching.UnitTests/CachingTests/SQLiteCachingTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/SQLiteCachingTest.cs @@ -3,8 +3,11 @@ using Dapper; using EasyCaching.Core; using EasyCaching.SQLite; + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; + using System.IO; + using System.Threading.Tasks; using Xunit; public class SQLiteCachingTest : BaseCachingProviderTest @@ -17,9 +20,8 @@ public SQLiteCachingTest() services.AddSQLiteCache(options => { options.DBConfig = new SQLiteDBOptions - { - FileName = "", - FilePath = "", + { + FileName = "s1.db", CacheMode = Microsoft.Data.Sqlite.SqliteCacheMode.Default, OpenMode = Microsoft.Data.Sqlite.SqliteOpenMode.Memory, }; @@ -41,13 +43,8 @@ public SQLiteCachingTest() } - - public class SQLiteCachingProviderWithFactoryTest : BaseCachingProviderTest + public class SQLiteCachingProviderWithFactoryTest : BaseCachingProviderWithFactoryTest { - private readonly IEasyCachingProvider _secondProvider; - - private const string SECOND_PROVIDER_NAME = "second"; - public SQLiteCachingProviderWithFactoryTest() { IServiceCollection services = new ServiceCollection(); @@ -55,19 +52,17 @@ public SQLiteCachingProviderWithFactoryTest() { options.DBConfig = new SQLiteDBOptions { - FileName = "", - FilePath = "", + FileName = "f0.db", CacheMode = Microsoft.Data.Sqlite.SqliteCacheMode.Default, OpenMode = Microsoft.Data.Sqlite.SqliteOpenMode.Memory, }; - + }); services.AddSQLiteCacheWithFactory(SECOND_PROVIDER_NAME, options => { options.DBConfig = new SQLiteDBOptions { - FileName = "", - FilePath = "", + FileName = "f1.db", CacheMode = Microsoft.Data.Sqlite.SqliteCacheMode.Default, OpenMode = Microsoft.Data.Sqlite.SqliteOpenMode.Memory, }; @@ -77,40 +72,149 @@ public SQLiteCachingProviderWithFactoryTest() _provider = factory.GetCachingProvider(EasyCachingConstValue.DefaultSQLiteName); _secondProvider = factory.GetCachingProvider(SECOND_PROVIDER_NAME); - var _dbProvider = serviceProvider.GetService(); - var conn = _dbProvider.GetConnection(); - if (conn.State == System.Data.ConnectionState.Closed) + var _dbProviders = serviceProvider.GetServices(); + foreach (var _dbProvider in _dbProviders) { - conn.Open(); + var conn = _dbProvider.GetConnection(); + if (conn.State == System.Data.ConnectionState.Closed) + { + conn.Open(); + } + conn.Execute(ConstSQL.CREATESQL); + } + + _defaultTs = TimeSpan.FromSeconds(30); + } + } + + public class SQLiteCachingProviderUseEasyCachingTest : BaseUsingEasyCachingTest + { + private readonly IEasyCachingProvider _secondProvider; + private const string SECOND_PROVIDER_NAME = "second"; + + public SQLiteCachingProviderUseEasyCachingTest() + { + IServiceCollection services = new ServiceCollection(); + + services.AddEasyCaching(option => + { + option.UseSQLite(config => + { + config.DBConfig = new SQLiteDBOptions + { + FileName = "use_0.db", + }; + }, EasyCachingConstValue.DefaultSQLiteName); + option.UseSQLite(config => + { + config.DBConfig = new SQLiteDBOptions + { + FileName = "use_1.db", + }; + }, SECOND_PROVIDER_NAME); + }); + + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var factory = serviceProvider.GetService(); + _provider = factory.GetCachingProvider(EasyCachingConstValue.DefaultSQLiteName); + _secondProvider = factory.GetCachingProvider(SECOND_PROVIDER_NAME); + + var _dbProviders = serviceProvider.GetServices(); + foreach (var _dbProvider in _dbProviders) + { + var conn = _dbProvider.GetConnection(); + if (conn.State == System.Data.ConnectionState.Closed) + { + conn.Open(); + } + conn.Execute(ConstSQL.CREATESQL); } - conn.Execute(ConstSQL.CREATESQL); _defaultTs = TimeSpan.FromSeconds(30); } [Fact] - public void Multi_Instance_Set_And_Get_Should_Succeed() + public void Sec_Set_Value_And_Get_Cached_Value_Should_Succeed() { - var cacheKey1 = "named-provider-1"; - var cacheKey2 = "named-provider-2"; + var cacheKey = Guid.NewGuid().ToString(); + var cacheValue = "value"; - var value1 = Guid.NewGuid().ToString(); - var value2 = Guid.NewGuid().ToString("N"); + _secondProvider.Set(cacheKey, cacheValue, _defaultTs); - _provider.Set(cacheKey1, value1, _defaultTs); - _secondProvider.Set(cacheKey2, value2, _defaultTs); + var val = _secondProvider.Get(cacheKey); + Assert.True(val.HasValue); + Assert.Equal(cacheValue, val.Value); + } - var p1 = _provider.Get(cacheKey1); - var p2 = _provider.Get(cacheKey2); + [Fact] + public async Task Sec_Set_Value_And_Get_Cached_Value_Async_Should_Succeed() + { + var cacheKey = Guid.NewGuid().ToString(); + var cacheValue = "value"; - var s1 = _secondProvider.Get(cacheKey1); - var s2 = _secondProvider.Get(cacheKey2); + await _secondProvider.SetAsync(cacheKey, cacheValue, _defaultTs); - Assert.Equal(value1, p1.Value); - Assert.False(p2.HasValue); + var val = await _secondProvider.GetAsync(cacheKey); + Assert.True(val.HasValue); + Assert.Equal(cacheValue, val.Value); + } + } - Assert.False(s1.HasValue); - Assert.Equal(value2, s2.Value); + public class SQLiteCachingProviderUseEasyCachingWithConfigTest : BaseUsingEasyCachingTest + { + public SQLiteCachingProviderUseEasyCachingWithConfigTest() + { + IServiceCollection services = new ServiceCollection(); + + var appsettings = @" +{ + 'easycaching': { + 'sqlite': { + 'MaxRdSecond': 600, + 'Order': 99, + 'dbconfig': { + 'FileName': 'my.db' + } + } + } +}"; + var path = TestHelpers.CreateTempFile(appsettings); + var directory = Path.GetDirectoryName(path); + var fileName = Path.GetFileName(path); + + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.SetBasePath(directory); + configurationBuilder.AddJsonFile(fileName); + var config = configurationBuilder.Build(); + + services.AddEasyCaching(option => + { + option.UseSQLite(config, "mName"); + }); + + IServiceProvider serviceProvider = services.BuildServiceProvider(); + _provider = serviceProvider.GetService(); + + var _dbProviders = serviceProvider.GetServices(); + foreach (var _dbProvider in _dbProviders) + { + var conn = _dbProvider.GetConnection(); + if (conn.State == System.Data.ConnectionState.Closed) + { + conn.Open(); + } + conn.Execute(ConstSQL.CREATESQL); + } + + _defaultTs = TimeSpan.FromSeconds(30); + } + + [Fact] + public void Provider_Information_Should_Be_Correct() + { + Assert.Equal(600, _provider.MaxRdSecond); + Assert.Equal(99, _provider.Order); + Assert.Equal("mName", _provider.Name); } } } diff --git a/test/EasyCaching.UnitTests/TestHelpers.cs b/test/EasyCaching.UnitTests/TestHelpers.cs index 25057f86..247fc560 100644 --- a/test/EasyCaching.UnitTests/TestHelpers.cs +++ b/test/EasyCaching.UnitTests/TestHelpers.cs @@ -1,5 +1,6 @@ namespace EasyCaching.UnitTests { + using System.Collections.Generic; using System.IO; public class TestHelpers @@ -10,5 +11,14 @@ public static string CreateTempFile(string contents) File.WriteAllText(tempFile, contents); return tempFile; } + + public static Dictionary GetMultiDict(string prefix = "") + { + return new Dictionary() + { + {string.Concat(prefix,"key:1"), "value1"}, + {string.Concat(prefix,"key:2"), "value2"} + }; + } } }