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

Added a RemoveAll function to clear the entire cache #91

Open
wants to merge 4 commits 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
9 changes: 5 additions & 4 deletions LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LazyCache.Providers;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

// ReSharper disable once CheckNamespace - MS guidelines say put DI registration in this NS
namespace Microsoft.Extensions.DependencyInjection
Expand All @@ -15,8 +16,8 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services)
if (services == null) throw new ArgumentNullException(nameof(services));

services.AddOptions();
services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
services.TryAdd(ServiceDescriptor.Singleton<ICacheProvider, MemoryCacheProvider>());
services.TryAdd(ServiceDescriptor.Singleton<ICacheProvider, MemoryCacheProvider>(serviceProvider =>
new MemoryCacheProvider(() => new MemoryCache(new MemoryCacheOptions()))));

services.TryAdd(ServiceDescriptor.Singleton<IAppCache, CachingService>());

Expand All @@ -30,8 +31,8 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services,
if (implementationFactory == null) throw new ArgumentNullException(nameof(implementationFactory));

services.AddOptions();
services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
services.TryAdd(ServiceDescriptor.Singleton<ICacheProvider, MemoryCacheProvider>());
services.TryAdd(ServiceDescriptor.Singleton<ICacheProvider, MemoryCacheProvider>(serviceProvider =>
new MemoryCacheProvider(() => new MemoryCache(new MemoryCacheOptions()))));

services.TryAdd(ServiceDescriptor.Singleton<IAppCache>(implementationFactory));

Expand Down
5 changes: 3 additions & 2 deletions LazyCache.Ninject/LazyCacheModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using LazyCache.Providers;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Ninject;
using Ninject.Modules;

namespace LazyCache
Expand All @@ -23,8 +24,8 @@ public LazyCacheModule(Func<IAppCache> implementationFactory)
public override void Load()
{
Bind<IOptions<MemoryCacheOptions>>().ToConstant(Options.Create(new MemoryCacheOptions()));
Bind<IMemoryCache>().To<MemoryCache>().InSingletonScope();
Bind<ICacheProvider>().To<MemoryCacheProvider>().InSingletonScope();
Bind<ICacheProvider>().To<MemoryCacheProvider>().InSingletonScope()
.WithConstructorArgument<Func<IMemoryCache>>(context => () => new MemoryCache(context.Kernel.Get<IOptions<MemoryCacheOptions>>()));

if (implementationFactory == null)
Bind<IAppCache>().To<CachingService>().InSingletonScope();
Expand Down
15 changes: 14 additions & 1 deletion LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void BeforeEachTest()

private static CachingService BuildCache()
{
return new CachingService(new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions())));
return new CachingService(new MemoryCacheProvider(() => new MemoryCache(new MemoryCacheOptions())));
}

private IAppCache sut;
Expand Down Expand Up @@ -755,5 +755,18 @@ public void RemovedItemCannotBeRetrievedFromCache()
sut.Remove(TestKey);
Assert.Null(sut.Get<object>(TestKey));
}

[Test]
public void NoItemsCanBeRetrievedFromCacheAfterRemoveAll()
{
var testKey2 = "TestKey2";
sut.Add(TestKey, new object());
sut.Add(testKey2, new object());
Assert.NotNull(sut.Get<object>(TestKey));
Assert.NotNull(sut.Get<object>(testKey2));
sut.RemoveAll();
Assert.Null(sut.Get<object>(TestKey));
Assert.Null(sut.Get<object>(testKey2));
}
}
}
7 changes: 6 additions & 1 deletion LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public CachingService(ICacheProvider cache) : this(() => cache)

public static Lazy<ICacheProvider> DefaultCacheProvider { get; set; }
= new Lazy<ICacheProvider>(() =>
new MemoryCacheProvider(
new MemoryCacheProvider(() =>
new MemoryCache(
new MemoryCacheOptions())
));
Expand Down Expand Up @@ -122,6 +122,11 @@ public virtual void Remove(string key)
CacheProvider.Remove(key);
}

public virtual void RemoveAll()
{
CacheProvider.RemoveAll();
}

public virtual ICacheProvider CacheProvider => cacheProvider.Value;

public virtual async Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory)
Expand Down
2 changes: 2 additions & 0 deletions LazyCache/IAppCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public interface IAppCache
Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory);

void Remove(string key);

void RemoveAll();
}
}
1 change: 1 addition & 0 deletions LazyCache/ICacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface ICacheProvider : IDisposable
object Get(string key);
object GetOrCreate<T>(string key, Func<ICacheEntry, T> func);
void Remove(string key);
void RemoveAll();
Task<T> GetOrCreateAsync<T>(string key, Func<ICacheEntry, Task<T>> func);
}
}
4 changes: 4 additions & 0 deletions LazyCache/Mocks/MockCacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public void Remove(string key)
{
}

public void RemoveAll()
{
}

public Task<T> GetOrCreateAsync<T>(string key, Func<ICacheEntry, Task<T>> func)
{
return func(null);
Expand Down
4 changes: 4 additions & 0 deletions LazyCache/Mocks/MockCachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public void Remove(string key)
{
}

public void RemoveAll()
{
}

public Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory)
{
return addItemFactory(new MockCacheEntry(key));
Expand Down
14 changes: 11 additions & 3 deletions LazyCache/Providers/MemoryCacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace LazyCache.Providers
{
public class MemoryCacheProvider : ICacheProvider
{
internal readonly IMemoryCache cache;
internal readonly Func<IMemoryCache> cacheFactory;
internal IMemoryCache cache;

public MemoryCacheProvider(IMemoryCache cache)
public MemoryCacheProvider(Func<IMemoryCache> cacheFactory)
{
this.cache = cache;
this.cacheFactory = cacheFactory;
cache = cacheFactory();
}

public void Set(string key, object item, MemoryCacheEntryOptions policy)
Expand All @@ -33,6 +35,12 @@ public void Remove(string key)
cache.Remove(key);
}

public void RemoveAll()
{
var oldCache = System.Threading.Interlocked.Exchange(ref cache, cacheFactory());
oldCache?.Dispose();
}

public Task<T> GetOrCreateAsync<T>(string key, Func<ICacheEntry, Task<T>> factory)
{
return cache.GetOrCreateAsync(key, factory);
Expand Down