From 3ad74da83f8cd252b5452e85dc5138f849fd4f46 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Thu, 6 Dec 2018 20:29:11 +0100 Subject: [PATCH] Made LRUCAche generic. --- .../Caching/LRUCache.cs | 44 +++++++------------ .../Caching/LRUCacheItem.cs | 7 +-- .../Caching/LRUCacheTests.cs | 2 +- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/Squidex.Infrastructure/Caching/LRUCache.cs b/src/Squidex.Infrastructure/Caching/LRUCache.cs index 4632d2ab0..7132eb1d3 100644 --- a/src/Squidex.Infrastructure/Caching/LRUCache.cs +++ b/src/Squidex.Infrastructure/Caching/LRUCache.cs @@ -9,23 +9,19 @@ using System.Collections.Generic; namespace Squidex.Infrastructure.Caching { - public sealed class LRUCache + public sealed class LRUCache { - private readonly Dictionary> cacheMap = new Dictionary>(); - private readonly LinkedList cacheHistory = new LinkedList(); + private readonly Dictionary>> cacheMap = new Dictionary>>(); + private readonly LinkedList> cacheHistory = new LinkedList>(); private readonly int capacity; public LRUCache(int capacity) { - Guard.GreaterThan(capacity, 0, nameof(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)) { node.Value.Value = value; @@ -37,28 +33,24 @@ namespace Squidex.Infrastructure.Caching 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 { Key = key, Value = value }; - node = new LinkedListNode(cacheItem); + node = new LinkedListNode>(cacheItem); - cacheMap.Add(key, node); - cacheHistory.AddLast(node); + cacheMap.Add(key, 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)) { cacheMap.Remove(key); @@ -70,10 +62,8 @@ namespace Squidex.Infrastructure.Caching 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; if (cacheMap.TryGetValue(key, out var node)) @@ -89,10 +79,8 @@ namespace Squidex.Infrastructure.Caching return false; } - public bool Contains(object key) + public bool Contains(TKey key) { - Guard.NotNull(key, nameof(key)); - return cacheMap.ContainsKey(key); } diff --git a/src/Squidex.Infrastructure/Caching/LRUCacheItem.cs b/src/Squidex.Infrastructure/Caching/LRUCacheItem.cs index ddf885afd..ff9cb3eef 100644 --- a/src/Squidex.Infrastructure/Caching/LRUCacheItem.cs +++ b/src/Squidex.Infrastructure/Caching/LRUCacheItem.cs @@ -9,9 +9,10 @@ namespace Squidex.Infrastructure.Caching { - internal class LRUCacheItem + internal class LRUCacheItem { - public object Key; - public object Value; + public TKey Key; + + public TValue Value; } } \ No newline at end of file diff --git a/tests/Squidex.Infrastructure.Tests/Caching/LRUCacheTests.cs b/tests/Squidex.Infrastructure.Tests/Caching/LRUCacheTests.cs index 4551fbe57..52e029193 100644 --- a/tests/Squidex.Infrastructure.Tests/Caching/LRUCacheTests.cs +++ b/tests/Squidex.Infrastructure.Tests/Caching/LRUCacheTests.cs @@ -11,7 +11,7 @@ namespace Squidex.Infrastructure.Caching { public class LRUCacheTests { - private readonly LRUCache sut = new LRUCache(10); + private readonly LRUCache sut = new LRUCache(10); private readonly string key = "Key"; [Fact]