当前位置:网站首页>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目录
边栏推荐
- Derivative Operations on Vectors and Derivative Operations on Vector Cross and Dot Products
- export , export default,import完整用法
- Ali two sides: Sentinel vs Hystrix comparison, how to choose?
- Local Implicit Grid Representations for 3D Scenes详解
- Electron之初出茅庐——搭建环境并运行第一个程序
- Advanced multi-threading (lock strategy, spin+CAS, Synchronized, JUC, semaphore)
- Graphical relational database design ideas, this is too vivid
- Redis 如何实现防止超卖和库存扣减操作?
- The usage of window.open(), js opens a new form
- “AI教练”请进家,家庭智能健身蓬勃发展
猜你喜欢

AI元学习引入神经科学,医疗效果有望精准提升

让百度地图生成器里的“标注”内容展开--解决方案

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

图解关系数据库设计思想,这也太形象了

大飞机C919都用了哪些新材料?

Advanced multi-threading (CountDownLatch, deadlock, thread-safe collection class)

Playing script killing with AI: actually more involved than me

向量三重积的等式推导证明

CTO说不建议我使用SELECT * ,这是为什么?

Rodrigues: vector representation of rotation matrices
随机推荐
搭建vsftpd服务并实现本地用户访问
When does MySQL use table locks and when does it use row locks?
万能js时间日期格式转换
Local Implicit Grid Representations for 3D Scenes详解
The usage of window.open(), js opens a new form
Data types of Redis6
PXE efficient mass network capacity
Electron日常学习笔记
Test Development Engineer Growth Diary 010 - CI/CD/CT in Jenkins (Continuous Integration Build/Continuous Delivery/Continuous Testing)
使用 Grafana 的 Redis Data Source 插件监控 Redis
Electron使用romote报错 : Uncaught TypeError: Cannot read property ‘BrowserWindow‘ of undefined
The calculation proof of the intersection of the space line and the plane and its source code
Graphical relational database design ideas, this is too vivid
Ali two sides: List several tips for Api interface optimization
Rodrigues:旋转矩阵的向量表达
“AI教练”请进家,家庭智能健身蓬勃发展
Test Development Engineer Growth Diary 001 - Some Introduction to Agile Testing, CI/CD/CT, DecOps
(GGG)JWT
The terminal connection tools, rolling Xshell
阿里二面:列出 Api 接口优化的几个技巧