Beispiel #1
0
        private void RemoveLeastValuableNode()
        {
            LinkedListNode lln  = mLru.PeekLast();
            LruNode        node = (LruNode)lln.NodeValue;

            Delete(node);
        }
Beispiel #2
0
        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);
            }
        }
Beispiel #3
0
        public object RemoveNodeAndExpiredElements(object key)
        {
            RemoveExpiredElements();
            LruNode node   = (LruNode)mMap[key];
            object  oldVal = null;

            if (node != null)
            {
                Delete(node);
                oldVal = node.Data;
            }
            return(oldVal);
        }
Beispiel #4
0
        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);
            }
        }
Beispiel #5
0
        /**
         * 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);
        }
Beispiel #6
0
 private void Delete(LruNode node)
 {
     mLru.Remove(node.NodePtr);
     mMap.Remove(node.Key);
 }
Beispiel #7
0
 private void RevalueNode(LruNode node)
 {
     mLru.MoveToFirst(node.NodePtr);
 }