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