gdb支持对查询结果的缓存处理,常用于多读少写的查询缓存场景,并支持手动的缓存清理。需要注意的是,查询缓存仅支持链式操作,且在事务操作下不可用。

创新互联建站提供成都做网站、网站设计、网页设计,品牌网站制作,1元广告等致力于企业网站建设与公司网站制作,10年的网站开发和建站经验,助力企业信息化建设,成功案例突破上1000家,是您实现网站建设的好选择.
相关方法:
type CacheOption struct {
	// Duration is the TTL for the cache.
	// If the parameter `Duration` < 0, which means it clear the cache with given `Name`.
	// If the parameter `Duration` = 0, which means it never expires.
	// If the parameter `Duration` > 0, which means it expires after `Duration`.
	Duration time.Duration
	// Name is an optional unique name for the cache.
	// The Name is used to bind a name to the cache, which means you can later control the cache
	// like changing the `duration` or clearing the cache with specified Name.
	Name string
	// Force caches the query result whatever the result is nil or not.
	// It is used to avoid Cache Penetration.
	Force bool
}
// Cache sets the cache feature for the model. It caches the result of the sql, which means
// if there's another same sql request, it just reads and returns the result from cache, it
// but not committed and executed into the database.
//
// Note that, the cache feature is disabled if the model is performing select statement
// on a transaction.
func (m *Model) Cache(option CacheOption) *Model
ORM对象默认情况下提供了缓存管理对象,该缓存对象类型为*gcache.Cache,也就是说同时也支持*gcache.Cache的所有特性。可以通过GetCache() *gcache.Cache 接口方法获得该缓存对象,并通过返回的对象实现自定义的各种缓存操作,例如:g.DB().GetCache().Keys()。 
默认情况下ORM的*gcache.Cache缓存对象提供的是单进程内存缓存,虽然性能非常高效,但是只能在单进程内使用。如果服务如果采用多节点部署,多节点之间的缓存可能会产生数据不一致的情况,因此大多数场景下我们都是通过Redis服务器来实现对数据库查询数据的缓存。*gcache.Cache对象采用了适配器设计模式,可以轻松实现从单进程内存缓存切换为分布式的Redis缓存。使用示例:
redisCache := gcache.NewAdapterRedis(g.Redis())
g.DB().GetCache().SetAdapter(redisCache)
CREATE TABLE `user` (
  `uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL DEFAULT '' COMMENT '昵称',
  `site` varchar(255) NOT NULL DEFAULT '' COMMENT '主页',
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
package main
import (
	"time"
	"github.com/GOgf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
)
func main() {
	var (
		db  = g.DB()
		ctx = gctx.New()
	)
	// 开启调试模式,以便于记录所有执行的SQL
	db.SetDebug(true)
	// 写入测试数据
	_, err := db.Model("user").Ctx(ctx).Data(g.Map{
		"name": "john",
		"site": "https://GoFrame.org",
	}).Insert()
	// 执行2次查询并将查询结果缓存1小时,并可执行缓存名称(可选)
	for i := 0; i < 2; i++ {
		r, _ := db.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
			Duration: time.Hour,
			Name:     "vip-user",
			Force:    false,
		}).Where("uid", 1).One()
		g.Log().Debug(ctx, r.Map())
	}
	// 执行更新操作,并清理指定名称的查询缓存
	_, err = db.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
		Duration: -1,
		Name:     "vip-user",
		Force:    false,
	}).Data(gdb.Map{"name": "smith"}).Where("uid", 1).Update()
	if err != nil {
		g.Log().Fatal(ctx, err)
	}
	// 再次执行查询,启用查询缓存特性
	r, _ := db.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
		Duration: time.Hour,
		Name:     "vip-user",
		Force:    false,
	}).Where("uid", 1).One()
	g.Log().Debug(ctx, r.Map())
}执行后输出结果为(测试表数据结构仅供示例参考):
2022-02-08 17:36:19.817 [DEBU] {c0424c75f1c5d116d0df0f7197379412} {"name":"john","site":"https://goframe.org","uid":1} 
2022-02-08 17:36:19.817 [DEBU] {c0424c75f1c5d116d0df0f7197379412} {"name":"john","site":"https://goframe.org","uid":1} 
2022-02-08 17:36:19.817 [DEBU] {c0424c75f1c5d116d0df0f7197379412} [  0 ms] [default] [rows:1  ] UPDATE `user` SET `name`='smith' WHERE `uid`=1 
2022-02-08 17:36:19.818 [DEBU] {c0424c75f1c5d116d0df0f7197379412} [  1 ms] [default] [rows:1  ] SELECT * FROM `user` WHERE `uid`=1 LIMIT 1 
2022-02-08 17:36:19.818 [DEBU] {c0424c75f1c5d116d0df0f7197379412} {"name":"smith","site":"https://goframe.org","uid":1}可以看到:
为了方便展示缓存效果,这里开启了数据debug特性,当有任何的SQL操作时将会输出到终端。
执行两次One方法数据查询,第一次走了SQL查询,第二次直接使用到了缓存,SQL没有提交到数据库执行,因此这里只打印了一条查询SQL,并且两次查询的结果也是一致的。
注意这里为该查询的缓存设置了一个自定义的名称vip-user,以便于后续清空更新缓存。如果缓存不需要清理,那么可以不用设置缓存名称。
当执行Update更新操作时,同时根据名称清空指定的缓存。
随后再执行One方法数据查询,这时重新缓存新的数据。
                文章名称:创新互联GoFrame教程:GoFrame链式操作-查询缓存
                
                本文路径:http://www.csdahua.cn/qtweb/news3/178903.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网