8.16 LRU Cache Java Edition

Source: Internet
Author: User

LRU Detailed introduction and simple code implementation:
http://blog.csdn.net/beiyetengqing/article/details/7855933

The following is my enhanced LRU cache class, which adds a single case fetch, cache timeout mechanism, and fix a clear () bug

 PackageCom.george.xblog.utils;Importjava.util.Hashtable;ImportJava.util.Map;/** * Created by George.yang on 15/8/15. * * Public  class LruCache {    Private Static Final  class Entry {         PublicString key;//Key         PublicObject value;//value         PublicEntry prev;//Previous node         PublicEntry Next;//Next node         Public LongValidtime;//Valid maximum time}Private StaticLruCache instance; Public Static synchronizedLruCachegetinstance() {if(instance==NULL) {instance =NewLruCache ( -); }returnInstance }Private intCacheSize;PrivateMap<string, entry> nodes;//Cache container    PrivateEntry first;//Chain list head    PrivateEntry last;//Chain footer     Public LruCache(inti) {cacheSize = i; nodes =NewHashtable<string, entry> (i);//Cache container}/** * Get the object in the cache and put it on the front * *     Public<T> TGet(String key) {Entry node = nodes.get (key);if(Node! =NULL) {if(System.currenttimemillis () >node.validtime) {//delete in linked listRemove (node);//Delete in HashtableNodes.remove (key); }Else{Movetohead (node);return(T) Node.value; }        }return NULL; }/** * Add entry to Hashtable and put entry * @param key * @param Timeout time-out, MS * @param Value * /     Public void put(String Key,LongTimeout, Object value) {///See if the Hashtable exists for the entry, and if it exists, only update its valueEntry node = nodes.get (key);if(node = =NULL) {//cache container has exceeded size.            if(Nodes.size () >= cacheSize)                {Nodes.remove (Last.key);            Removelast (); } node =NewEntry ();        } node.validtime = System.currenttimemillis () + timeout; Node.value = value;//Put the most recently used node in the list header, indicating the latest use.Movetohead (node);    Nodes.put (key, node); }/** * Delete entry, note: The delete operation will be executed only if the cache is full */     Public void Remove(String key) {Entry node = nodes.get (key);//delete in linked listRemove (node);//Delete in HashtableNodes.remove (key); }Private void Remove(Entry node) {if(Node! =NULL) {if(Node.prev! =NULL) {node.prev.next = Node.next; }if(Node.next! =NULL) {Node.next.prev = Node.prev; }if(Last = = node) last = Node.prev;if(First = = node) first = Node.next; }    }/** * Delete the tail node of the linked list, even with the last used entry */    Private void Removelast() {//The tail of the chain is not empty, then the end of the chain is pointed to null. Delete the footer (delete the least used cache object)        if(Last! =NULL) {if(Last.prev! =NULL) Last.prev.next =NULL;ElseFirst =NULL;        last = Last.prev; }    }/** * Moves to the list header, indicating that the node is the most recently used */    Private void Movetohead(Entry node) {if(node = = first)return;if(Node.prev! =NULL) Node.prev.next = Node.next;if(Node.next! =NULL) Node.next.prev = Node.prev;if(Last = = node) last = Node.prev;if(First! =NULL) {node.next = first;        First.prev = node;        } first = node; Node.prev =NULL;if(Last = =NULL) last = first; }/ * * Empty cache * /     Public void Clear() {first =NULL; Last =NULL; nodes =NewHashtable<string, entry> (cacheSize); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

8.16 LRU Cache Java Edition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.