当前位置:网站首页>简单好用的缓存库 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等淘汰算法。如果你在开发时候有这方面的需求,不妨试试看,相信一定会喜欢上的!
参考资料:

边栏推荐
- Presto中Limit算子的处理过程
- In depth analysis of Flink fine-grained resource management
- MySQL Chapter 15 lock
- Sqlmap learning (sqli labs as an example)
- leetcode: 49. 字母异位词分组
- Measurement fitting based on Halcon learning -- Practice [2]
- Milan video technology exchange meeting sharing
- CVPR2022教程 | 马里兰大学《机器学习遥感处理:农业与粮食安全》教程
- HNU network counting experiment: experiment I application protocol and packet analysis experiment (using Wireshark)
- JS disable the browser PDF printing and downloading functions (pdf.js disable the printing and downloading functions)
猜你喜欢

智云健康上市在即:长期亏损,美年健康俞熔已退出,未来难言乐观

Presto中Limit算子的处理过程

NARI radar's IPO meeting: it plans to raise nearly 1billion yuan. Bao Xiaojun and his wife are Canadians

Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing Bing

How does idea package its own projects into jar packages

用idea建立第一个网站

Where is win11 screen recording data saved? Win11 screen recording data storage location

InfiniBand& RDMA
![[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF](/img/50/bb9e73cb4eabcef4bee8f6d5b2fcb6.png)
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF

HNU network counting experiment: experiment I application protocol and packet analysis experiment (using Wireshark)
随机推荐
Leetcode topic [array] -18- sum of four numbers
Summary of digital image processing knowledge points
Application runtime layotto enters CNCF cloud native panorama
HNU计网实验:实验一 应用协议与数据包分析实验(使用Wireshark)
No nonsense, code practice will help you master strong caching and negotiation caching!
HLS. JS: past, present and future
InfiniBand& RDMA
. Thoughts on software trends in the 20th anniversary of net
Is there any risk for CICC fortune to open an account? Is it safe to open an account?
Obsidian基础教程
What is a code baseline?
In depth analysis of Flink fine-grained resource management
了解有哪几个C标准&了解C编译管道
[proteus simulation] Arduino uno+ key controls 2-bit digital tube countdown
js 限制鼠标移动范围
When we talk about the metauniverse, what are we talking about?
Processing of limit operator in Presto
Zero Trust: break the passive development mode of "attack and defense" and build a "moat" for enterprise safety
AbstractFactory Abstract Factory
2022giao考游记