当前位置:网站首页>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目录
边栏推荐
猜你喜欢

Process and Scheduled Task Management

Local Implicit Grid Representations for 3D Scenes详解

redis实现分布式锁的原理

Multithreading basics (multithreaded memory, security, communication, thread pools and blocking queues)

VR机器人教你如何正确打乒乓球

云服务器零基础部署网站(保姆级教程)

Linx common directory & file management commands & VI editor usage introduction

从 Google 离职,前Go 语言负责人跳槽小公司

Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)

限塑令下的新材料——聚乳酸(PLA)
随机推荐
Test Development Engineer Growth Diary 001 - Some Introduction to Agile Testing, CI/CD/CT, DecOps
export , export default, import complete usage
DNS domain name resolution services
如何理解普吕克坐标(几何理解)
MySQL master-slave replication configuration construction, one step in place
【MySQL】MySQL中如何实现分页操作
Equation Derivation Proof of Vector Triple Product
Test Development Engineer Growth Diary 010 - CI/CD/CT in Jenkins (Continuous Integration Build/Continuous Delivery/Continuous Testing)
Test development engineer growth diary 016 - those things about the test
大飞机C919都用了哪些新材料?
阿里二面:Sentinel vs Hystrix 对比,如何选择?
Local Implicit Grid Representations for 3D Scenes详解
Electron之初出茅庐——搭建环境并运行第一个程序
Linx常见目录&文件管理命令&VI编辑器使用 介绍
prometheus-Federation机制配置
Required request body is missing 问题解决
2020年度总结——品曾经,明得失,展未来
New breakthrough in artificial muscle smart materials
AI元学习引入神经科学,医疗效果有望精准提升
From catching up to surpassing, domestic software shows its talents