public virtual MruNode <V> Add(V item) { var node = new MruNode <V>(item, this); try { SlimLock.EnterWriteLock(); node.Next = Head; Tail = Tail ?? node; if (Head != null) { Head.Previous = node; } Head = node; ItemCount++; if (ItemCount > Limit) { var deleted = Tail; if (Tail != null) { Tail = Tail.Previous; deleted.Erase(); } ItemCount--; } } finally { SlimLock.ExitWriteLock(); } return(node); }
public TValue ReadOrWrite(TKey key, Func <TValue> valueProvider) { TValue value = default(TValue); if (!Dictionary.TryGetValue(key, out value)) { try { if (SlimLock.TryEnterWriteLock(1000)) { UpdateWaiting(); if (!Dictionary.TryGetValue(key, out value)) { value = Dictionary.GetOrAdd(key, valueProvider()); } } else { throw new Exception("THIS IS CRAP"); } } finally { SlimLock.ExitWriteLock(); } } return(value); }
public void Remove(TKey key) { try { SlimLock.EnterWriteLock(); TValue value; Dictionary.TryRemove(key, out value); } finally { SlimLock.ExitWriteLock(); } }
public virtual void Clear() { try { SlimLock.EnterWriteLock(); var node = Head; while (node != null) { var old = node; node = node.Next; old.Erase(); } ItemCount = 0; Head = null; Tail = null; } finally { SlimLock.ExitWriteLock(); } }