All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references核心组件数据库ORMORM链式操作ORM链式操作-查询缓存.md

查询缓存

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

相关方法:

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()。

缓存适配( Redis 缓存)

默认情况下 ORM 的 *gcache.Cache 缓存对象提供的是单进程内存缓存,虽然性能非常高效,但是只能在单进程内使用。如果服务如果采用多节点部署,多节点之间的缓存可能会产生数据不一致的情况,因此大多数场景下我们都是通过 Redis 服务器来实现对数据库查询数据的缓存。 *gcache.Cache 对象采用了适配器设计模式,可以轻松实现从单进程内存缓存切换为分布式的 Redis 缓存。使用示例:

redisCache := gcache.NewAdapterRedis(g.Redis())
g.DB().GetCache().SetAdapter(redisCache)

更多介绍请参考: 缓存管理-Redis缓存

管理方法

为简化数据库的查询缓存管理,从 v2.2.0 版本开始,提供了两个缓存管理方法:

// ClearCache removes cached sql result of certain table.
func (c *Core) ClearCache(ctx context.Context, table string) (err error)

// ClearCacheAll removes all cached sql result from cache
func (c *Core) ClearCacheAll(ctx context.Context) (err error)

方法介绍如注释。可以看到这两个方法是挂载 Core 对象上的,而底层的 Core 对象已经通过 DB 接口暴露,因此我们这么来获取 Core 对象:

g.DB().GetCore()

使用示例

数据表结构

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 := g.Model("user").Ctx(ctx).Data(g.Map{
        "name": "john",
        "site": "https://goframe.org",
    }).Insert()

    // 执行2次查询并将查询结果缓存1小时,并可执行缓存名称(可选)
    for i := 0; i < 2; i++ {
        r, _ := g.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 = g.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, _ := g.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}

可以看到:

  1. 为了方便展示缓存效果,这里开启了数据 debug 特性,当有任何的SQL操作时将会输出到终端。
  2. 执行两次 One 方法数据查询,第一次走了SQL查询,第二次直接使用到了缓存,SQL没有提交到数据库执行,因此这里只打印了一条查询SQL,并且两次查询的结果也是一致的。
  3. 注意这里为该查询的缓存设置了一个自定义的名称 vip-user,以便于后续清空更新缓存。如果缓存不需要清理,那么可以不用设置缓存名称。
  4. 当执行 Update 更新操作时,同时根据名称清空指定的缓存。
  5. 随后再执行 One 方法数据查询,这时重新缓存新的数据。

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.