您当前的位置: 首页 >  缓存

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Golang:golang-lru一个基于双向链表实现的LRU缓存工具

彭世瑜 发布时间:2022-09-07 10:04:08 ,浏览量:3

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,最近最少使用

文档

  • https://pkg.go.dev/github.com/hashicorp/golang-lru
  • https://github.com/hashicorp/golang-lru

安装

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
}

参考 「Go工具箱」一个基于双向链表实现的LRU缓存工具

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0577s