当前位置:网站首页>简单好用的缓存库 gcache
简单好用的缓存库 gcache
2022-06-25 21:33:00 【15231181628】
1.前言
开发时,如果你需要对数据进行临时缓存,按照一定的淘汰策略,那么gcache你一定不要错过。 gcache golang的缓存库。它支持可扩展的Cache,可以选择 LFU,LRU、ARC等淘汰算法。
2.特性
gcache 有很多特性:
支持过期淘汰算法Cache,比如 LFU, LRU和ARC。 Goroutine安全。 支持事件处理程序,淘汰、清除、添加。(可选) 自动加载缓存,如果它不存在。(可选) … ….
更多功能特性请查看:gcache
3.快速安装
直接get即可使用。
$ go get -u https://github.com/bluele/gcache
4.简单举例
package main
import (
"github.com/bluele/gcache"
"fmt"
)
func main() {
gc := gcache.New(20).
LRU().
Build()
gc.Set("key", "ok")
value, err := gc.Get("key")
if err != nil {
panic(err)
}
fmt.Println("Get:", value)
}
执行,控制台输出如下:
Get: ok
5.设置淘汰时间举例
package main
import (
"github.com/bluele/gcache"
"fmt"
"time"
)
func main() {
gc := gcache.New(20).
LRU().
Build()
gc.SetWithExpire("key", "ok", time.Second*10)
value, _ := gc.Get("key")
fmt.Println("Get:", value)
// Wait for value to expire
time.Sleep(time.Second*10)
value, err := gc.Get("key")
if err != nil {
panic(err)
}
fmt.Println("Get:", value)
}
执行,控制台输出如下:
Get: ok
panic: Key not found.
goroutine 1 [running]:
main.main()
/Users/laocheng/work/code/market-data-backend/utils/t/2.go:22 +0x21b
exit status 2
可以看到,一开始获取成功;但是超时时间设定后,过期删除,无法获取到了。
6.其他算法举例
6.1 最不经常使用(LFU)
func main() {
// size: 10
gc := gcache.New(10).
LFU().
Build()
gc.Set("key", "value")
}
6.2 最近使用最少的(LRU)
func main() {
// size: 10
gc := gcache.New(10).
LRU().
Build()
gc.Set("key", "value")
}
6.3 自适应替换缓存(ARC) 在LRU和LFU之间不断平衡,以提高综合结果。
func main() {
// size: 10
gc := gcache.New(10).
ARC().
Build()
gc.Set("key", "value")
}
7.添加hanlder使用
func main() {
gc := gcache.New(2).
AddedFunc(func(key, value interface{}) {
fmt.Println("added key:", key)
}).
Build()
for i := 0; i < 3; i++ {
gc.Set(i, i*i)
}
}
执行,控制台输出如下:
added key: 0
added key: 1
added key: 2
可以在set时候做一些额外的处理。
6.总结
gcache 是一个非常简单,又好用的缓存库,它支持LFU,LRU、ARC等淘汰算法。如果你在开发时候有这方面的需求,不妨试试看,相信一定会喜欢上的!
参考资料:

边栏推荐
- Various special effect cases of Experiment 3
- Oracle case: does index range scan really not read multiple blocks?
- Build the first website with idea
- Winget: the "Winget" item cannot be recognized as the name of cmdlet, function, script file or runnable program. Win11 Winget cannot be used to solve this problem
- Data annotation in the second half: growth flywheel under PLG mode Manfu Technology
- China coated abrasive tools industry market depth analysis and development strategy consulting report 2022-2028
- Leetcode topic [array] -18- sum of four numbers
- Bear market guide | some essential lessons and specific survival rules
- Conglin environmental protection IPO meeting: annual profit of more than 200million to raise 2.03 billion
- Will neuvector be the next popular cloud native security artifact?
猜你喜欢

Zhiyun health is about to go public: long-term losses, meinian health Yu Rong has withdrawn, and it is difficult to be optimistic about the future

Obsidian basic tutorial

IAAs, PAAS, SaaS, baas, FAAS differences

Win11开始菜单右键空白?Win11开始菜单右键没反应解决方法

Presto中Limit算子的处理过程

Various special effect cases of Experiment 3

idea怎么把自己的项目打包成jar包
Data annotation in the second half: growth flywheel under PLG mode Manfu Technology

剖析虚幻渲染体系(16)- 图形驱动的秘密

The latest (2022-2-16) vulnerability of WordPress plug-in bee collection (XSS, WordPress user name exposure, arbitrary article publishing) is repeated
随机推荐
聊聊Adapter模式
Adaptive streaming playback statistics set
Obsidian basic tutorial
Practice of product library platform nexus of Devops
Obsidian基础教程
Win11开始菜单右键空白?Win11开始菜单右键没反应解决方法
什么是代码基线?
Generic cmaf container for efficient cross format low latency delivery
Pat 1050 string subtraction (20 points) string find
Presto中Limit算子的处理过程
HNU计网实验:实验五 网络层与链路层协议分析(PacketTracer)
Obsidian基础教程
Factorymethod factory method
HLS. JS: past, present and future
How to use Matplotlib library to realize enlarged display of graphic local data
Build the first website with idea
Devops之制品库平台nexus实践
Analysis of China's tractor manufacturing and operation situation and forecast report of prospect trend 2022-2028
Fujilai pharmaceutical has passed the registration: the annual revenue is nearly 500million yuan. Xiangyun once illegally traded foreign exchange
MySQL Chapter 15 lock