private void RemoveLeastValuableNode() { LinkedListNode lln = mLru.PeekLast(); LruNode node = (LruNode)lln.NodeValue; Delete(node); }
public object GetObject(object key) { if (key == null) { throw new ArgumentNullException("key"); } object data = null; lock (mLock) { RemoveExpiredElements(); LruNode node = (LruNode)mMap[key]; if (node == null) { // cache miss } else if (node.IsExpired()) { Debug.WriteLine("found expired object in cache for key " + key); Delete(node); } else { Debug.WriteLine("cache hit for key " + key); RevalueNode(node); data = node.Data; } return(data); } }
public object RemoveNodeAndExpiredElements(object key) { RemoveExpiredElements(); LruNode node = (LruNode)mMap[key]; object oldVal = null; if (node != null) { Delete(node); oldVal = node.Data; } return(oldVal); }
public void AddObject(object userKey, object cacheObject) { if (userKey == null) { throw new ArgumentNullException("userKey"); } if (cacheObject == null) { throw new ArgumentNullException("cacheObject"); } lock (mLock) { RemoveNodeAndExpiredElements(userKey); ShrinkToSize(mMaxSize - 1); LruNode node = CreateNodeAndAppendMap(userKey, cacheObject); } }
/** * Method CreateNodeAndAppendMap */ private LruNode CreateNodeAndAppendMap(object userKey, object cacheObject) { LruNode node = new LruNode(); node.Key = userKey; node.Data = cacheObject; node.NodePtr = mLru.AddFirst(node); if (mTimeoutSeconds < 0) { node.Timeout = -1; } else { long currTicks = System.DateTime.UtcNow.Ticks; node.Timeout = currTicks + mTimeoutSeconds * 10000000; //Debug.WriteLine("currTicks = " + currTicks + " timeoutSecs = " + mTimeoutSeconds // + " expiry = " + node.Timeout); } mMap[userKey] = node; return(node); }
private void Delete(LruNode node) { mLru.Remove(node.NodePtr); mMap.Remove(node.Key); }
private void RevalueNode(LruNode node) { mLru.MoveToFirst(node.NodePtr); }