Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add LazyCacheOptions and use Options Pattern #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LazyCacheOptions> setupAction)
{
services.AddLazyCache();
services.Configure(setupAction);
return services;
}

public static IServiceCollection AddLazyCache(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
Expand All @@ -18,13 +25,12 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services)
services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
services.TryAdd(ServiceDescriptor.Singleton<ICacheProvider, MemoryCacheProvider>());

services.TryAdd(ServiceDescriptor.Singleton<IAppCache, CachingService>(serviceProvider =>
new CachingService(
new Lazy<ICacheProvider>(serviceProvider.GetRequiredService<ICacheProvider>))));
services.TryAdd(ServiceDescriptor.Singleton<IAppCache, CachingService>());

return services;
}

[Obsolete("use other signature for change options")]
public static IServiceCollection AddLazyCache(this IServiceCollection services,
Func<IServiceProvider, CachingService> implementationFactory)
{
Expand Down
16 changes: 16 additions & 0 deletions LazyCache.UnitTests/AspNetCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAppCache>();
var result = cache?.GetOrAdd("key", () => new object());

cache.Should().NotBeNull();
cache.DefaultCachePolicy.DefaultCacheDurationSeconds.Should().Be(cacheDurationSeconds);
result.Should().NotBeNull();
}
}
}
14 changes: 14 additions & 0 deletions LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -13,6 +15,7 @@ public class CachingService : IAppCache
private readonly Lazy<ICacheProvider> cacheProvider;

private readonly int[] keyLocks;
private readonly LazyCacheOptions _options;

public CachingService() : this(DefaultCacheProvider)
{
Expand All @@ -34,6 +37,17 @@ public CachingService(Func<ICacheProvider> cacheProviderFactory)

}

[ActivatorUtilitiesConstructor]
public CachingService(ICacheProvider cacheProvider, IOptions<LazyCacheOptions> options)
{
if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider));
_options = options.Value;
this.cacheProvider = new Lazy<ICacheProvider>(() => cacheProvider);
keyLocks = new int[_options.NumberOfKeyLocks];
DefaultCachePolicy.DefaultCacheDurationSeconds = _options.DefaultCacheDurationSeconds;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor Suggestion: Consider removing this redundant empty line for cleaner code presentation.

}

public CachingService(ICacheProvider cache) : this(() => cache)
{
if (cache == null) throw new ArgumentNullException(nameof(cache));
Expand Down
11 changes: 11 additions & 0 deletions LazyCache/LazyCacheOptions.cs
Original file line number Diff line number Diff line change
@@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor Suggestion: Consider removing this redundant empty line for cleaner code presentation.

}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including an empty line at the end of a file ensures consistency in version control diffs.