缓存和事务一起使用的问题 #2140

Open
opened 2022-04-29 00:57:18 +00:00 by liangpengcheng · 1 comment

在开启缓存的时候,在session中删除数据,commit后发现之前缓存的数据依然可以查询到,数据表中已经删除,下面是我的代码


type testtable struct {
	ID   int64  `xorm:"pk autoincr"`
	Name string `xorm:"varchar(20)"`
}
func test_orm2(orm *xorm.Engine) {

	cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour*24, 1000000)
	orm.SetDefaultCacher(cacher)

	orm.Sync2(new(testtable))
	ins := &testtable{Name: "test"}
	orm.Insert(ins)
	getresult := &testtable{}
	orm.ID(ins.ID).Get(getresult)
	fmt.Printf("get %v\n", getresult)
	ins.Name = "update"
	orm.Update(ins)

	session := orm.NewSession()
	session.Begin()
	ins.Name = "update session"
	session.Update(ins)
	orm.ID(ins.ID).Get(getresult)
	fmt.Printf("insession %v\n", getresult)
	var retarray []*testtable
	orm.Find(&retarray)
	if len(retarray) > 0 {
		fmt.Printf("before delete %v\n", retarray[0])
	}
	session.ID(ins.ID).Delete(new(testtable))
	session.Commit()
	session.Close()
	var afterDelete []*testtable
	err := orm.Find(&afterDelete)
	if err != nil {
		fmt.Printf("err:%v\n", err)
	}
	if len(afterDelete) > 0 {
		fmt.Printf("after delete %v\n", afterDelete[0])
	}

}

打印输出为:
&{6 test}
insession &{6 test}
before delete &{6 update}
after delete &{6 update}

关闭缓存之后的输出为:
get &{7 test}
insession &{7 test}
before delete &{7 update}

在开启缓存的时候,在session中删除数据,commit后发现之前缓存的数据依然可以查询到,数据表中已经删除,下面是我的代码 ``` type testtable struct { ID int64 `xorm:"pk autoincr"` Name string `xorm:"varchar(20)"` } func test_orm2(orm *xorm.Engine) { cacher := caches.NewLRUCacher2(caches.NewMemoryStore(), time.Hour*24, 1000000) orm.SetDefaultCacher(cacher) orm.Sync2(new(testtable)) ins := &testtable{Name: "test"} orm.Insert(ins) getresult := &testtable{} orm.ID(ins.ID).Get(getresult) fmt.Printf("get %v\n", getresult) ins.Name = "update" orm.Update(ins) session := orm.NewSession() session.Begin() ins.Name = "update session" session.Update(ins) orm.ID(ins.ID).Get(getresult) fmt.Printf("insession %v\n", getresult) var retarray []*testtable orm.Find(&retarray) if len(retarray) > 0 { fmt.Printf("before delete %v\n", retarray[0]) } session.ID(ins.ID).Delete(new(testtable)) session.Commit() session.Close() var afterDelete []*testtable err := orm.Find(&afterDelete) if err != nil { fmt.Printf("err:%v\n", err) } if len(afterDelete) > 0 { fmt.Printf("after delete %v\n", afterDelete[0]) } } ``` 打印输出为: &{6 test} insession &{6 test} before delete &{6 update} after delete &{6 update} 关闭缓存之后的输出为: get &{7 test} insession &{7 test} before delete &{7 update}
Author

开启日志后

[xorm] [info] 2022/04/29 09:45:53.193853 [SQL] SELECT TABLE_NAME, ENGINE, AUTO_INCREMENT, TABLE_COMMENT from INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=? AND (ENGINE='MyISAM' OR ENGINE = 'InnoDB' OR ENGINE = 'TokuDB') [xgames] - 921.944µs
[xorm] [info] 2022/04/29 09:45:53.194580 [SQL] SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_TYPE, COLUMN_KEY, EXTRA, COLUMN_COMMENT, (INSTR(VERSION(), 'maria') > 0 && (SUBSTRING_INDEX(VERSION(), '.', 1) > 10 || (SUBSTRING_INDEX(VERSION(), '.', 1) = 10 && (SUBSTRING_INDEX(SUBSTRING(VERSION(), 4), '.', 1) > 2 || (SUBSTRING_INDEX(SUBSTRING(VERSION(), 4), '.', 1) = 2 && SUBSTRING_INDEX(SUBSTRING(VERSION(), 6), '-', 1) >= 7))))) AS NEEDS_QUOTE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? ORDER BY COLUMNS.ORDINAL_POSITION [xgames bw_testtable] - 576.489µs
[xorm] [info] 2022/04/29 09:45:53.194890 [SQL] SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? [xgames bw_testtable] - 227.154µs
[xorm] [info] 2022/04/29 09:45:53.199220 [SQL] INSERT INTO bw_testtable (name) VALUES (?) [created] - 3.611744ms
[xorm] [debug] 2022/04/29 09:45:53.199248 [cache] clear SQL: bw_testtable
[xorm] [debug] 2022/04/29 09:45:53.199322 [cache] Get SQL: SELECT i_d FROM bw_testtable WHERE i_d=? LIMIT 1, [20]
[xorm] [info] 2022/04/29 09:45:53.199763 [SQL] SELECT i_d FROM bw_testtable WHERE i_d=? LIMIT 1 [20] - 406.59µs
[xorm] [debug] 2022/04/29 09:45:53.199797 [cache] cache ids: SELECT i_d FROM bw_testtable WHERE i_d=? LIMIT 1, 20
[xorm] [debug] 2022/04/29 09:45:53.199889 [cache] get bean: bw_testtable, [20]
[xorm] [info] 2022/04/29 09:45:53.200783 [SQL] SELECT i_d, name FROM bw_testtable WHERE i_d=? LIMIT 1 [20] - 857.924µs
[xorm] [debug] 2022/04/29 09:45:53.200863 [cache] cache bean: bw_testtable, [20], &{20 created}
get &{20 created}
[xorm] [info] 2022/04/29 09:45:53.202191 [SQL] UPDATE bw_testtable SET name = ? [update] - 1.232555ms
[xorm] [debug] 2022/04/29 09:45:53.202219 [cache] clear table: bw_testtable
[xorm] [info] 2022/04/29 09:45:53.202919 [SQL] BEGIN TRANSACTION [] - 687.836µs
[xorm] [info] 2022/04/29 09:45:53.203398 [SQL] UPDATE bw_testtable SET name = ? [update session] - 432.128µs
[xorm] [debug] 2022/04/29 09:45:53.203428 [cache] clear table: bw_testtable
[xorm] [info] 2022/04/29 09:45:53.203724 [SQL] SELECT i_d FROM bw_testtable [] - 237.675µs
[xorm] [debug] 2022/04/29 09:45:53.203761 [cache] cache sql: 20, bw_testtable, SELECT i_d, name FROM bw_testtable, SELECT i_d FROM bw_testtable, []
[xorm] [info] 2022/04/29 09:45:53.204151 [SQL] SELECT i_d, name FROM bw_testtable WHERE i_d IN (?) [20] - 301.434µs
[xorm] [debug] 2022/04/29 09:45:53.204261 [cache] cache bean: bw_testtable, [20], &{20 update}, [0xc0005767b0]
before delete &{20 update}
[xorm] [debug] 2022/04/29 09:45:53.204343 [cache] Get SQL: SELECT i_d FROM bw_testtable WHERE i_d=? AND name=? AND i_d=? LIMIT 1, [20 created 20]
[xorm] [info] 2022/04/29 09:45:53.204815 [SQL] SELECT i_d FROM bw_testtable WHERE i_d=? AND name=? AND i_d=? LIMIT 1 [20 created 20] - 443.279µs
[xorm] [info] 2022/04/29 09:45:53.205137 [SQL] SELECT i_d, name FROM bw_testtable WHERE i_d=? AND name=? AND i_d=? LIMIT 1 [20 created 20] - 267.931µs
find before commit &{20 created}
[xorm] [info] 2022/04/29 09:45:53.205590 [SQL] DELETE FROM bw_testtable WHERE i_d=? [20] - 350.765µs
[xorm] [info] 2022/04/29 09:45:53.207313 [SQL] COMMIT [] - 1.686394ms
[xorm] [debug] 2022/04/29 09:45:53.207409 [cache] cache hit sql: bw_testtable, SELECT i_d, name FROM bw_testtable, SELECT i_d FROM bw_testtable, []
[xorm] [debug] 2022/04/29 09:45:53.207444 [cache] cache hit bean: bw_testtable, [20], &{20 update}
find after delete &{20 update}
[xorm] [debug] 2022/04/29 09:45:53.207519 [cache] Get SQL: SELECT i_d FROM bw_testtable WHERE i_d=? AND name=? AND i_d=? LIMIT 1, [20 created 20]
[xorm] [info] 2022/04/29 09:45:53.208162 [SQL] SELECT i_d FROM bw_testtable WHERE i_d=? AND name=? AND i_d=? LIMIT 1 [20 created 20] - 613.367µs
[xorm] [info] 2022/04/29 09:45:53.208539 [SQL] SELECT i_d, name FROM bw_testtable WHERE i_d=? AND name=? AND i_d=? LIMIT 1 [20 created 20] - 320.81µs

开启日志后 [xorm] [info] 2022/04/29 09:45:53.193853 [SQL] SELECT `TABLE_NAME`, `ENGINE`, `AUTO_INCREMENT`, `TABLE_COMMENT` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? AND (`ENGINE`='MyISAM' OR `ENGINE` = 'InnoDB' OR `ENGINE` = 'TokuDB') [xgames] - 921.944µs [xorm] [info] 2022/04/29 09:45:53.194580 [SQL] SELECT `COLUMN_NAME`, `IS_NULLABLE`, `COLUMN_DEFAULT`, `COLUMN_TYPE`, `COLUMN_KEY`, `EXTRA`, `COLUMN_COMMENT`, (INSTR(VERSION(), 'maria') > 0 && (SUBSTRING_INDEX(VERSION(), '.', 1) > 10 || (SUBSTRING_INDEX(VERSION(), '.', 1) = 10 && (SUBSTRING_INDEX(SUBSTRING(VERSION(), 4), '.', 1) > 2 || (SUBSTRING_INDEX(SUBSTRING(VERSION(), 4), '.', 1) = 2 && SUBSTRING_INDEX(SUBSTRING(VERSION(), 6), '-', 1) >= 7))))) AS NEEDS_QUOTE FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? ORDER BY `COLUMNS`.ORDINAL_POSITION [xgames bw_testtable] - 576.489µs [xorm] [info] 2022/04/29 09:45:53.194890 [SQL] SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? [xgames bw_testtable] - 227.154µs [xorm] [info] 2022/04/29 09:45:53.199220 [SQL] INSERT INTO `bw_testtable` (`name`) VALUES (?) [created] - 3.611744ms [xorm] [debug] 2022/04/29 09:45:53.199248 [cache] clear SQL: bw_testtable [xorm] [debug] 2022/04/29 09:45:53.199322 [cache] Get SQL: SELECT `i_d` FROM `bw_testtable` WHERE `i_d`=? LIMIT 1, [20] [xorm] [info] 2022/04/29 09:45:53.199763 [SQL] SELECT `i_d` FROM `bw_testtable` WHERE `i_d`=? LIMIT 1 [20] - 406.59µs [xorm] [debug] 2022/04/29 09:45:53.199797 [cache] cache ids: SELECT `i_d` FROM `bw_testtable` WHERE `i_d`=? LIMIT 1, [[20]] [xorm] [debug] 2022/04/29 09:45:53.199889 [cache] get bean: bw_testtable, [20] [xorm] [info] 2022/04/29 09:45:53.200783 [SQL] SELECT `i_d`, `name` FROM `bw_testtable` WHERE `i_d`=? LIMIT 1 [20] - 857.924µs [xorm] [debug] 2022/04/29 09:45:53.200863 [cache] cache bean: bw_testtable, [20], &{20 created} get &{20 created} [xorm] [info] 2022/04/29 09:45:53.202191 [SQL] UPDATE `bw_testtable` SET `name` = ? [update] - 1.232555ms [xorm] [debug] 2022/04/29 09:45:53.202219 [cache] clear table: bw_testtable [xorm] [info] 2022/04/29 09:45:53.202919 [SQL] BEGIN TRANSACTION [] - 687.836µs [xorm] [info] 2022/04/29 09:45:53.203398 [SQL] UPDATE `bw_testtable` SET `name` = ? [update session] - 432.128µs [xorm] [debug] 2022/04/29 09:45:53.203428 [cache] clear table: bw_testtable [xorm] [info] 2022/04/29 09:45:53.203724 [SQL] SELECT `i_d` FROM `bw_testtable` [] - 237.675µs [xorm] [debug] 2022/04/29 09:45:53.203761 [cache] cache sql: [[20]], bw_testtable, SELECT `i_d`, `name` FROM `bw_testtable`, SELECT `i_d` FROM `bw_testtable`, [] [xorm] [info] 2022/04/29 09:45:53.204151 [SQL] SELECT `i_d`, `name` FROM `bw_testtable` WHERE `i_d` IN (?) [20] - 301.434µs [xorm] [debug] 2022/04/29 09:45:53.204261 [cache] cache bean: bw_testtable, [20], &{20 update}, [0xc0005767b0] before delete &{20 update} [xorm] [debug] 2022/04/29 09:45:53.204343 [cache] Get SQL: SELECT `i_d` FROM `bw_testtable` WHERE `i_d`=? AND `name`=? AND `i_d`=? LIMIT 1, [20 created 20] [xorm] [info] 2022/04/29 09:45:53.204815 [SQL] SELECT `i_d` FROM `bw_testtable` WHERE `i_d`=? AND `name`=? AND `i_d`=? LIMIT 1 [20 created 20] - 443.279µs [xorm] [info] 2022/04/29 09:45:53.205137 [SQL] SELECT `i_d`, `name` FROM `bw_testtable` WHERE `i_d`=? AND `name`=? AND `i_d`=? LIMIT 1 [20 created 20] - 267.931µs find before commit &{20 created} [xorm] [info] 2022/04/29 09:45:53.205590 [SQL] DELETE FROM `bw_testtable` WHERE `i_d`=? [20] - 350.765µs [xorm] [info] 2022/04/29 09:45:53.207313 [SQL] COMMIT [] - 1.686394ms [xorm] [debug] 2022/04/29 09:45:53.207409 [cache] cache hit sql: bw_testtable, SELECT `i_d`, `name` FROM `bw_testtable`, SELECT `i_d` FROM `bw_testtable`, [] [xorm] [debug] 2022/04/29 09:45:53.207444 [cache] cache hit bean: bw_testtable, [20], &{20 update} find after delete &{20 update} [xorm] [debug] 2022/04/29 09:45:53.207519 [cache] Get SQL: SELECT `i_d` FROM `bw_testtable` WHERE `i_d`=? AND `name`=? AND `i_d`=? LIMIT 1, [20 created 20] [xorm] [info] 2022/04/29 09:45:53.208162 [SQL] SELECT `i_d` FROM `bw_testtable` WHERE `i_d`=? AND `name`=? AND `i_d`=? LIMIT 1 [20 created 20] - 613.367µs [xorm] [info] 2022/04/29 09:45:53.208539 [SQL] SELECT `i_d`, `name` FROM `bw_testtable` WHERE `i_d`=? AND `name`=? AND `i_d`=? LIMIT 1 [20 created 20] - 320.81µs
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#2140
No description provided.