当前位置:网站首页>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: How many methods are there for multi-threaded sequential operation?
- Equation Derivation Proof of Vector Triple Product
- Test development engineer diary 002 - starting from 0 interface automation
- Pioneer in Distributed Systems - Leslie Lambert
- 首届人工智能安全大赛正式启动
- Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)
- What new materials are used in the large aircraft C919?
- Proof of distance calculation from space vertex to plane and its source code
- The calculation of the determinant of the matrix and its source code
- Selenium01
猜你喜欢
MongoDB-CUD without R
How to understand plucker coordinates (geometric understanding)
The terminal connection tools, rolling Xshell
MYSQL-GROUP BY 用法 全网最精,通熟易懂的话解释
STL source code analysis: conceptual understanding of iterators, and code testing.
The Geometric Meaning of Vector Cross Product and the Calculation of Modulus
Electron之初出茅庐——搭建环境并运行第一个程序
使用 Grafana 的 Redis Data Source 插件监控 Redis
Huawei released "ten inventions", including computing, intelligent driving and other new fields
Rodrigues: vector representation of rotation matrices
随机推荐
The calculation proof of the intersection of the space line and the plane and its source code
Electron日常学习笔记
人工肌肉智能材料新突破
UDP和TCP使用同一个端口,可行吗?
The Society of Mind - Marvin Minsky
DNS domain name resolution services
首届人工智能安全大赛正式启动
How to use Swagger, say goodbye to postman
阿里一面:多线程顺序运行有多少种方法?
Process and Scheduled Task Management
Swagger使用方式,告别postman
2020 数学建模之旅
VR机器人教你如何正确打乒乓球
MySQL什么时候用表锁,什么时候用行锁?
《心智社会》—马文·明斯基
idea built-in translation plugin
prometheus-Federation机制配置
你被MySQL 中的反斜杠 \\坑过吗?
新人误删数据,组长巧用MySQL主从复制延迟挽回损失
matlab机器学习_01