Ejemplo n.º 1
0
        public bool Add(int key, int cacheItem)
        {
            LRUCacheNode cacheNode = null;

            if (cacheMap.TryGetValue(key.ToString(), out cacheNode))
            {
                return(false);
            }
            cacheNode = new LRUCacheNode()
            {
                Key   = key,
                Value = cacheItem
            };
            if (head == null)
            {
                head = cacheNode;
                tail = cacheNode;
            }
            else
            {
                if (cacheMap.Count >= capacity)
                {
                    this.RemoveTailNode();
                }
                cacheNode.Next = head;
                head.Previous  = cacheNode;
                head           = cacheNode;
            }
            return(cacheMap.TryAdd(key.ToString(), cacheNode));
        }
Ejemplo n.º 2
0
        public bool Remove(int key)
        {
            LRUCacheNode cacheNode;

            if (this.cacheMap.TryGetValue(key.ToString(), out cacheNode))
            {
                if (cacheNode.Previous != null)
                {
                    cacheNode.Previous.Next = cacheNode.Next;
                }
                else
                {
                    head = cacheNode.Next;
                }

                if (cacheNode.Next != null)
                {
                    cacheNode.Next.Previous = cacheNode.Previous;
                }
                else
                {
                    tail = cacheNode.Previous;
                }

                cacheNode.Next     = null;
                cacheNode.Previous = null;
                return(this.cacheMap.TryRemove(key.ToString(), out cacheNode));
            }
            return(false);
        }
Ejemplo n.º 3
0
        private void RemoveTailNode()
        {
            LRUCacheNode cacheNode;

            if (this.cacheMap.TryRemove(tail.Key.ToString(), out cacheNode))
            {
                tail.Previous.Next = null;
                tail = tail.Previous;
            }
        }
Ejemplo n.º 4
0
        public void DisplayCacheMap()
        {
            StringBuilder sb        = new StringBuilder();
            LRUCacheNode  cacheNode = head;

            while (cacheNode != null)
            {
                sb.Append(cacheNode.Key);
                cacheNode = cacheNode.Next;
                if (cacheNode != null)
                {
                    sb.Append("-->");
                }
            }
            Console.WriteLine(sb.ToString());
        }
Ejemplo n.º 5
0
        public object GetCacheItem(int key)
        {
            LRUCacheNode cacheNode;

            if (this.cacheMap.TryGetValue(key.ToString(), out cacheNode))
            {
                if (cacheNode.Previous != null)
                {
                    cacheNode.Previous.Next = cacheNode.Next;
                }

                if (cacheNode.Next != null)
                {
                    cacheNode.Next.Previous = cacheNode.Previous;
                }

                cacheNode.Next = head;
                head.Previous  = cacheNode;
                head           = cacheNode;
                return(cacheNode.Value);
            }
            return(null);
        }