using System.Collections.Generic;
private const int NotFound = -1;
private readonly int _capacity;
private readonly LinkedList<CacheItem> _queue;
private readonly Dictionary<int, LinkedListNode<CacheItem>> _cache;
public LRUCache(int capacity)
_queue = new LinkedList<CacheItem>();
_cache = new Dictionary<int, LinkedListNode<CacheItem>>(capacity);
if (_cache.ContainsKey(key) == false)
var cacheItem = node.Value;
public void Put(int key, int value)
if (_cache.TryGetValue(key, out var node))
var cacheItem = node.Value;
node = new LinkedListNode<CacheItem>(new CacheItem(key, value));
if (_queue.Count > _capacity)
var lastNode = _queue.Last;
var cacheItem = lastNode.Value;
_cache.Remove(cacheItem.Key);
private void BringToFirst(LinkedListNode<CacheItem> node)
public int Value { get; set; }
public CacheItem(int key, int value)