当前位置:网站首页>Simple and easy-to-use cache library gcache
Simple and easy-to-use cache library gcache
2022-06-25 22:22:00 【fifteen billion two hundred and thirty-one million one hundred 】
1. Preface
When developing , If you need to cache data temporarily , According to a certain elimination strategy , that gcache You must not miss . gcache golang Cache library for . It supports extensible Cache, You can choose LFU,LRU、ARC Equal elimination algorithm .
2. characteristic
gcache There are many characteristics :
Support expiration elimination algorithm Cache, such as LFU, LRU and ARC. Goroutine Security . Support event handler , Eliminate 、 eliminate 、 add to .( Optional ) Auto load cache , If it doesn't exist .( Optional ) … ….
See... For more features :gcache
3. Fast installation
direct get You can use .
$ go get -u https://github.com/bluele/gcache
4. A simple example
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)
}
perform , The console output is as follows :
Get: ok
5. Example of setting obsolescence time
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)
}
perform , The console output is as follows :
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
You can see , Success at first ; But after the timeout is set , Expired delete , Unable to get .
6. Examples of other algorithms
6.1 The least used (LFU)
func main() {
// size: 10
gc := gcache.New(10).
LFU().
Build()
gc.Set("key", "value")
}
6.2 The least used recently (LRU)
func main() {
// size: 10
gc := gcache.New(10).
LRU().
Build()
gc.Set("key", "value")
}
6.3 Adaptive replacement cache (ARC) stay LRU and LFU Between them , To improve the overall results .
func main() {
// size: 10
gc := gcache.New(10).
ARC().
Build()
gc.Set("key", "value")
}
7. add to hanlder Use
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)
}
}
perform , The console output is as follows :
added key: 0
added key: 1
added key: 2
Can be in set It's time to do some extra processing .
6. summary
gcache It's a very simple , Easy to use cache Library , It supports LFU,LRU、ARC Equal elimination algorithm . If you have this requirement during development , Try it , I'm sure I'll like it !
Reference material :

边栏推荐
- Factorymethod factory method
- [proteus simulation] arduinouno+ digital tube cycle display 0~9
- Research on depth image compression in YUV420 color space
- Progress of the 137th MPEG Conference
- What are the debugging methods for nodejs
- HNU计网实验:实验五 网络层与链路层协议分析(PacketTracer)
- Is it safe to open an account with Caicai securities?
- Yyds dry goods inventory CEPH installation visual dashboard
- Webrtc crash course
- SSH modifies grub in heiqunhui ds918+ system 7.0.1 cfg
猜你喜欢

Summary of digital image processing knowledge points

Obsidian basic tutorial

24 pictures to clarify TCP at one time

How to use Matplotlib library to realize enlarged display of graphic local data

用idea建立第一个网站

How can the computer tablet be connected to the computer

【hnu暑学期】数据库系统设计 准备阶段

How does idea package its own projects into jar packages
be careful! This written examination method is gradually being replaced

了解有哪几个C标准&了解C编译管道
随机推荐
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF
Dio encapsulé pour les requêtes réseau flutter (gestion des cookies, ajout d'intercepteurs, téléchargement de fichiers, gestion des exceptions, annulation des requêtes, etc.)
Pycharm 2022.1 EAP 2 release
Measurement fitting based on Halcon learning -- Practice [2]
【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧
Fujilai pharmaceutical has passed the registration: the annual revenue is nearly 500million yuan. Xiangyun once illegally traded foreign exchange
HNU计网实验:实验五 网络层与链路层协议分析(PacketTracer)
Top in the whole network, it is no exaggeration to say that this Stanford machine learning tutorial in Chinese notes can help you learn from the beginning to the mastery of machine learning
【Proteus仿真】Arduino UNO+按键控制2位数码管倒计时
No nonsense, code practice will help you master strong caching and negotiation caching!
Application runtime layotto enters CNCF cloud native panorama
Preliminary solution of i/o in socket programming
了解有哪几个C标准&了解C编译管道
Summary of basic knowledge of neural network
HNU network counting experiment: Experiment 4 application layer and transport layer protocol analysis (packettracer)
CVPR2022教程 | 马里兰大学《机器学习遥感处理:农业与粮食安全》教程
Mastering quantization technology is the key to video compression
Analysis of gpl3.0 license software copyright dispute cases
. Thoughts on software trends in the 20th anniversary of net
What if win11 cannot delete the folder? Win11 cannot delete folder