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 1 commit
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
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
13 changes: 10 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,11 @@ public void Remove(string key)
cache.Remove(key);
}

public void RemoveAll()
{
cache = cacheFactory();
}
migr1 marked this conversation as resolved.
Show resolved Hide resolved

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