How to implement the LRU caching mechanism of Redis in Java
This article introduces the knowledge of "how to implement the LRU caching mechanism of Redis in Java". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The first implementation (using LinkedHashMap) public class LRUCache {int capacity; Map map; public LRUCache (int capacity) {this.capacity = capacity; map = new LinkedHashMap ();} public int get (int key) {/ / if if (! map.containsKey (key)) {return-1 is not found } / / refresh data Integer value = map.remove (key); map.put (key,value); return value;} public void put (int key,int value) {if (map.containsKey (key)) {map.remove (key); map.put (key,value); return } map.put (key,value); / / beyond capacity, delete the first one that is not used for the longest time, or you can overwrite the removeEldestEntry method if (map.size () > capacity) {map.remove (map.entrySet (). Iterator (). Next (). GetKey ());} public static void main (String [] args) {LRUCache lruCache = new LRUCache (10) For (int I = 0; I
< 10; i++) { lruCache.map.put(i,i); System.out.println(lruCache.map.size()); } System.out.println(lruCache.map); lruCache.put(10,200); System.out.println(lruCache.map); } 第二种实现(双链表+hashmap)public class LRUCache { private int capacity; private Mapmap; private ListNode head; private ListNode tail; public LRUCache2(int capacity){ this.capacity = capacity; map = new HashMap(); head = new ListNode(-1,-1); tail = new ListNode(-1,-1); head.next = tail; tail.pre = head; } public int get(int key){ if (!map.containsKey(key)){ return -1; } ListNode node = map.get(key); node.pre.next = node.next; node.next.pre = node.pre; return node.val; } public void put(int key,int value){ if (get(key)!=-1){ map.get(key).val = value; return; } ListNode node = new ListNode(key,value); map.put(key,node); moveToTail(node); if (map.size() >Capacity) {map.remove (head.next.key); head.next = head.next.next; head.next.pre = head;}} / / move the node to the tail private void moveToTail (ListNode node) {node.pre = tail.pre; tail.pre = node; node.pre.next = node; node.next = tail } / / define bi-directional linked list node private class ListNode {int key; int val; ListNode pre; ListNode next; / / initialize bi-directional linked list public ListNode (int key,int val) {this.key = key; this.val = val; pre = null; next = null;}} supplement
Like the first way, it would be easier to copy removeEldestEntry. Here's a simple demonstration.
Public class LRUCache extends LinkedHashMap {private int capacity; @ Override protected boolean removeEldestEntry (Map.Entry eldest) {return size () > capacity;}} "how to implement the LRU caching mechanism of Redis in Java" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!