Browse Source

Made LRUCAche generic.

pull/337/head
Sebastian Stehle 8 years ago
parent
commit
3ad74da83f
  1. 44
      src/Squidex.Infrastructure/Caching/LRUCache.cs
  2. 7
      src/Squidex.Infrastructure/Caching/LRUCacheItem.cs
  3. 2
      tests/Squidex.Infrastructure.Tests/Caching/LRUCacheTests.cs

44
src/Squidex.Infrastructure/Caching/LRUCache.cs

@ -9,23 +9,19 @@ using System.Collections.Generic;
namespace Squidex.Infrastructure.Caching namespace Squidex.Infrastructure.Caching
{ {
public sealed class LRUCache public sealed class LRUCache<TKey, TValue>
{ {
private readonly Dictionary<object, LinkedListNode<LRUCacheItem>> cacheMap = new Dictionary<object, LinkedListNode<LRUCacheItem>>(); private readonly Dictionary<TKey, LinkedListNode<LRUCacheItem<TKey, TValue>>> cacheMap = new Dictionary<TKey, LinkedListNode<LRUCacheItem<TKey, TValue>>>();
private readonly LinkedList<LRUCacheItem> cacheHistory = new LinkedList<LRUCacheItem>(); private readonly LinkedList<LRUCacheItem<TKey, TValue>> cacheHistory = new LinkedList<LRUCacheItem<TKey, TValue>>();
private readonly int capacity; private readonly int capacity;
public LRUCache(int capacity) public LRUCache(int capacity)
{ {
Guard.GreaterThan(capacity, 0, nameof(capacity));
this.capacity = capacity; this.capacity = capacity;
} }
public bool Set(object key, object value) public bool Set(TKey key, TValue value)
{ {
Guard.NotNull(key, nameof(key));
if (cacheMap.TryGetValue(key, out var node)) if (cacheMap.TryGetValue(key, out var node))
{ {
node.Value.Value = value; node.Value.Value = value;
@ -37,28 +33,24 @@ namespace Squidex.Infrastructure.Caching
return true; return true;
} }
else
if (cacheMap.Count >= capacity)
{ {
if (cacheMap.Count >= capacity) RemoveFirst();
{ }
RemoveFirst();
}
var cacheItem = new LRUCacheItem { Key = key, Value = value }; var cacheItem = new LRUCacheItem<TKey, TValue> { Key = key, Value = value };
node = new LinkedListNode<LRUCacheItem>(cacheItem); node = new LinkedListNode<LRUCacheItem<TKey, TValue>>(cacheItem);
cacheMap.Add(key, node); cacheMap.Add(key, node);
cacheHistory.AddLast(node); cacheHistory.AddLast(node);
return false; return false;
}
} }
public bool Remove(object key) public bool Remove(TKey key)
{ {
Guard.NotNull(key, nameof(key));
if (cacheMap.TryGetValue(key, out var node)) if (cacheMap.TryGetValue(key, out var node))
{ {
cacheMap.Remove(key); cacheMap.Remove(key);
@ -70,10 +62,8 @@ namespace Squidex.Infrastructure.Caching
return false; return false;
} }
public bool TryGetValue(object key, out object value) public bool TryGetValue(TKey key, out object value)
{ {
Guard.NotNull(key, nameof(key));
value = null; value = null;
if (cacheMap.TryGetValue(key, out var node)) if (cacheMap.TryGetValue(key, out var node))
@ -89,10 +79,8 @@ namespace Squidex.Infrastructure.Caching
return false; return false;
} }
public bool Contains(object key) public bool Contains(TKey key)
{ {
Guard.NotNull(key, nameof(key));
return cacheMap.ContainsKey(key); return cacheMap.ContainsKey(key);
} }

7
src/Squidex.Infrastructure/Caching/LRUCacheItem.cs

@ -9,9 +9,10 @@
namespace Squidex.Infrastructure.Caching namespace Squidex.Infrastructure.Caching
{ {
internal class LRUCacheItem internal class LRUCacheItem<TKey, TValue>
{ {
public object Key; public TKey Key;
public object Value;
public TValue Value;
} }
} }

2
tests/Squidex.Infrastructure.Tests/Caching/LRUCacheTests.cs

@ -11,7 +11,7 @@ namespace Squidex.Infrastructure.Caching
{ {
public class LRUCacheTests public class LRUCacheTests
{ {
private readonly LRUCache sut = new LRUCache(10); private readonly LRUCache<string, int> sut = new LRUCache<string, int>(10);
private readonly string key = "Key"; private readonly string key = "Key";
[Fact] [Fact]

Loading…
Cancel
Save