当前位置:网站首页>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目录
边栏推荐
- MYSQL-GROUP BY 用法 全网最精,通熟易懂的话解释
- Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)
- 分布式系统中的开创者—莱斯利·兰伯特
- DHCP principle and configuration
- Ali two sides: List several tips for Api interface optimization
- The CTO said I was not advised to use SELECT *, why is that?
- 你被MySQL 中的反斜杠 \\坑过吗?
- STL源码剖析:class template explicit specialization代码测试和理解
- 如何理解普吕克坐标(几何理解)
- 空间平面相交的直线的计算及其源码
猜你喜欢

Detailed explanation of numpy multidimensional array ndarray

LVM和磁盘配额

Distance calculation from space vertex to straight line and its source code

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

空间平面相交的直线的计算及其源码

万能js时间日期格式转换

Is it possible to use the same port for UDP and TCP?

(GGG)JWT

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

Redis下载与安装
随机推荐
How to use Swagger, say goodbye to postman
Test Development Engineer Growth Diary 001 - Some Introduction to Agile Testing, CI/CD/CT, DecOps
AI元学习引入神经科学,医疗效果有望精准提升
STL源码剖析:临时对象的代码测试和理解
How does Redis prevent oversold and inventory deduction operations?
限塑令下的新材料——聚乳酸(PLA)
Playing script killing with AI: actually more involved than me
RAID disk array
手机端滚动至页面指定位置
和AI一起玩儿剧本杀:居然比我还入戏
Redis download and installation
这个终端连接工具,碾压Xshell
MYSQL-GROUP BY 用法 全网最精,通熟易懂的话解释
向量叉乘的几何意义及其模的计算
sql concat()函数
让百度地图生成器里的“标注”内容展开--解决方案
Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)
云服务器零基础部署网站(保姆级教程)
你被MySQL 中的反斜杠 \\坑过吗?
向量三重积的等式推导证明