Golang:golang-lru一个基于双向链表实现的LRU缓存工具
【摘要】
This provides the lru package which implements a fixed-size thread safe LRU cache. It is based on the ...
This provides the lru package which implements a fixed-size thread safe LRU cache. It is based on the cache in Groupcache.
译文:这提供了实现固定大小线程安全lru缓存的lru包。它基于Groupcache中的缓存。
LRU:Least Recently Used,最近最少使用
文档
安装
go get github.com/hashicorp/golang-lru
示例
package main
import (
"fmt"
lru "github.com/hashicorp/golang-lru"
)
func main() {
cache, _ := lru.New(128)
// 添加
cache.Add("name", "Tom")
cache.Add("age", 12)
// 获取
name, ok := cache.Get("name")
if ok {
fmt.Println(name)
// Tom
}
// 获取最老的键值
key, value, ok := cache.GetOldest()
if ok {
fmt.Println(key, value)
// age 12
}
// 移除缓存中的key
cache.Remove("name")
// 获取元素个数
len := cache.Len()
fmt.Printf("len: %v\n", len)
// len: 1
}
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/126739698
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)