gf
gf copied to clipboard
cache feature takes effect in transaction
1. What version of Go
and system type/arch are you using?
go 1.18.4
2. What version of GoFrame
are you using?
gf v2.1.2
3. Can this issue be re-produced with the latest release?
yes
4. What did you do?
gdb事务中的查询cache生效了
建表
CREATE TABLE test4 (
id int8 NOT NULL
);
INSERT INTO test4 (id) values (1);
示例代码
package main
import (
"errors"
"time"
_ "github.com/gogf/gf/contrib/drivers/pgsql/v2"
_ "github.com/lib/pq"
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/gctx"
"demo/internal/dao"
)
func main() {
adapter := gcache.NewAdapterRedis(g.Redis())
g.DB().GetCache().SetAdapter(adapter)
ctx := gctx.New()
for i := 0; i < 3; i++ {
_ = dao.Test.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
_, _ = dao.Test.Ctx(ctx).
Handler(func(m *gdb.Model) *gdb.Model {
return m.Cache(gdb.CacheOption{Duration: time.Hour, Name: "test:1"})
}).
One("id", 1)
return errors.New("rollback")
})
}
}
输出
第一次查询数据库并且更新了cache,第二次开始使用了cache
2022-08-04 16:06:20.031 [DEBU] {f819a1acc01508176f2767602609a310} [ 71 ms] [default] [rows:0 ] [txid:1] BEGIN
2022-08-04 16:06:20.079 [DEBU] {f819a1acc01508176f2767602609a310} [ 48 ms] [default] [rows:4 ] [txid:1] SELECT a.attname AS field, t.typname AS type,a.attnotnull as null, (case when d.contype is not null then 'pri' else '' end) as key ,ic.column_default as default_value,b.description as comment ,coalesce(character_maximum_length, numeric_precision, -1) as length ,numeric_scale as scale FROM pg_attribute a left join pg_class c on a.attrelid = c.oid left join pg_constraint d on d.conrelid = c.oid and a.attnum = d.conkey[1] left join pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid left join pg_type t ON a.atttypid = t.oid left join information_schema.columns ic on ic.column_name = a.attname and ic.table_name = c.relname WHERE c.relname = 'test' and a.attisdropped is false and a.attnum > 0 ORDER BY a.attnum
2022-08-04 16:06:20.085 [DEBU] {f819a1acc01508176f2767602609a310} [ 4 ms] [default] [rows:1 ] [txid:1] SELECT * FROM "test" WHERE "id"=1 LIMIT 1
2022-08-04 16:06:20.088 [DEBU] {f819a1acc01508176f2767602609a310} [ 2 ms] [default] [rows:0 ] [txid:1] ROLLBACK
2022-08-04 16:06:20.108 [DEBU] {f819a1acc01508176f2767602609a310} [ 20 ms] [default] [rows:0 ] [txid:2] BEGIN
2022-08-04 16:06:20.111 [DEBU] {f819a1acc01508176f2767602609a310} [ 2 ms] [default] [rows:0 ] [txid:2] ROLLBACK
2022-08-04 16:06:20.131 [DEBU] {f819a1acc01508176f2767602609a310} [ 20 ms] [default] [rows:0 ] [txid:3] BEGIN
2022-08-04 16:06:20.134 [DEBU] {f819a1acc01508176f2767602609a310} [ 2 ms] [default] [rows:0 ] [txid:3] ROLLBACK
5. What did you expect to see?
事务中的select场景,cache不应该生效,否则事务回滚时cache中可能产生脏数据
@gqcn
@gqcn
@qinyuguang 请问该问题在新版本中是否还存在?
@gqcn 最新版本还存在这个问题