Skip to content

database/gdb: ClearCache 没有清除缓存 #4581

@sanrentai

Description

@sanrentai

Go version

go version go1.25.5 windows/amd64

GoFrame version

v2.9.6

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

package main

import (
	"time"

	_ "github.com/gogf/gf/contrib/drivers/mssql/v2"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
)

func main() {
	ctx := gctx.New()
	g.Log().Info(ctx, "程序开始执行...")

	// 先尝试删除表(如果存在)
	g.Log().Info(ctx, "清理可能存在的旧表...")
	_, err := g.DB().Exec(ctx, "IF OBJECT_ID('test_table', 'U') IS NOT NULL DROP TABLE test_table")
	if err != nil {
		g.Log().Error(ctx, "删除表失败:", err)
	}

	// 创建表
	g.Log().Info(ctx, "创建表test_table...")
	createTableSQL := `
	CREATE TABLE [test_table]( 
		[id] [bigint] NOT null primary key, 
		name varchar(10) default '' ,
		content varchar(10) default '' 
	);
	`
	_, err = g.DB().Exec(ctx, createTableSQL)
	if err != nil {
		g.Log().Error(ctx, "创建表失败:", err)
		return
	}
	g.Log().Info(ctx, "创建表成功")

	// 写入数据
	g.DB().Model("test_table").Ctx(ctx).Insert(g.List{
		g.Map{
			"id":      1,
			"name":    "hello",
			"content": "你好",
		},
		g.Map{
			"id":      2,
			"name":    "world",
			"content": "世界",
		},
	})

	// 初始查询表数据
	g.Log().Info(ctx, "初始查询表数据...")

	result, err := g.DB().Model("test_table").Ctx(ctx).Cache(gdb.CacheOption{
		Duration: time.Hour,
		Name:     "hello",
		Force:    false,
	}).Where("name=?", "hello").All()
	if err != nil {
		g.Log().Error(ctx, "查询表数据失败:", err)
	} else {
		g.Log().Info(ctx, "表中的数据:", result)
	}

	// 修改数据
	g.Log().Info(ctx, "更新数据...")
	_, err = g.DB().Model("test_table").Ctx(ctx).
		// Cache(gdb.CacheOption{
		// 	Duration: -1,
		// 	Name:     "hello",
		// 	Force:    false,
		// }).
		Where("name=?", "hello").
		Update(g.Map{
			"content": "你好中国",
		})
	if err != nil {
		g.Log().Error(ctx, "更新数据失败:", err)
	} else {
		g.Log().Info(ctx, "更新数据成功")
	}

	err = g.DB().GetCore().ClearCache(ctx, "test_table")
	if err != nil {
		g.Log().Error(ctx, "清除缓存失败:", err)
	} else {
		g.Log().Info(ctx, "清除缓存成功")
	}

	// 再次查询
	result, err = g.DB().Model("test_table").Ctx(ctx).Cache(gdb.CacheOption{
		Duration: time.Hour,
		Name:     "hello",
		Force:    false,
	}).Where("name=?", "hello").All()
	if err != nil {
		g.Log().Error(ctx, "查询表数据失败:", err)
	} else {
		g.Log().Info(ctx, "表中的数据:", result)
	}

	// 清理表
	g.Log().Info(ctx, "清理表...")
	_, err = g.DB().Exec(ctx, "DROP TABLE test_table")
	if err != nil {
		g.Log().Error(ctx, "删除表失败:", err)
	} else {
		g.Log().Info(ctx, "删除表成功")
	}

	g.Log().Info(ctx, "程序执行完成")
}

What did you see happen?

2025-12-29T15:53:06.030+08:00 [INFO] {80b7be4c65a085181a57647d1fb9ff05} 初始查询表数据...
2025-12-29T15:53:06.069+08:00 [DEBU] {80b7be4c65a085181a57647d1fb9ff05} [ 36 ms] [default] [hms_cs] [rows:1  ] SELECT * FROM test_table WHERE name='hello'
2025-12-29T15:53:06.069+08:00 [INFO] {80b7be4c65a085181a57647d1fb9ff05} 表中的数据: [{"content":"你好","id":1,"name":"hello"}]
2025-12-29T15:53:06.071+08:00 [INFO] {80b7be4c65a085181a57647d1fb9ff05} 更新数据...
2025-12-29T15:53:06.104+08:00 [DEBU] {80b7be4c65a085181a57647d1fb9ff05} [ 33 ms] [default] [hms_cs] [rows:1  ] UPDATE test_table SET content='你好中国' WHERE name='hello'
2025-12-29T15:53:06.104+08:00 [INFO] {80b7be4c65a085181a57647d1fb9ff05} 更新数据成功
2025-12-29T15:53:06.105+08:00 [INFO] {80b7be4c65a085181a57647d1fb9ff05} 清除缓存成功
2025-12-29T15:53:06.106+08:00 [INFO] {80b7be4c65a085181a57647d1fb9ff05} 表中的数据: [{"content":"你好","id":1,"name":"hello"}]
2025-12-29T15:53:06.106+08:00 [INFO] {80b7be4c65a085181a57647d1fb9ff05} 清理表...
2025-12-29T15:53:06.174+08:00 [DEBU] {80b7be4c65a085181a57647d1fb9ff05} [ 67 ms] [default] [hms_cs] [rows:0  ] DROP TABLE test_table
2025-12-29T15:53:06.174+08:00 [INFO] {80b7be4c65a085181a57647d1fb9ff05} 删除表成功
2025-12-29T15:53:06.174+08:00 [INFO] {80b7be4c65a085181a57647d1fb9ff05} 程序执行完成

What did you expect to see?

第二次能够查询数据库中的数据

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIt is confirmed a bug, but don't worry, we'll handle it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions