Skip to content

Add GetCacheKeys() method to allow for partial key removal operations #153

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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: 9 additions & 0 deletions Console.Net461/Program.cs
Original file line number Diff line number Diff line change
@@ -22,6 +22,15 @@ static void Main()
item = cache.GetOrAdd("Program.Main.Person", () => Tuple.Create("Joe Blogs", DateTime.UtcNow));

System.Console.WriteLine(item.Item1);

System.Console.WriteLine("Enumerating keys...");
foreach (var key in cache.GetCacheKeys().Keys)
{
System.Console.WriteLine($"{key}");
}
System.Console.WriteLine("Finished enumerating keys...");

System.Console.ReadLine();
}
}
}
89 changes: 89 additions & 0 deletions LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
@@ -1125,5 +1126,93 @@ public void TryGetReturnsCachedValueAndTrue()

Assert.IsFalse(contains2);
}

[Test]
public void ListOfCacheKeysContainsAllKeysAfterCallingAdd()
{
List<string> keys = new List<string>(){"one", "two", "three", "four", "five"};


foreach (var key in keys)
{
sut.Add(key, key.Reverse());
}

var cachedKeys = sut.GetCacheKeys().Keys;

Assert.IsTrue(keys.Intersect(cachedKeys).Count() == keys.Count);
}

[Test]
public void ListOfCacheKeysContainsAllKeysAfterCallingGetOrAdd()
{
List<string> keys = new List<string>() { "one", "two", "three", "four", "five" };

foreach (var key in keys)
{
sut.GetOrAdd(key, () => key.Reverse());
}

var cachedKeys = sut.GetCacheKeys().Keys;

Assert.IsTrue(keys.Intersect(cachedKeys).Count() == keys.Count);
}

[Test]
public void ListOfCacheKeysContainsSomeKeysAfterCallingAddAndRemovingOne()
{
List<string> keys = new List<string>() { "one", "two", "three", "four", "five" };

foreach (var key in keys)
{
sut.Add(key, key.Reverse());
}

// remove three
sut.Remove("three");

var cachedKeys = sut.GetCacheKeys().Keys.ToList();

Assert.IsTrue(!cachedKeys.Contains("three"), "Three should be gone");
}

[Test]
public void ListOfCacheKeysContainsAllKeysAfterCallingGetOrAddAndRemovingOne()
{
List<string> keys = new List<string>() { "one", "two", "three", "four", "five" };

foreach (var key in keys)
{
sut.GetOrAdd(key, () => key.Reverse());
}

// remove three
sut.Remove("three");

var cachedKeys = sut.GetCacheKeys().Keys.ToList();

Assert.IsTrue(!cachedKeys.Contains("three"), "Three should be gone");
}

public void ListOfCacheKeysIsEmptyAfterRemovingThemAll()
{
List<string> keys = new List<string>() {"one", "two", "three", "four", "five"};

foreach (var key in keys)
{
sut.GetOrAdd(key, () => key.Reverse());
}

var cachedKeys = sut.GetCacheKeys().Keys.ToList();
Assert.IsTrue(keys.Intersect(cachedKeys).Count() == keys.Count);

foreach (var key in cachedKeys)
{
sut.Remove(key);
}

var cachedKeys2 = sut.GetCacheKeys().Keys.ToList();
Assert.AreEqual(cachedKeys2.Count, 0, "Should be zero keys left");
}
}
}
14 changes: 14 additions & 0 deletions LazyCache/CachedItemMeta.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace LazyCache
{
public class CachedItemMeta
{
public CachedItemMeta()
{
this.CreatedDate = DateTime.UtcNow;
}

public DateTime CreatedDate { get; set; }
}
}
35 changes: 34 additions & 1 deletion LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LazyCache.Providers;
@@ -15,6 +17,8 @@ public class CachingService : IAppCache

private readonly int[] keyLocks;

private ConcurrentDictionary<string, CachedItemMeta> cacheKeyDictionary;

public CachingService() : this(DefaultCacheProvider)
{
}
@@ -24,6 +28,7 @@ public CachingService(Lazy<ICacheProvider> cacheProvider)
this.cacheProvider = cacheProvider ?? throw new ArgumentNullException(nameof(cacheProvider));
var lockCount = Math.Max(Environment.ProcessorCount * 8, 32);
keyLocks = new int[lockCount];
cacheKeyDictionary = new ConcurrentDictionary<string, CachedItemMeta>();
}

public CachingService(Func<ICacheProvider> cacheProviderFactory)
@@ -32,7 +37,7 @@ public CachingService(Func<ICacheProvider> cacheProviderFactory)
cacheProvider = new Lazy<ICacheProvider>(cacheProviderFactory);
var lockCount = Math.Max(Environment.ProcessorCount * 8, 32);
keyLocks = new int[lockCount];

cacheKeyDictionary = new ConcurrentDictionary<string, CachedItemMeta>();
}

public CachingService(ICacheProvider cache) : this(() => cache)
@@ -69,6 +74,7 @@ public virtual void Add<T>(string key, T item, MemoryCacheEntryOptions policy)
ValidateKey(key);

CacheProvider.Set(key, item, policy);
RememberCacheKey(key);
}

public virtual T Get<T>(string key)
@@ -113,6 +119,7 @@ object CacheFactory(ICacheEntry entry) =>
var result = addItemFactory(entry);
SetAbsoluteExpirationFromRelative(entry);
EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy<T>(entry.PostEvictionCallbacks);
RememberCacheKey(entry.Key.ToString());
return result;
});

@@ -137,6 +144,7 @@ object CacheFactory(ICacheEntry entry) =>
if (valueHasChangedType)
{
CacheProvider.Remove(key);
this.RemoveRememberedCacheKey(key);

// acquire lock again
hash = (uint)key.GetHashCode() % (uint)keyLocks.Length;
@@ -158,6 +166,7 @@ object CacheFactory(ICacheEntry entry) =>
catch //addItemFactory errored so do not cache the exception
{
CacheProvider.Remove(key);
this.RemoveRememberedCacheKey(key);
throw;
}
}
@@ -175,6 +184,7 @@ public virtual void Remove(string key)
{
ValidateKey(key);
CacheProvider.Remove(key);
RemoveRememberedCacheKey(key);
}

public virtual ICacheProvider CacheProvider => cacheProvider.Value;
@@ -206,6 +216,7 @@ object CacheFactory(ICacheEntry entry) =>
var result = addItemFactory(entry);
SetAbsoluteExpirationFromRelative(entry);
EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy<T>(entry.PostEvictionCallbacks);
RememberCacheKey(entry.Key.ToString());
return result;
});

@@ -226,6 +237,7 @@ object CacheFactory(ICacheEntry entry) =>
if (valueHasChangedType)
{
CacheProvider.Remove(key);
this.RemoveRememberedCacheKey(key);

// acquire lock
hash = (uint)key.GetHashCode() % (uint)keyLocks.Length;
@@ -244,17 +256,26 @@ object CacheFactory(ICacheEntry entry) =>


if (result.IsCanceled || result.IsFaulted)
{
CacheProvider.Remove(key);
this.RemoveRememberedCacheKey(key);
}

return await result.ConfigureAwait(false);
}
catch //addItemFactory errored so do not cache the exception
{
CacheProvider.Remove(key);
this.RemoveRememberedCacheKey(key);
throw;
}
}

public Dictionary<string, CachedItemMeta> GetCacheKeys()
{
return cacheKeyDictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}

protected virtual T GetValueFromLazy<T>(object item, out bool valueHasChangedType)
{
valueHasChangedType = false;
@@ -342,5 +363,17 @@ protected virtual void ValidateKey(string key)
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentOutOfRangeException(nameof(key), "Cache keys cannot be empty or whitespace");
}

protected void RememberCacheKey(string key)
{
var meta = new CachedItemMeta();
cacheKeyDictionary.AddOrUpdate(key, meta, (oldKey, oldValue) => meta);
}

protected void RemoveRememberedCacheKey(string key)
{
CachedItemMeta remove;
cacheKeyDictionary.TryRemove(key, out remove);
}
}
}
2 changes: 2 additions & 0 deletions LazyCache/IAppCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;

@@ -22,5 +23,6 @@ public interface IAppCache
Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory);
Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory, MemoryCacheEntryOptions policy);
void Remove(string key);
Dictionary<string, CachedItemMeta> GetCacheKeys();
}
}
7 changes: 7 additions & 0 deletions LazyCache/Mocks/MockCachingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;

@@ -11,6 +12,7 @@ namespace LazyCache.Mocks
public class MockCachingService : IAppCache
{
public ICacheProvider CacheProvider { get; } = new MockCacheProvider();

public CacheDefaults DefaultCachePolicy { get; set; } = new CacheDefaults();

public T Get<T>(string key)
@@ -57,5 +59,10 @@ public bool TryGetValue<T>(string key, out object value)
value = default(T);
return true;
}

public Dictionary<string, CachedItemMeta> GetCacheKeys()
{
return new Dictionary<string, CachedItemMeta>();
}
}
}