diff --git a/.travis.yml b/.travis.yml index a93ca438..1d0ffa88 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,7 @@ osx_image: xcode9.1 branches: only: - master + - dev script: - if test "$TRAVIS_OS_NAME" == "linux"; then dotnet restore; fi diff --git a/README.md b/README.md index 1e4e9e3c..cfe3ddf1 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ -![](https://raw.githubusercontent.com/catcherwong/EasyCaching/master/media/easycaching-icon.png) +![](media/easycaching-icon.png?raw=true) EasyCaching is a open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier! [![Coverage Status](https://coveralls.io/repos/github/catcherwong/EasyCaching/badge.svg?branch=master)](https://coveralls.io/github/catcherwong/EasyCaching?branch=master) - -[![GitHub license](https://img.shields.io/github/license/catcherwong/EasyCaching.svg)](https://github.com/catcherwong/EasyCaching/blob/master/LICENSE) +[![Member project of .NET China Foundation](https://img.shields.io/badge/member_project_of-.NET_CHINA-red.svg?style=flat&colorB=9E20C8)](https://github.com/dotnetcore) +[![GitHub license](https://img.shields.io/github/license/dotnetcore/EasyCaching.svg)](https://github.com/dotnetcore/EasyCaching/blob/master/LICENSE) ## CI Build Status | Platform | Build Server | Status | |--------- |------------- |---------| -| AppVeyor | Windows |[![Build status](https://ci.appveyor.com/api/projects/status/ji7513h4uv4ysq2i?svg=true)](https://ci.appveyor.com/project/catcherwong/easycaching) | -| Travis | Linux/OSX | [![Build Status](https://travis-ci.org/catcherwong/EasyCaching.svg?branch=master)](https://travis-ci.org/catcherwong/EasyCaching) | +| AppVeyor | Windows |[![Build status](https://ci.appveyor.com/api/projects/status/4x6qal9c1r10wn6x?svg=true)](https://ci.appveyor.com/project/catcherwong/easycaching-48okb) | +| Travis | Linux/OSX | [![Build Status](https://travis-ci.org/dotnetcore/EasyCaching.svg?branch=master)](https://travis-ci.org/dotnetcore/EasyCaching) | ## Nuget Packages diff --git a/sample/EasyCaching.Demo.Interceptor.AspectCore/Startup.cs b/sample/EasyCaching.Demo.Interceptor.AspectCore/Startup.cs index 5adb19bd..bef2cf03 100644 --- a/sample/EasyCaching.Demo.Interceptor.AspectCore/Startup.cs +++ b/sample/EasyCaching.Demo.Interceptor.AspectCore/Startup.cs @@ -3,6 +3,8 @@ using EasyCaching.Demo.Interceptor.AspectCore.Services; using EasyCaching.InMemory; using EasyCaching.Interceptor.AspectCore; + using global::AspectCore.Configuration; + using global::AspectCore.Injector; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -26,7 +28,28 @@ public IServiceProvider ConfigureServices(IServiceCollection services) services.AddMvc(); + //1. all default return services.ConfigureAspectCoreInterceptor(); + + //2. default and customize + //Action action = x => + //{ + // x.AddType(); + //}; + + //return services.ConfigureAspectCoreInterceptor(action); + + //3. all customize + //Action action = x => + //{ + // x.AddType(); + // x.Configure(config => + // { + // config.Interceptors.AddTyped(method => typeof(Core.Internal.IEasyCaching).IsAssignableFrom(method.DeclaringType)); + // }); + //}; + + //return services.ConfigureAspectCoreInterceptor(action, true); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) diff --git a/sample/EasyCaching.Demo.Interceptor.Castle/Startup.cs b/sample/EasyCaching.Demo.Interceptor.Castle/Startup.cs index 0c88c1bc..40021b2d 100644 --- a/sample/EasyCaching.Demo.Interceptor.Castle/Startup.cs +++ b/sample/EasyCaching.Demo.Interceptor.Castle/Startup.cs @@ -1,13 +1,16 @@ namespace EasyCaching.Demo.Interceptor.Castle -{ +{ + using Autofac; + using Autofac.Extras.DynamicProxy; using EasyCaching.InMemory; using EasyCaching.Interceptor.Castle; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; - using System; - + using System; + using System.Reflection; + public class Startup { public Startup(IConfiguration configuration) @@ -25,7 +28,34 @@ public IServiceProvider ConfigureServices(IServiceCollection services) services.AddDefaultInMemoryCache(); + //1. all default return services.ConfigureCastleInterceptor(); + + //2. default and customize + //Action action = x => + //{ + // x.RegisterType().As(); + //}; + + //return services.ConfigureCastleInterceptor(action); + + //3. all customize + //Action action = x => + //{ + // x.RegisterType().As(); + + // var assembly = Assembly.GetExecutingAssembly(); + // x.RegisterType(); + + // x.RegisterAssemblyTypes(assembly) + // .Where(type => typeof(Core.Internal.IEasyCaching).IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract) + // .AsImplementedInterfaces() + // .InstancePerLifetimeScope() + // .EnableInterfaceInterceptors() + // .InterceptedBy(typeof(EasyCachingInterceptor)); + //}; + + //return services.ConfigureCastleInterceptor(action, true); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) diff --git a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs index f575ec15..7cfebeb7 100644 --- a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs +++ b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs @@ -130,7 +130,7 @@ public async Task> GetAsync(string cacheKey) where T : class { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var result = await Task.Run(() => { return _cache.Get(cacheKey) as T; }); + var result = await Task.FromResult((T)_cache.Get(cacheKey)); if (result != null) return new CacheValue(result, true); @@ -161,9 +161,11 @@ public async Task RemoveAsync(string cacheKey) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - await Task.Run(() => _cache.Remove(cacheKey)); - - _cacheKeys.TryRemove(cacheKey); + await Task.Run(() => + { + _cache.Remove(cacheKey); + _cacheKeys.TryRemove(cacheKey); + }); } /// @@ -200,9 +202,11 @@ public async Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration ArgumentCheck.NotNull(cacheValue, nameof(cacheValue)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); - await Task.Run(() => _cache.Set(cacheKey, cacheValue, expiration)); - - _cacheKeys.Add(cacheKey); + await Task.Run(() => + { + _cache.Set(cacheKey, cacheValue, expiration); + _cacheKeys.Add(cacheKey); + }); } /// @@ -226,7 +230,7 @@ public async Task ExistsAsync(string cacheKey) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - return await Task.Run(() => { return _cache.TryGetValue(cacheKey, out object value); }); + return await Task.FromResult(_cache.TryGetValue(cacheKey, out object value)); } /// @@ -272,8 +276,8 @@ public void RemoveByPrefix(string prefix) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); - var keys = _cacheKeys.Where(x => x.StartsWith(prefix.Trim(),StringComparison.OrdinalIgnoreCase)); - if(keys.Count()>0) + var keys = _cacheKeys.Where(x => x.StartsWith(prefix.Trim(), StringComparison.OrdinalIgnoreCase)); + if (keys.Count() > 0) { foreach (var item in keys) { @@ -331,7 +335,7 @@ public async Task SetAllAsync(IDictionary values, TimeSpan expirat var tasks = new List(); foreach (var entry in values) - tasks.Add(SetAsync(entry.Key, entry.Value,expiration)); + tasks.Add(SetAsync(entry.Key, entry.Value, expiration)); await Task.WhenAll(tasks); } @@ -386,7 +390,7 @@ public IDictionary> GetByPrefix(string prefix) where T var keys = _cacheKeys.Where(x => x.StartsWith(prefix.Trim(), StringComparison.OrdinalIgnoreCase)); if (keys.Count() > 0) - { + { foreach (var item in keys) { map[item] = this.Get(item); @@ -402,7 +406,7 @@ public IDictionary> GetByPrefix(string prefix) where T /// Prefix. /// The 1st type parameter. public Task>> GetByPrefixAsync(string prefix) where T : class - { + { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); var keys = _cacheKeys.Where(x => x.StartsWith(prefix.Trim(), StringComparison.OrdinalIgnoreCase)); diff --git a/src/EasyCaching.Interceptor.AspectCore/AspectCoreInterceptorServiceCollectionExtensions.cs b/src/EasyCaching.Interceptor.AspectCore/AspectCoreInterceptorServiceCollectionExtensions.cs index fc62cf2d..2796f1ee 100644 --- a/src/EasyCaching.Interceptor.AspectCore/AspectCoreInterceptorServiceCollectionExtensions.cs +++ b/src/EasyCaching.Interceptor.AspectCore/AspectCoreInterceptorServiceCollectionExtensions.cs @@ -6,6 +6,7 @@ using EasyCaching.Core; using EasyCaching.Core.Internal; using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.DependencyInjection.Extensions; using System; /// @@ -14,41 +15,65 @@ public static class AspectCoreInterceptorServiceCollectionExtensions { /// - /// Configures the easy caching. + /// Configures the AspectCore interceptor. /// - /// The easy caching. + /// The aspect core interceptor. /// Services. public static IServiceProvider ConfigureAspectCoreInterceptor(this IServiceCollection services) { + services.TryAddSingleton(); - services.AddSingleton(); var container = services.ToServiceContainer(); - container.Configure(config => + return container.Configure(config => { config.Interceptors.AddTyped(method => typeof(IEasyCaching).IsAssignableFrom(method.DeclaringType)); - }); - - return container.Build(); + }).Build(); } - + /// - /// Configures the easy caching. + /// Configures the AspectCore interceptor. /// - /// The easy caching. + /// The aspect core interceptor. /// Services. /// Action. - public static IServiceProvider ConfigureAspectCoreInterceptor(this IServiceCollection services, Action action) + public static IServiceProvider ConfigureAspectCoreInterceptor(this IServiceCollection services, Action action) { + services.TryAddSingleton(); + var container = services.ToServiceContainer(); - container.Configure(config => + action(container); + + return container.Configure(config => { config.Interceptors.AddTyped(method => typeof(IEasyCaching).IsAssignableFrom(method.DeclaringType)); - action(config); - }); + }).Build(); + } - return container.Build(); + /// + /// Configures the aspect core interceptor. + /// + /// The aspect core interceptor. + /// Services. + /// Action. + /// If set to true is remove default. + public static IServiceProvider ConfigureAspectCoreInterceptor(this IServiceCollection services, Action action, bool isRemoveDefault) + { + if (isRemoveDefault) + { + services.TryAddSingleton(); + + var container = services.ToServiceContainer(); + + action(container); + + return container.Build(); + } + else + { + return services.ConfigureAspectCoreInterceptor(action); + } } } } diff --git a/src/EasyCaching.Interceptor.Castle/CastleInterceptorServiceCollectionExtensions.cs b/src/EasyCaching.Interceptor.Castle/CastleInterceptorServiceCollectionExtensions.cs index 97eff813..a5754251 100644 --- a/src/EasyCaching.Interceptor.Castle/CastleInterceptorServiceCollectionExtensions.cs +++ b/src/EasyCaching.Interceptor.Castle/CastleInterceptorServiceCollectionExtensions.cs @@ -6,6 +6,7 @@ using EasyCaching.Core; using EasyCaching.Core.Internal; using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.DependencyInjection.Extensions; using System; using System.Reflection; @@ -21,7 +22,7 @@ public static class CastleInterceptorServiceCollectionExtensions /// Services. public static IServiceProvider ConfigureCastleInterceptor(this IServiceCollection services) { - services.AddSingleton(); + services.TryAddSingleton(); var builder = new ContainerBuilder(); builder.Populate(services); @@ -37,6 +38,58 @@ public static IServiceProvider ConfigureCastleInterceptor(this IServiceCollectio .InterceptedBy(typeof(EasyCachingInterceptor)); return new AutofacServiceProvider(builder.Build()); - } + } + + /// + /// Configures the castle interceptor. + /// + /// The castle interceptor. + /// Services. + /// Action. + public static IServiceProvider ConfigureCastleInterceptor(this IServiceCollection services, Action action) + { + services.TryAddSingleton(); + + var builder = new ContainerBuilder(); + builder.Populate(services); + + var assembly = Assembly.GetCallingAssembly(); + builder.RegisterType(); + + builder.RegisterAssemblyTypes(assembly) + .Where(type => typeof(IEasyCaching).IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract) + .AsImplementedInterfaces() + .InstancePerLifetimeScope() + .EnableInterfaceInterceptors() + .InterceptedBy(typeof(EasyCachingInterceptor)); + + action(builder); + + return new AutofacServiceProvider(builder.Build()); + } + + /// + /// Configures the castle interceptor. + /// + /// The castle interceptor. + /// Services. + /// Action. + /// If set to true is remove default. + public static IServiceProvider ConfigureCastleInterceptor(this IServiceCollection services, Action action, bool isRemoveDefault) + { + if (isRemoveDefault) + { + services.TryAddSingleton(); + + var builder = new ContainerBuilder(); + builder.Populate(services); + action(builder); + return new AutofacServiceProvider(builder.Build()); + } + else + { + return services.ConfigureCastleInterceptor(action); + } + } } } diff --git a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs index ddb23d80..436ae2d4 100644 --- a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs +++ b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs @@ -214,11 +214,11 @@ public bool Exists(string cacheKey) /// /// The async. /// Cache key. - public Task ExistsAsync(string cacheKey) + public async Task ExistsAsync(string cacheKey) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - return Task.Run(() => { return _memcachedClient.TryGet(this.HandleCacheKey(cacheKey), out object obj); }); + return await Task.FromResult(_memcachedClient.TryGet(this.HandleCacheKey(cacheKey), out object obj)); } /// @@ -379,12 +379,12 @@ public IDictionary> GetAll(IEnumerable cacheKey foreach (var item in values) { if (item.Value != null) - result.Add(item.Key,new CacheValue(item.Value, true)); + result.Add(item.Key, new CacheValue(item.Value, true)); else - result.Add(item.Key,CacheValue.NoValue); + result.Add(item.Key, CacheValue.NoValue); } - - return result; + + return result; } /// @@ -396,19 +396,19 @@ public IDictionary> GetAll(IEnumerable cacheKey public async Task>> GetAllAsync(IEnumerable cacheKeys) where T : class { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); - + var values = await _memcachedClient.GetAsync(cacheKeys); var result = new Dictionary>(); foreach (var item in values) { if (item.Value != null) - result.Add(item.Key,new CacheValue(item.Value, true)); + result.Add(item.Key, new CacheValue(item.Value, true)); else - result.Add(item.Key,CacheValue.NoValue); + result.Add(item.Key, CacheValue.NoValue); } - - return result; + + return result; } /// @@ -442,7 +442,7 @@ public void RemoveAll(IEnumerable cacheKeys) ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); foreach (var item in cacheKeys.Distinct()) - Remove(item); + Remove(item); } /// @@ -456,7 +456,7 @@ public async Task RemoveAllAsync(IEnumerable cacheKeys) var tasks = new List(); foreach (var item in cacheKeys.Distinct()) - tasks.Add(RemoveAsync(item)); + tasks.Add(RemoveAsync(item)); await Task.WhenAll(tasks); } diff --git a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs index 08e25927..cf95f208 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs @@ -83,6 +83,23 @@ protected override async Task RemoveByPrefixAsync_Should_Succeed() Assert.NotEqual(prefixValue, afterPrefixValue.Value); } + [Fact] + public void CacheKey_Length_GT_250_Should_Call_SHA1() + { + var cacheKey = ""; + var part = "1000000000"; + + for (int i = 0; i < 26; i++) + cacheKey += part; + + var cacheValue = "value"; + + _provider.Set(cacheKey, cacheValue, _defaultTs); + + var val = _provider.Get(cacheKey); + Assert.True(val.HasValue); + } + [Fact] protected override void GetByPrefix_Should_Succeed() { diff --git a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs index b40219da..1a6b67c6 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs @@ -16,22 +16,5 @@ public MemoryCachingProviderTest() _provider = serviceProvider.GetService(); _defaultTs = TimeSpan.FromSeconds(30); } - - [Fact] - public void CacheKey_Length_GT_250_Should_Call_SHA1() - { - var cacheKey = ""; - var part = "1000000000"; - - for (int i = 0; i < 26; i++) - cacheKey += part; - - var cacheValue = "value"; - - _provider.Set(cacheKey, cacheValue, _defaultTs); - - var val = _provider.Get(cacheKey); - Assert.True(val.HasValue); - } } } diff --git a/test/EasyCaching.UnitTests/Infrastructure/ITestInterface.cs b/test/EasyCaching.UnitTests/Infrastructure/ITestInterface.cs new file mode 100644 index 00000000..38559c21 --- /dev/null +++ b/test/EasyCaching.UnitTests/Infrastructure/ITestInterface.cs @@ -0,0 +1,12 @@ +namespace EasyCaching.UnitTests.Infrastructure +{ + public interface ITestInterface + { + + } + + public class TestInterface : ITestInterface + { + + } +} diff --git a/test/EasyCaching.UnitTests/InterceptorTests/AspectCoreInterceptorTest.cs b/test/EasyCaching.UnitTests/InterceptorTests/AspectCoreInterceptorTest.cs index ea0d6b46..ab545609 100755 --- a/test/EasyCaching.UnitTests/InterceptorTests/AspectCoreInterceptorTest.cs +++ b/test/EasyCaching.UnitTests/InterceptorTests/AspectCoreInterceptorTest.cs @@ -1,5 +1,7 @@ namespace EasyCaching.UnitTests { + using AspectCore.Configuration; + using AspectCore.Injector; using EasyCaching.Core; using EasyCaching.InMemory; using EasyCaching.Interceptor.AspectCore; @@ -9,28 +11,16 @@ namespace EasyCaching.UnitTests using System.Threading; using Xunit; - public class AspectCoreInterceptorTest + public abstract class BaseAspectCoreInterceptorTest { - private readonly IEasyCachingProvider _cachingProvider; + protected IEasyCachingProvider _cachingProvider; - private readonly IAspectCoreExampleService _service; + protected IAspectCoreExampleService _service; - private readonly IEasyCachingKeyGenerator _keyGenerator; - - public AspectCoreInterceptorTest() - { - IServiceCollection services = new ServiceCollection(); - services.AddTransient(); - services.AddDefaultInMemoryCache(); - IServiceProvider serviceProvider = services.ConfigureAspectCoreInterceptor(); - - _cachingProvider = serviceProvider.GetService(); - _service = serviceProvider.GetService(); - _keyGenerator = serviceProvider.GetService(); - } + protected IEasyCachingKeyGenerator _keyGenerator; [Fact] - public void Interceptor_Attribute_Method_Should_Handle_Caching() + protected virtual void Interceptor_Attribute_Method_Should_Handle_Caching() { var tick1 = _service.GetCurrentUTC(); @@ -42,7 +32,7 @@ public void Interceptor_Attribute_Method_Should_Handle_Caching() } [Fact] - public void Interceptor_Attribute_Method_Should_Handle_Caching_Twice() + protected virtual void Interceptor_Attribute_Method_Should_Handle_Caching_Twice() { var tick1 = _service.GetCurrentUTC(); @@ -55,7 +45,7 @@ public void Interceptor_Attribute_Method_Should_Handle_Caching_Twice() [Fact] - public void Not_Interceptor_Attribute_Method_Should_Not_Handle_Caching() + protected virtual void Not_Interceptor_Attribute_Method_Should_Not_Handle_Caching() { var tick1 = _service.GetCurrentUTCTick(); @@ -67,7 +57,7 @@ public void Not_Interceptor_Attribute_Method_Should_Not_Handle_Caching() } [Fact] - public void Put_Should_Succeed() + protected virtual void Put_Should_Succeed() { var str = _service.PutTest(1); @@ -82,7 +72,7 @@ public void Put_Should_Succeed() } [Fact] - public void Evict_Should_Succeed() + protected virtual void Evict_Should_Succeed() { System.Reflection.MethodInfo method = typeof(AspectCoreExampleService).GetMethod("EvictTest"); @@ -103,7 +93,7 @@ public void Evict_Should_Succeed() } [Fact] - public void EvictAll_Should_Succeed() + protected virtual void EvictAll_Should_Succeed() { System.Reflection.MethodInfo method = typeof(AspectCoreExampleService).GetMethod("EvictAllTest"); @@ -126,6 +116,86 @@ public void EvictAll_Should_Succeed() Assert.False(after1.HasValue); Assert.False(after2.HasValue); } + } + public class AspectCoreInterceptorTest : BaseAspectCoreInterceptorTest + { + public AspectCoreInterceptorTest() + { + IServiceCollection services = new ServiceCollection(); + services.AddTransient(); + services.AddDefaultInMemoryCache(); + IServiceProvider serviceProvider = services.ConfigureAspectCoreInterceptor(); + + _cachingProvider = serviceProvider.GetService(); + _service = serviceProvider.GetService(); + _keyGenerator = serviceProvider.GetService(); + } } -} \ No newline at end of file + + //public class AspectCoreInterceptorWithActionTest : BaseAspectCoreInterceptorTest + //{ + // private ITestInterface _interface; + + // public AspectCoreInterceptorWithActionTest() + // { + // IServiceCollection services = new ServiceCollection(); + // services.AddTransient(); + // services.AddDefaultInMemoryCache(); + + // Action action = x => + // { + // x.AddType(); + // }; + + // IServiceProvider serviceProvider = services.ConfigureAspectCoreInterceptor(action); + + // _cachingProvider = serviceProvider.GetService(); + // _service = serviceProvider.GetService(); + // _keyGenerator = serviceProvider.GetService(); + + // _interface = serviceProvider.GetService(); + // } + + // [Fact] + // public void Add_Other_Types_Should_Succeed() + // { + // Assert.IsType(_interface); + // } + //} + + //public class AspectCoreInterceptorWithActionAndIsRemoveDefaultTest : BaseAspectCoreInterceptorTest + //{ + // private ITestInterface _interface; + + // public AspectCoreInterceptorWithActionAndIsRemoveDefaultTest() + // { + // IServiceCollection services = new ServiceCollection(); + // services.AddTransient(); + // services.AddDefaultInMemoryCache(); + + // Action action = x => + // { + // x.AddType(); + // x.Configure(config => + // { + // config.Interceptors.AddTyped(method => typeof(Core.Internal.IEasyCaching).IsAssignableFrom(method.DeclaringType)); + // }); + // }; + + // IServiceProvider serviceProvider = services.ConfigureAspectCoreInterceptor(action, true); + + // _cachingProvider = serviceProvider.GetService(); + // _service = serviceProvider.GetService(); + // _keyGenerator = serviceProvider.GetService(); + + // _interface = serviceProvider.GetService(); + // } + + // [Fact] + // public void Add_Other_Types_Should_Succeed() + // { + // Assert.IsType(_interface); + // } + //} +} diff --git a/test/EasyCaching.UnitTests/InterceptorTests/CastleInterceptorTest.cs b/test/EasyCaching.UnitTests/InterceptorTests/CastleInterceptorTest.cs index e7649018..b718d837 100755 --- a/test/EasyCaching.UnitTests/InterceptorTests/CastleInterceptorTest.cs +++ b/test/EasyCaching.UnitTests/InterceptorTests/CastleInterceptorTest.cs @@ -9,29 +9,20 @@ namespace EasyCaching.UnitTests using Xunit; using Microsoft.Extensions.DependencyInjection; using EasyCaching.Core; + using Autofac; + using System.Reflection; + using Autofac.Extras.DynamicProxy; - public class CastleInterceptorTest + public abstract class BaseCastleInterceptorTest { - private readonly IEasyCachingProvider _cachingProvider; + protected IEasyCachingProvider _cachingProvider; - private readonly ICastleExampleService _service; + protected ICastleExampleService _service; - private readonly IEasyCachingKeyGenerator _keyGenerator; - - public CastleInterceptorTest() - { - IServiceCollection services = new ServiceCollection(); - services.AddTransient(); - services.AddDefaultInMemoryCache(); - IServiceProvider serviceProvider = services.ConfigureCastleInterceptor(); - - _cachingProvider = serviceProvider.GetService(); - _service = serviceProvider.GetService(); - _keyGenerator = serviceProvider.GetService(); - } + protected IEasyCachingKeyGenerator _keyGenerator; [Fact] - public void Interceptor_Attribute_Method_Should_Handle_Caching() + protected virtual void Interceptor_Attribute_Method_Should_Handle_Caching() { var tick1 = _service.GetCurrentUTC(); @@ -43,7 +34,7 @@ public void Interceptor_Attribute_Method_Should_Handle_Caching() } [Fact] - public void Interceptor_Attribute_Method_Should_Handle_Caching_Twice() + protected virtual void Interceptor_Attribute_Method_Should_Handle_Caching_Twice() { var tick1 = _service.GetCurrentUTC(); @@ -56,7 +47,7 @@ public void Interceptor_Attribute_Method_Should_Handle_Caching_Twice() [Fact] - public void Not_Interceptor_Attribute_Method_Should_Not_Handle_Caching() + protected virtual void Not_Interceptor_Attribute_Method_Should_Not_Handle_Caching() { var tick1 = _service.GetCurrentUTCTick(); @@ -69,13 +60,13 @@ public void Not_Interceptor_Attribute_Method_Should_Not_Handle_Caching() [Fact] - public void Put_Should_Succeed() + protected virtual void Put_Should_Succeed() { var str = _service.PutTest(1); System.Reflection.MethodInfo method = typeof(CastleExampleService).GetMethod("PutTest"); - var key = _keyGenerator.GetCacheKey(method, new object[]{1,"123" } ,"CastleExample"); + var key = _keyGenerator.GetCacheKey(method, new object[] { 1, "123" }, "CastleExample"); var value = _cachingProvider.Get(key); @@ -84,7 +75,7 @@ public void Put_Should_Succeed() } [Fact] - public void Evict_Should_Succeed() + protected virtual void Evict_Should_Succeed() { System.Reflection.MethodInfo method = typeof(CastleExampleService).GetMethod("EvictTest"); @@ -105,7 +96,7 @@ public void Evict_Should_Succeed() } [Fact] - public void EvictAll_Should_Succeed() + protected virtual void EvictAll_Should_Succeed() { System.Reflection.MethodInfo method = typeof(AspectCoreExampleService).GetMethod("EvictAllTest"); @@ -129,4 +120,92 @@ public void EvictAll_Should_Succeed() Assert.False(after2.HasValue); } } -} \ No newline at end of file + + public class CastleInterceptorTest : BaseCastleInterceptorTest + { + public CastleInterceptorTest() + { + IServiceCollection services = new ServiceCollection(); + services.AddTransient(); + services.AddDefaultInMemoryCache(); + IServiceProvider serviceProvider = services.ConfigureCastleInterceptor(); + + _cachingProvider = serviceProvider.GetService(); + _service = serviceProvider.GetService(); + _keyGenerator = serviceProvider.GetService(); + } + } + + public class CastleInterceptorWithActionTest : BaseCastleInterceptorTest + { + private ITestInterface _interface; + + public CastleInterceptorWithActionTest() + { + IServiceCollection services = new ServiceCollection(); + services.AddTransient(); + services.AddDefaultInMemoryCache(); + + Action action = x => + { + x.RegisterType().As(); + }; + + IServiceProvider serviceProvider = services.ConfigureCastleInterceptor(action); + + _cachingProvider = serviceProvider.GetService(); + _service = serviceProvider.GetService(); + _keyGenerator = serviceProvider.GetService(); + + _interface = serviceProvider.GetService(); + } + + [Fact] + public void Add_Other_Types_Should_Succeed() + { + Assert.IsType(_interface); + } + } + + public class CastleInterceptorWithActionAndIsRemoveDefaultTest : BaseCastleInterceptorTest + { + private ITestInterface _interface; + + public CastleInterceptorWithActionAndIsRemoveDefaultTest() + { + IServiceCollection services = new ServiceCollection(); + services.AddTransient(); + services.AddDefaultInMemoryCache(); + + Action action = x => + { + x.RegisterType().As(); + + var assembly = Assembly.GetExecutingAssembly(); + x.RegisterType(); + + x.RegisterAssemblyTypes(assembly) + .Where(type => typeof(IEasyCaching).IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract) + .AsImplementedInterfaces() + .InstancePerLifetimeScope() + .EnableInterfaceInterceptors() + .InterceptedBy(typeof(EasyCachingInterceptor)); + }; + + IServiceProvider serviceProvider = services.ConfigureCastleInterceptor(action, true); + + _cachingProvider = serviceProvider.GetService(); + _service = serviceProvider.GetService(); + _keyGenerator = serviceProvider.GetService(); + + _interface = serviceProvider.GetService(); + } + + [Fact] + public void Add_Other_Types_Should_Succeed() + { + Assert.IsType(_interface); + } + } + +}