当前位置:网站首页>Go 使用 freecache 缓存
Go 使用 freecache 缓存
2022-07-30 05:51:00 【行人已】
1、freecache 介绍(以下简称free)
用于 Go 的缓存库,具有零 GC 开销和高并发性能, 厉不厉害?
2、 free 获取
git官网: https://github.com/coocood/freecache
golang 获取:go get -u github.com/coocood/freecache
个人觉得比较好的文档:(不得不说大佬就是写得好)
https://juejin.cn/post/7072121084136882183
https://cdmana.com/2022/03/202203071328243883.html
3、golang 整合 free
go-cashe
├─ .vscode
│ └─ launch.json
├─ freecache
│ └─ free.go
├─ main.go
├─ README-free.md
3.1、主要代码 (free.go)
import (
"github.com/coocood/freecache"
)
//初始化Cache
// freecache.NewCacheCustomTimer(100*1024*1024, freecache.NewCachedTimer()) 这种方式也可以
var cache = freecache.NewCache(100 * 1024 * 1024)
//定一个 model struct
type FreeCasheModel struct {
Key []byte
Value []byte
ExpireSeconds int //过期时间-s
}
//Get
func FreeGet(free FreeCasheModel) (string, bool) {
value, err := cache.Get(free.Key)
if err != nil {
return "", false
}
return string(value), true
}
//GetOrSet
//如果没有就存入新的key
func FreeCacheGetOrSet(free FreeCasheModel) (string, bool) {
retValue, err := cache.GetOrSet(free.Key, free.Value, free.ExpireSeconds)
if err != nil {
return "", false
}
return string(retValue), true
}
//Set
func FreeCacheSet(free FreeCasheModel) bool {
err := cache.Set(free.Key, free.Value, free.ExpireSeconds)
if err != nil {
return false
}
return true
}
//SetAndGet
func FreeCacheSetAndGet(free FreeCasheModel) (string, bool) {
retValue, found, err := cache.SetAndGet(free.Key, free.Value, free.ExpireSeconds)
if err != nil {
return "", false
}
return string(retValue), found
}
//更新key的过期时间--如果这个key不存在会返回错误
func FreeChasheTouch(free FreeCasheModel) bool {
err := cache.Touch(free.Key, free.ExpireSeconds)
if err != nil {
return false
}
return true
}
//删除---
func FreeCasheDel(free FreeCasheModel) bool {
return cache.Del(free.Key)
}
4、简单测试

5、补充说明
1、代码看起来很简单,但是要多看源码多看文档是怎么实现的
2、 如果要测试性能问题老师们可以自己去测试,这里只是简单的实现一下
3、 上篇文档我写了memcached的缓存,我觉得这个更简单一些,但是应用场景是不一样的, memcached主要是分布式循环,多个项目可以同时去访问,free更适合单项目,并发高访问量多的。
6、文档代码地址
https://gitee.com/hjx_RuGuoYunZhiDao/strom-huang-go.git —go-cashe目录
边栏推荐
- Ali two sides: Sentinel vs Hystrix comparison, how to choose?
- Station B collapsed, what would you do if you were the developer in charge that night?
- 向量三重积的等式推导证明
- 2020年度总结——品曾经,明得失,展未来
- Playing script killing with AI: actually more involved than me
- 深度学习:线性回归模型
- MYSQL-GROUP BY 用法 全网最精,通熟易懂的话解释
- [硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!
- 舒尔补(schur completement)
- RAID磁盘阵列
猜你喜欢

The introduction of AI meta-learning into neuroscience, the medical effect is expected to improve accurately

MySQL什么时候用表锁,什么时候用行锁?

阿里二面:Sentinel vs Hystrix 对比,如何选择?

MongoDB-CUD without R

2020 数学建模之旅

Huawei released "ten inventions", including computing, intelligent driving and other new fields

Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)

你被MySQL 中的反斜杠 \\坑过吗?

进程和计划任务管理

Rodrigues: vector representation of rotation matrices
随机推荐
PXE高效批量网络装机
DNS域名解析服务
export , export default,import完整用法
DHCP原理与配置
Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)
CTO说不建议我使用SELECT * ,这是为什么?
Mobile phone side scroll to page to specify location
(GGG)JWT
进程和计划任务管理
roslyn folder under bin folder
Table with tens of millions of data, how to query the fastest?
iptables命令
上传文件--文件类型大全,图片类型,文档类型,视频类型,压缩包类型
The Society of Mind - Marvin Minsky
Ali: How many methods are there for multi-threaded sequential operation?
Data types of Redis6
人工肌肉智能材料新突破
向量三重积的等式推导证明
Linx common directory & file management commands & VI editor usage introduction
舒尔补(schur completement)