diff --git a/LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs b/LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs index 4eb7182..e1b99b3 100644 --- a/LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs +++ b/LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs @@ -10,6 +10,13 @@ namespace Microsoft.Extensions.DependencyInjection // See https://github.com/dotnet/runtime/blob/master/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCacheServiceCollectionExtensions.cs public static class LazyCacheServiceCollectionExtensions { + public static IServiceCollection AddLazyCache(this IServiceCollection services, Action setupAction) + { + services.AddLazyCache(); + services.Configure(setupAction); + return services; + } + public static IServiceCollection AddLazyCache(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); @@ -18,13 +25,12 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services) services.TryAdd(ServiceDescriptor.Singleton()); services.TryAdd(ServiceDescriptor.Singleton()); - services.TryAdd(ServiceDescriptor.Singleton(serviceProvider => - new CachingService( - new Lazy(serviceProvider.GetRequiredService)))); + services.TryAdd(ServiceDescriptor.Singleton()); return services; } + [Obsolete("use other signature for change options")] public static IServiceCollection AddLazyCache(this IServiceCollection services, Func implementationFactory) { diff --git a/LazyCache.UnitTests/AspNetCoreTests.cs b/LazyCache.UnitTests/AspNetCoreTests.cs index deabd8c..1a2a38d 100644 --- a/LazyCache.UnitTests/AspNetCoreTests.cs +++ b/LazyCache.UnitTests/AspNetCoreTests.cs @@ -38,5 +38,21 @@ public void CanResolveCacheFromServiceCollectionAsService() cache.Should().NotBeNull(); result.Should().NotBeNull(); } + + [Test] + public void CanResolveCacheFromServiceCollectionWithOptionsAsService() + { + var container = new ServiceCollection(); + var cacheDurationSeconds = 12345; + container.AddLazyCache(options => options.DefaultCacheDurationSeconds = cacheDurationSeconds); + var provider = container.BuildServiceProvider(); + + var cache = provider.GetService(); + var result = cache?.GetOrAdd("key", () => new object()); + + cache.Should().NotBeNull(); + cache.DefaultCachePolicy.DefaultCacheDurationSeconds.Should().Be(cacheDurationSeconds); + result.Should().NotBeNull(); + } } } diff --git a/LazyCache/CachingService.cs b/LazyCache/CachingService.cs index df091de..c8a31aa 100644 --- a/LazyCache/CachingService.cs +++ b/LazyCache/CachingService.cs @@ -4,6 +4,8 @@ using System.Threading.Tasks; using LazyCache.Providers; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; namespace LazyCache { @@ -13,6 +15,7 @@ public class CachingService : IAppCache private readonly Lazy cacheProvider; private readonly int[] keyLocks; + private readonly LazyCacheOptions _options; public CachingService() : this(DefaultCacheProvider) { @@ -34,6 +37,17 @@ public CachingService(Func cacheProviderFactory) } + [ActivatorUtilitiesConstructor] + public CachingService(ICacheProvider cacheProvider, IOptions options) + { + if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); + _options = options.Value; + this.cacheProvider = new Lazy(() => cacheProvider); + keyLocks = new int[_options.NumberOfKeyLocks]; + DefaultCachePolicy.DefaultCacheDurationSeconds = _options.DefaultCacheDurationSeconds; + + } + public CachingService(ICacheProvider cache) : this(() => cache) { if (cache == null) throw new ArgumentNullException(nameof(cache)); diff --git a/LazyCache/LazyCacheOptions.cs b/LazyCache/LazyCacheOptions.cs new file mode 100644 index 0000000..bb08cf7 --- /dev/null +++ b/LazyCache/LazyCacheOptions.cs @@ -0,0 +1,11 @@ +using System; + +namespace LazyCache +{ + public class LazyCacheOptions + { + public int DefaultCacheDurationSeconds { get; set; } = 60 * 20; + public int NumberOfKeyLocks { get; set; } = Math.Max(Environment.ProcessorCount * 8, 32); + + } +} \ No newline at end of file