Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the LRU caching mechanism of Redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to implement the LRU caching mechanism of Redis". In the daily operation, I believe that many people have doubts about how to implement the LRU caching mechanism of Redis. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the LRU caching mechanism of Redis". Next, please follow the editor to study!

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 not found

If (! map.containsKey (key)) {

Return-1

}

/ / refresh the data when you find it

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 the longest useless, or you can override 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); }

The second implementation (double linked list + 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)

Tail = new ListNode (- 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 a two-way linked list node

Private class ListNode {

Int key

Int val

ListNode pre

ListNode next

/ / initialize two-way linked list

Public ListNode (int key,int val) {

This.key = key

This.val = val

Pre = null

Next = null

}

}

}

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

}

}

At this point, the study on "how to implement Redis's LRU caching mechanism" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report