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

边栏推荐
- Pycharm 2022.1 EAP 2 release
- Factorymethod factory method
- Is it safe to open an account with Caicai securities?
- PHP Chinese word segmentation API, Harbin Institute of technology ltpcloud, naturallanguageprocessing, free, best practices!
- HNU计网实验:实验四 应用层和传输层协议分析(PacketTracer)
- How can the computer tablet be connected to the computer
- 【Proteus仿真】Arduino UNO+按键控制2位数码管倒计时
- Analysis of gpl3.0 license software copyright dispute cases
- AbstractFactory Abstract Factory
- Where is win11 screen recording data saved? Win11 screen recording data storage location
猜你喜欢

How can the computer tablet be connected to the computer

SSH modifies grub in heiqunhui ds918+ system 7.0.1 cfg

CVPR2022教程 | 马里兰大学《机器学习遥感处理:农业与粮食安全》教程

Did you really learn the right game modeling with 3DMAX?

What if win11 cannot delete the folder? Win11 cannot delete folder

图解栈帧运行过程
![Measurement fitting based on Halcon learning -- Practice [2]](/img/2f/098d2c0b55522ae7ba65e34442a7ee.jpg)
Measurement fitting based on Halcon learning -- Practice [2]

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

InfiniBand& RDMA

电脑手写板怎么才能连接电脑使用
随机推荐
挖财证券开户安全嘛?
Tcapulusdb Jun · industry news collection (VI)
China coated abrasive tools industry market depth analysis and development strategy consulting report 2022-2028
“No bean named ‘UserController‘ available“
MySQL modifies multiple tables and adds multiple fields through SQL
If switch branch structure
ASP. Net core uses function switches to control Route Access (Continued) yyds dry inventory
Q5 s905l firmware version 202109
Free cloud function proxy IP pool just released
multiplication table
Exclusive interview with deepmindceo hassabis: we will see a new scientific Renaissance! AI's new achievements in nuclear fusion are officially announced today
Some websites used by Beijing University of technology when graduating
JS disable the browser PDF printing and downloading functions (pdf.js disable the printing and downloading functions)
Jingwei Hengrun is registered through the science and Innovation Board: it plans to raise 5billion yuan, with a 9-month revenue of 2.1 billion yuan
MySQL Chapter 15 lock
电脑手写板怎么才能连接电脑使用
Market depth analysis and development strategy consulting report of China's fire equipment market 2022-2028
"Exclusive interview with IDC people" Suzhou Shengwang: the road of IDC transformation in the post epidemic Era
Pat 1083 list grades (25 points)
idea怎么把自己的项目打包成jar包