File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,18 +38,18 @@ func AfterDelete(cache *Gorm2Cache) func(db *gorm.DB) {
3838 if err != nil {
3939 cache .Logger .CtxError (ctx , "[AfterDelete] invalidating cache for primary keys: %v error: %v" ,
4040 primaryKeys , err )
41- return
41+ } else {
42+ cache .Logger .CtxInfo (ctx , "[AfterDelete] invalidating cache for primary keys: %v finished." , primaryKeys )
4243 }
43- cache .Logger .CtxInfo (ctx , "[AfterDelete] invalidating cache for primary keys: %v finished." , primaryKeys )
4444 } else {
4545 cache .Logger .CtxInfo (ctx , "[AfterDelete] now start to invalidate all primary cache for table: %s" , tableName )
4646 err := cache .InvalidateAllPrimaryCache (ctx , tableName )
4747 if err != nil {
4848 cache .Logger .CtxError (ctx , "[AfterDelete] invalidating primary cache for table %s error: %v" ,
4949 tableName , err )
50- return
50+ } else {
51+ cache .Logger .CtxInfo (ctx , "[AfterDelete] invalidating all primary cache for table: %s finished." , tableName )
5152 }
52- cache .Logger .CtxInfo (ctx , "[AfterDelete] invalidating all primary cache for table: %s finished." , tableName )
5353 }
5454
5555 // 失效unique键缓存
Original file line number Diff line number Diff line change @@ -40,18 +40,18 @@ func AfterUpdate(cache *Gorm2Cache) func(db *gorm.DB) {
4040 if err != nil {
4141 cache .Logger .CtxError (ctx , "[AfterUpdate] invalidating primary cache for key %v error: %v" ,
4242 primaryKeys , err )
43- return
43+ } else {
44+ cache .Logger .CtxInfo (ctx , "[AfterUpdate] invalidating cache for primary keys: %+v finished." , primaryKeys )
4445 }
45- cache .Logger .CtxInfo (ctx , "[AfterUpdate] invalidating cache for primary keys: %+v finished." , primaryKeys )
4646 } else {
4747 cache .Logger .CtxInfo (ctx , "[AfterUpdate] now start to invalidate all primary cache for table: %s" , tableName )
4848 err := cache .InvalidateAllPrimaryCache (ctx , tableName )
4949 if err != nil {
5050 cache .Logger .CtxError (ctx , "[AfterUpdate] invalidating primary cache for table %s error: %v" ,
5151 tableName , err )
52- return
52+ } else {
53+ cache .Logger .CtxInfo (ctx , "[AfterUpdate] invalidating all primary cache for table: %s finished." , tableName )
5354 }
54- cache .Logger .CtxInfo (ctx , "[AfterUpdate] invalidating all primary cache for table: %s finished." , tableName )
5555 }
5656
5757 // 失效unique键缓存
Original file line number Diff line number Diff line change @@ -437,6 +437,8 @@ func (h *queryHandler) AfterQuery() func(db *gorm.DB) {
437437 wg .Wait ()
438438 cancel ()
439439 }()
440+ // 替换为 no-op,fillCallAfterQuery 统一调用 cancel 时不会提前中止异步写;cache hit 时仍是真实 cancel,会释放 timer
441+ db .InstanceSet ("gorm:cache:query:single_flight_cancel" , context .CancelFunc (func () {}))
440442 }
441443 }
442444 } else {
@@ -501,12 +503,10 @@ func (h *queryHandler) fillCallAfterQuery(db *gorm.DB) {
501503 return
502504 }
503505 // 释放 singleflight leader 使用的 background context,避免 timer 泄漏
504- // AsyncWrite 时由 AfterQuery 内启动的 goroutine 在 wg.Wait() 后 cancel,此处不 cancel 以免提前中止异步写缓存
505- if ! h .cache .Config .AsyncWrite {
506- if cancelObj , hasCancel := db .InstanceGet ("gorm:cache:query:single_flight_cancel" ); hasCancel {
507- if cancel , ok := cancelObj .(context.CancelFunc ); ok {
508- cancel ()
509- }
506+ // AsyncWrite 且走异步写路径时已把 cancel 替换为 no-op;cache hit 时此处调用真实 cancel,避免 30s timer 泄漏
507+ if cancelObj , hasCancel := db .InstanceGet ("gorm:cache:query:single_flight_cancel" ); hasCancel {
508+ if cancel , ok := cancelObj .(context.CancelFunc ); ok {
509+ cancel ()
510510 }
511511 }
512512 c .dest = db .Statement .Dest
You can’t perform that action at this time.
0 commit comments