public bool Set(TKey key, TValue value) { if (cacheMap.TryGetValue(key, out var node)) { node.Value.Value = value; cacheHistory.Remove(node); cacheHistory.AddLast(node); cacheMap[key] = node; return(true); } if (cacheMap.Count >= capacity) { RemoveFirst(); } var cacheItem = new LRUCacheItem <TKey, TValue> { Key = key, Value = value }; node = new LinkedListNode <LRUCacheItem <TKey, TValue> >(cacheItem); cacheMap.Add(key, node); cacheHistory.AddLast(node); return(false); }
public bool Set(object key, object value) { Guard.NotNull(key, nameof(key)); if (cacheMap.TryGetValue(key, out var node)) { node.Value.Value = value; cacheHistory.Remove(node); cacheHistory.AddLast(node); cacheMap[key] = node; return(true); } else { if (cacheMap.Count >= capacity) { RemoveFirst(); } var cacheItem = new LRUCacheItem { Key = key, Value = value }; node = new LinkedListNode <LRUCacheItem>(cacheItem); cacheMap.Add(key, node); cacheHistory.AddLast(node); return(false); } }