在 Golang 中实现 Cache::remember 方法详解
作者:施国鹏的博客 时间:2024-04-26 17:21:08
项目需要把部分代码移植到 Golang , 之前用 Laravel 封装的写起来很舒服,在 Golang 里只能自动动手实现.
一开始想的是使用 interface 实现,但是遇到了一个坑, Golang 里的组合是一个虚假的继承
package main
import "fmt"
type Person interface {
Say()
Name()
}
type Parent struct {
}
func (s *Parent) Say() {
fmt.Println("i am " + s.Name())
}
func (s *Parent) Name() string {
return "parent"
}
type Child struct {
Parent
}
func (s *Child) Name() string {
return "child"
}
type Child1 struct {
Parent
}
func main() {
var c Child
// i am parent
c.Say()
var c1 Child1
// i am parent
c1.Say()
}
如上 c.say() 代码,在别的语言里应该是输出 i am child 才对, 而 Golang 不一样,查了一下 Golang 的资料才能理解 https://golang.org/ref/spec#Selectors
大致意思是说,通过 x.f 调用 f 方法或者属性时,从当前或者嵌套匿名结构体由浅到深的去调用,而不会去寻找上级
比如 child1 没有 Say 方法,会进入到匿名结构体 Parent 找到 Say 方法,然后调用
而 child 也没有 Say 方法,同样去调用 Parent 的 Say 方法,这时候 Say 是通过 Parent 调用的, 当在 Say 里调用 s.Name 方法,并不能找到 child , 所以还是会调用到 Parent 的 Name 方法
然后自己整理和同事一起写了大致的 remember 方法
import (
"context"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"time"
)
// redis 操作已经简化
func CacheGet(c context.Context, t interface{}, cacheKey string, callQuery func() error) error {
// 此处通过 redis 获取数据, 如果存在数据, 那么直接返回
dataBytes, err := redis.Get(c, cacheKey).Bytes()
if err == nil {
if err := json.Unmarshal(dataBytes, t); err == nil {
return nil
}
}
// 当 redis 没有数据, 那么调用此方法修改 t,
if err := callQuery(); err != nil {
return err
}
// 这里把修改之后的 t 存储到 redis, 下次使用便可以使用缓存
dataBytes, err = json.Marshal(t)
if err == nil {
redis.Set(c, cacheKey, dataBytes, time.Minute*30)
}
return nil
}
func handle(c *gin.Context) {
var model models.User
err := utils.CacheGet(
c.Request.Context(),
&model,
fmt.Sprintf("cache_xxx:%s", c.Param("id")),
func() error {
return db.First(&model)
},
)
}
来源:https://www.shiguopeng.cn/archives/499
标签:Golang,Cache::remember
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Python的身份证号码自动生成程序
2022-11-29 02:00:33
![](https://img.aspxhome.com/file/2023/3/96993_0s.jpg)
详解django使用include无法跳转的解决方法
2023-04-08 06:13:54
Python3.5面向对象编程图文与实例详解
2023-12-04 23:21:51
![](https://img.aspxhome.com/file/2023/8/88008_0s.png)
pycharm远程开发项目的实现步骤
2023-07-17 06:01:54
![](https://img.aspxhome.com/file/2023/5/102925_0s.png)
SQL Server数据库备份出错及应对措施
2009-04-20 17:02:00
K-means聚类算法介绍与利用python实现的代码示例
2023-07-29 11:08:55
![](https://img.aspxhome.com/file/2023/6/120606_0s.png)
pandas数据集的端到端处理
2023-01-20 04:48:43
如何让对方在线查看我的程序代码?
2010-05-13 16:34:00
使用python创建生成动态链接库dll的方法
2021-02-13 22:18:21
基于Python实现商场抽奖小系统
2021-07-01 14:46:14
![](https://img.aspxhome.com/file/2023/0/131480_0s.jpg)
使用fdopen实现对Python进程产生的文件进行权限最小化配置
2021-04-20 15:49:05
从错误中学习改正Go语言五个坏习惯提高编程技巧
2023-10-12 20:06:33
使用Python获取字典键对应值的两种方法
2022-08-04 05:25:16
![](https://img.aspxhome.com/file/2023/7/117267_0s.png)
Pyqt助手安装PyQt5帮助文档过程图解
2021-01-31 00:34:06
![](https://img.aspxhome.com/file/2023/5/101015_0s.png)
百万级asp分页存储过程代码(ver2.0)
2007-12-17 13:13:00
golang 字符串比较是否相等的方法示例
2024-02-05 14:45:07
浏览器是怎样工作的(一):基础知识
2012-05-09 20:32:48
python的re模块使用方法详解
2022-08-10 16:18:20
Ubuntu下使用python读取doc和docx文档的内容方法
2021-04-09 03:42:30
numpy.std() 计算矩阵标准差的方法
2023-06-04 19:50:34