当前位置:网站首页>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 :

边栏推荐
- Research and Analysis on the current situation of Chinese acne drug market and forecast report on its development prospect (2022)
- 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
- 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
- Zhihu Gaozan: what ability is important, but most people don't have it?
- Obsidian basic tutorial
- 【Proteus仿真】Arduino UNO+按键控制2位数码管倒计时
- Market demand analysis and investment prospect research report of China's CNC machine tool industry 2022-2028
- What is a ZFS file system
- InfiniBand& RDMA
- Pat 1083 list grades (25 points)
猜你喜欢
了解有哪几个C标准&了解C编译管道
. Thoughts on software trends in the 20th anniversary of net
Nacos 源码分析01 代码结构
Wd4t secondary opening firmware 190 module for mobile hard disk data recovery is successfully repaired
Presto中Limit算子的处理过程
Zero Trust: break the passive development mode of "attack and defense" and build a "moat" for enterprise safety
Where is win11 screen recording data saved? Win11 screen recording data storage location
Youku IPv6 evolution and Practice Guide
3.4 cloning and host time synchronization of VMware virtual machine
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
随机推荐
HNU计网实验:实验一 应用协议与数据包分析实验(使用Wireshark)
ITU AI and multimedia Seminar: exploring new areas and cross SDO synergy
Oracle case: does index range scan really not read multiple blocks?
在线CRUDhasone关联查询报错Unabletouseinternalvariable:List
What if win11 cannot delete the folder? Win11 cannot delete folder
China soft magnetic material market demand status and prospect scale forecast report 2022-2028
Research and Analysis on the current situation of Chinese acne drug market and forecast report on its development prospect (2022)
[proteus simulation] Arduino uno pattern water lamp
Conglin environmental protection IPO meeting: annual profit of more than 200million to raise 2.03 billion
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.)
idea怎么把自己的项目打包成jar包
HNU数据库系统概论 ODBC
Did you really learn the right game modeling with 3DMAX?
Bear market guide | some essential lessons and specific survival rules
华为云短信测了很多手机都提示发送频繁
SwiftUI 4 新功能 之 网格视图Gridview组件 (教程含源码)
Preliminary solution of i/o in socket programming
2022giao考游记
Leetcode topic [array] -18- sum of four numbers
What is the difficulty of the form tool that costs billions of dollars? Exclusive interview with si no