"Face Test" LRU algorithm and code implement LRU policy cache

Source: Internet
Author: User

Concept

LRU (least recently used) is to eliminate the data that has not been accessed recently, LRU is based on the assumption that the most recently used data will be used in the future and the probability that the data that has not been accessed will be used in the future is relatively low.

Principle

LRU generally through the form of a linked list of cached data, the newly inserted or accessed data placed on the head of the list, after a certain threshold, automatically eliminate the data at the end of the linked list. An image of the LRU cache elimination process is described. (Image from Network)

Steps:

1. New insert a, place a in the head of the queue

2, the new insert B, the B is placed in the head of the queue, a automatically elected sub-seats.

3, the new Insert C, the C is placed in the head of the queue, B automatically elected sub-seats.

4, new insert D, place D in the head of the queue, C automatically press the second seat.

5, the new Insert E, the E is placed in the head of the queue, D automatically elected sub-seats.

6, the new insert E, put E in the queue head, the queue length is greater than the threshold, automatically the tail data A to eliminate

7. Access data C, and then place C back in the queue header.

8, new Insert E, put E in the queue head, the queue length is greater than the threshold, automatically retire tail data b

Encoding implements LRU policy caching
/*** Created by Zhuhezan on 2018/4/11. * * Using a linked list +hashmap, where concurrency is not considered, so locks are not used in the code*/ Public classLrucache<k, v> {    classCachenode { PublicCachenode before;  PublicObject Key;  PublicObject value;  PublicCachenode after;  PublicCachenode () {}}PrivateHashmap<k, cachenode>caches; Private intmaxcapacity; Private intcurrentcachesize; /*** head node, head node not involved in elimination, just as the first node in the Identity list*/    PrivateCachenode Head; /*** Tail node, tail node does not participate in elimination, just as the last node in the identity list*/    PrivateCachenode Tail;  PublicLRUCache (intmaxcapacity) {         This. maxcapacity =maxcapacity; Caches=NewHashmap<>(maxcapacity); Head= Tail =NewCachenode (); Head.after= tail;//assign a value to the after node of the head to prevent null pointer exceptions from subsequent operationsTail.before = head;//assigning values to tail's before nodes to prevent null pointer exceptions in subsequent operations    }     Public voidput (k K, v V) {cachenode node=Caches.get (k); if(node = =NULL) {            if(Currentcachesize >=maxcapacity) {Caches.remove (tail.before.key);//eliminate the tail elementsRemovelast (); } node=NewCachenode (); Node.key=K; Currentcachesize++; } node.value=v; Movetofirst (node); //LRU Policy, the newly inserted element is placed in the queue headerCaches.put (k, node); }     Public voidMovetofirst (Cachenode node) {cachenode n=Head.after; Head.after=node; Node.before=Head; N.before=node; Node.after=N; }     Public voidRemovelast () {Cachenode n=Tail.before.before; N.after=tail; Tail.before=N; }     PublicObject get (k k) {Cachenode node=Caches.get (k); if(node = =NULL) {            return NULL; } Object v=Node.value;        Movetofirst (node); returnv; }     PublicObject remove (k k) {Cachenode node=Caches.get (k); if(node = =NULL) {            return NULL; } Cachenode Pre=Node.before; Cachenode Next=Node.after; Pre.after=Next; Next.before=Pre;        Caches.remove (k); Currentcachesize--; returnNode.value; }}

The above code in the Bo Master native Test validation Pass (single-threaded operation)

"Face Test" LRU algorithm and code implement LRU policy cache

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.