golang-lru源码解析(1)项目结构、simplelru

本文详细解析了golang-lru库中的项目结构,重点介绍了LRUCache接口和simplelru模块。simplelru通过list+map的数据结构实现LRU缓存,提供包括Add、Get、Remove等操作,同时讨论了各方法的时间复杂度。文章还概述了LRU结构及元素淘汰策略,并预告了后续将探讨的2Q缓存

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


项目git地址点我
golang-lru提供了三个模块。LRU,2Q,ARC三个模块。本文主要介绍整个包的结构,以及为三个模块提供最底层功能的simplelru。

项目结构

在这里插入图片描述
整个项目结构非常简单,对外提供三个平行的模块在各自的文件中。simplelru提供了底层的simplelru结构,并且提供了一个lru接口。

LRUCache接口

LRUCache定义了lru缓存的方法。这里注释给的比较详细,直接照搬了。

// LRUCache is the interface for simple LRU cache.
type LRUCache interface {
   
    // Adds a value to the cache, returns true if an eviction occurred and
    // updates the "recently used"-ness of the key.
    Add(key, value interface{
   }) bool
    
    
    // Returns key's value from the cache and
    // updates the "recently used"-ness of the key. #value, isFound
    Get(key interface{
   }) (value interface{
   }, ok bool)
    
    // Checks if a key exists in cache without updating the recent-ness.
    Contains(key interface{
   }) (ok bool)
    
    
    // Returns key's value without updating the "recently used"-ness of the key.
    // 这里额外提供的Peek方法用来根据key获取value,并且不更新这个元素在队列中的信息
    Peek(key interface{
   }) (value interface{
   }, ok bool)
    
    // Removes a key from the cache.
    Remove(key interface{
   }) bool
    
    // Removes the oldest entry from cache.
    RemoveOldest() (interface{
   }, interface{
   }, bool)
    
    // Returns the oldest entry from the cache. #key, value, isFound
    GetOldest() (interface{
   }, interface{
   }, bool)
    
    // Returns a slice of the keys in the cache, from oldest to newest.
    Keys() []interface{
   }
    
    // Returns the number of items in the cache.
    Len() int
    
    // Clears all cache entries.
    Purge()
    
    // Resizes cache, returning number evicted
    Resize(int) int
}

simplelru

entry结构

节点元素就是简单的kv结构

type entry struct {
   
    key interface{
   }
    value interface{
   }
}

LRU结构

典型的LRU结构,通过list + map的方式进行存储,链表用来给队列排序,方便淘汰队尾的元素,map结构保证激活元素的时候查询的复杂度为O(N)。还添加了一个回调方法当元素离开队列后被调用

type EvictCallback func(key interface{
   }, value interface{
   
golang-lru 是一个 Golang 语言实现的 LRU 缓存库,它提供了一个简单易用的 API 用于创建和使用 LRU 缓存。 下面是一个简单的使用示例: ```go package main import ( "fmt" "github.com/hashicorp/golang-lru" ) func main() { // 创建一个 LRU 缓存,容量为 2 cache, _ := lru.New(2) // 添加两个元素到缓存中 cache.Add("key1", "value1") cache.Add("key2", "value2") // 从缓存中获取一个元素 if v, ok := cache.Get("key1"); ok { fmt.Println(v.(string)) } // 添加一个新元素到缓存中,此时缓存容量已满,会自动淘汰最久未使用的元素 "key2" cache.Add("key3", "value3") // 遍历缓存中的所有元素 for _, k := range cache.Keys() { if v, ok := cache.Get(k); ok { fmt.Println(k, v.(string)) } } } ``` 运行上述代码,将会输出: ``` value1 key1 value1 key3 value3 ``` 在这个示例中,我们首先使用 `lru.New()` 函数创建了一个容量为 2 的 LRU 缓存。然后我们添加了两个元素到缓存中,并从缓存中获取了一个元素。接着我们添加了一个新元素,此时缓存已满,会自动淘汰最久未使用的元素 "key2"。最后我们遍历了缓存中的所有元素,输出了它们的键和值。 除了 `Add()` 和 `Get()` 方法外,golang-lru 还提供了 `Remove()` 和 `Contains()` 方法来删除和判断缓存中是否存在某个元素,以及 `Len()` 和 `Clear()` 方法来获取缓存中元素的数量和清空缓存。 golang-lru 还支持并发安全,你可以通过 `NewWithOptions()` 函数创建一个并发安全的 LRU 缓存,具体用法请参考官方文档:https://pkg.go.dev/github.com/hashicorp/golang-lru
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值