// DOES NOT LOCK private void Remove(LRUCacheNode <T> node) { LRUCacheNode <T> next = node.Next; LRUCacheNode <T> prev = node.Previous; if (prev != null) { prev.Next = next; } else { first = next; } if (next != null) { next.Previous = prev; } else { last = prev; } node.Next = null; node.Previous = null; }
// DOES NOT LOCK private void Add(LRUCacheNode <T> node) { size++; if (first == null) { first = node; last = node; return; } node.Next = first; first.Previous = node; first = node; first.Previous = null; while (size > capacity) { last = last.Previous; last.Next = null; size--; } }
public T Get(long id) { if (first == null) { return(default(T)); } LRUCacheNode <T> node = first; if (node.Id == id) { return(node.Value); } // Find node // BUG De objectverwijzing is niet op een exemplaar van een object ingesteld. regel hieronder. veroorzaakt, utrecht laden, en ondertussen youtube kijken. while (node.Id != id) { if (node.Next == null) { return(default(T)); } node = node.Next; } lock (writeLock) { // Move node to front if (node != first) { Remove(node); Add(node); } } return(node.Value); }