/// <summary> /// 移除元素 /// </summary> /// <param name="key">键</param> public void Remove(TKey key) { Guard.Requires <ArgumentNullException>(key != null); CacheNode <TKey, TVal> result; if (!lruCache.TryGetValue(key, out result)) { return; } lruCache.Remove(key); if (result == tail) { tail = result.Backward; } if (result == header) { header = result.Forward; } if (result.Backward != null) { result.Backward.Forward = result.Forward; } }
/// <summary> /// 移除最后一个元素 /// </summary> private void RemoveLeastUsed() { lruCache.Remove(tail.KeyValue.Key); tail.Backward.Forward = null; tail = tail.Backward; }