網站首頁 編程語言 正文
1 基本概念
LRU是一個老生常談的問題,即最近最少使用,LRU是Least Recently Used
的縮寫,是一種操作系統中常用的頁面置換算法,選擇最近最久未使用的頁面予以淘汰。該算法賦予每個頁面一個訪問字段,用來記錄一個頁面自上次被訪問以來所經歷的時間 t,當須淘汰一個頁面時,選擇現有頁面中其 t 值最大的,即最近最少使用的頁面予以淘汰。
實現LRU基本的數據結構:Map+LinkedList
一般規則:
- 添加數據時,將新增數據節點放在頭指針,尾結點部分大于最大長度時刪除。
- 刪除數據時,先按照Map的規則進行查找,再根據鏈表規則進行刪除。
- 查找數據時,按照
Map
進行查找,沒有則返回空,有則返回該數據的值并移動到頭節點。
2 代碼實現
package main import "fmt" var head *Node var end *Node type Node struct { ? ?Key ? string ? ?Value string ? ?pre ? *Node ? ?next ?*Node } func (n *Node) Init(key string, value string) { ? ?n.Key = key ? ?n.Value = value } type LRUCache struct { ? ?Capacity int ? ? ? ? ? ? ?//頁面初始化大小 ? ?Size ? ? int ? ? ? ? ? ? ?//頁面實際大小 ? ?Map ? ? ?map[string]*Node //具體的cache } func GetLRUCache(capacity int) *LRUCache { ? ?lruCache := LRUCache{Capacity: capacity} ? ?lruCache.Map = make(map[string]*Node, capacity) ? ?return &lruCache } func (l *LRUCache) get(key string) string { ? ?if v, ok := l.Map[key]; ok { ? ? ? l.refreshNode(v) ? ? ? return v.Value ? ?} else { ? ? ? return "null" ? ?} } func (l *LRUCache) put(key, value string) { ? ?if v, ok := l.Map[key]; !ok { ? ? ? if len(l.Map) >= l.Capacity { ? ? ? ? ?oldKey := l.removeNode(head) ? ? ? ? ?delete(l.Map, oldKey) ? ? ? } ? ? ? node := Node{Key: key, Value: value} ? ? ? l.addNode(&node) ? ? ? l.Map[key] = &node ? ?} else { ? ? ? v.Value = value ? ? ? l.refreshNode(v) ? ?} } func (l *LRUCache) refreshNode(node *Node) { ? ?if node == end { ? ? ? return ? ?} ? ?l.removeNode(node) ? ?l.addNode(node) } func (l *LRUCache) removeNode(node *Node) string { ? ?if node == end { ? ? ? end = end.pre ? ?} else if node == head { ? ? ? head = head.next ? ?} else { ? ? ? node.pre.next = node.next ? ? ? node.next.pre = node.pre ? ?} ? ?return node.Key } func (l *LRUCache) addNode(node *Node) { ? ?if end != nil { ? ? ? end.next = node ? ? ? node.pre = end ? ? ? node.next = nil ? ?} ? ?end = node ? ?if head == nil { ? ? ? head = node ? ?} }
3 測試使用
func main() { ? ?lruCache := GetLRUCache(3) ? ?lruCache.put("001", "1") ? ?lruCache.put("002", "2") ? ?lruCache.put("003", "3") ? ?lruCache.put("004", "4") ? ?lruCache.put("005", "5") ? ?lruCache.get("002") ? ?fmt.Println(lruCache.get("001")) ? ?fmt.Println(lruCache.get("002")) ? ?fmt.Print(lruCache.Map) }
原文鏈接:https://blog.csdn.net/Mr_YanMingXin/article/details/123022534
相關推薦
- 2022-07-25 C++全面精通類與對象_C 語言
- 2023-01-05 C++模板?index_sequence使用示例詳解_C 語言
- 2022-07-13 Android Canvas - save() & restore()
- 2023-02-15 React?中使用?react-i18next?國際化的過程(react-i18next?的基本用法
- 2022-07-23 python實現雙向鏈表原理_python
- 2023-01-10 利用C#實現修改圖片透明度功能_C#教程
- 2022-05-05 Flutter如何保證數據操作原子性詳解_Android
- 2022-03-17 c++智能指針unique_ptr的使用_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支