如何利用Go語(yǔ)言實(shí)現(xiàn)LRU?Cache
1 基本概念
LRU是一個(gè)老生常談的問(wèn)題,即最近最少使用,LRU是Least Recently Used的縮寫(xiě),是一種操作系統(tǒng)中常用的頁(yè)面置換算法,選擇最近最久未使用的頁(yè)面予以淘汰。該算法賦予每個(gè)頁(yè)面一個(gè)訪問(wèn)字段,用來(lái)記錄一個(gè)頁(yè)面自上次被訪問(wèn)以來(lái)所經(jīng)歷的時(shí)間 t,當(dāng)須淘汰一個(gè)頁(yè)面時(shí),選擇現(xiàn)有頁(yè)面中其 t 值最大的,即最近最少使用的頁(yè)面予以淘汰。
實(shí)現(xiàn)LRU基本的數(shù)據(jù)結(jié)構(gòu):Map+LinkedList

一般規(guī)則:
- 添加數(shù)據(jù)時(shí),將新增數(shù)據(jù)節(jié)點(diǎn)放在頭指針,尾結(jié)點(diǎn)部分大于最大長(zhǎng)度時(shí)刪除。
- 刪除數(shù)據(jù)時(shí),先按照Map的規(guī)則進(jìn)行查找,再根據(jù)鏈表規(guī)則進(jìn)行刪除。
- 查找數(shù)據(jù)時(shí),按照
Map進(jìn)行查找,沒(méi)有則返回空,有則返回該數(shù)據(jù)的值并移動(dòng)到頭節(jié)點(diǎn)。
2 代碼實(shí)現(xiàn)
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 ? ? ? ? ? ? ?//頁(yè)面初始化大小
? ?Size ? ? int ? ? ? ? ? ? ?//頁(yè)面實(shí)際大小
? ?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 測(cè)試使用
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)
}
到此這篇關(guān)于如何利用Go語(yǔ)言實(shí)現(xiàn)LRU Cache的文章就介紹到這了,更多相關(guān)Go實(shí)現(xiàn)LRU Cache內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文詳解Go語(yǔ)言中的有限狀態(tài)機(jī)FSM
有限狀態(tài)機(jī)(Finite?State?Machine,F(xiàn)SM)是一種數(shù)學(xué)模型,用于描述系統(tǒng)在不同狀態(tài)下的行為和轉(zhuǎn)移條件。本文主要來(lái)和大家簡(jiǎn)單講講Go語(yǔ)言中的有限狀態(tài)機(jī)FSM的使用,需要的可以參考一下2023-04-04
Go語(yǔ)言學(xué)習(xí)教程之goroutine和通道的示例詳解
這篇文章主要通過(guò)A?Tour?of?Go中的例子進(jìn)行學(xué)習(xí),以此了解Go語(yǔ)言中的goroutine和通道,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09
Go語(yǔ)言操作redis數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Go語(yǔ)言操作redis數(shù)據(jù)庫(kù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
Go項(xiàng)目在linux服務(wù)器的部署詳細(xì)步驟
在今天的軟件開(kāi)發(fā)中,使用Linux作為操作系統(tǒng)的比例越來(lái)越高,而Golang語(yǔ)言則因?yàn)槠涓咝А⒑?jiǎn)潔和并發(fā)性能等特點(diǎn),也被越來(lái)越多的開(kāi)發(fā)者所青睞,這篇文章主要給大家介紹了關(guān)于Go項(xiàng)目在linux服務(wù)器的部署詳細(xì)步驟,需要的朋友可以參考下2023-09-09
GO語(yǔ)言協(xié)程互斥鎖Mutex和讀寫(xiě)鎖RWMutex用法實(shí)例詳解
這篇文章主要介紹了GO語(yǔ)言協(xié)程互斥鎖Mutex和讀寫(xiě)鎖RWMutex用法詳解,需要的朋友可以參考下2022-04-04

