@@ -57,17 +57,17 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
57
57
db .InstanceSet ("gorm:cache:vars" , db .Statement .Vars )
58
58
59
59
if util .ShouldCache (tableName , cache .Config .Tables ) {
60
- hitted := false
60
+ hit := false
61
61
defer func () {
62
- if hitted {
62
+ if hit {
63
63
cache .IncrHitCount ()
64
64
} else {
65
65
cache .IncrMissCount ()
66
66
}
67
67
}()
68
68
69
69
// singleFlight Check
70
- singleFlightKey := fmt . Sprintf ( "%s:%s" , tableName , sql ) // todo: key with vars
70
+ singleFlightKey := util . GenSingleFlightKey ( tableName , sql , db . Statement . Vars ... )
71
71
h .singleFlight .mu .Lock ()
72
72
if h .singleFlight .m == nil {
73
73
h .singleFlight .m = make (map [string ]* call )
@@ -88,7 +88,7 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
88
88
_ = db .AddError (err )
89
89
return
90
90
}
91
- hitted = true
91
+ hit = true
92
92
db .RowsAffected = c .rowsAffected
93
93
db .Error = multierror .Append (util .SingleFlightHit ) // 为保证后续流程不走,必须设一个error
94
94
if c .err != nil {
@@ -103,7 +103,7 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
103
103
h .singleFlight .mu .Unlock ()
104
104
db .InstanceSet ("gorm:cache:query:single_flight_call" , c )
105
105
106
- tryPrimaryCache := func () (hitted bool ) {
106
+ tryPrimaryCache := func () (hit bool ) {
107
107
primaryKeys := getPrimaryKeysFromWhereClause (db )
108
108
cache .Logger .CtxInfo (ctx , "[BeforeQuery] parse primary keys = %v" , primaryKeys )
109
109
@@ -150,11 +150,11 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
150
150
return
151
151
}
152
152
db .Error = util .PrimaryCacheHit
153
- hitted = true
153
+ hit = true
154
154
return
155
155
}
156
156
157
- trySearchCache := func () (hitted bool ) {
157
+ trySearchCache := func () (hit bool ) {
158
158
// search cache hit
159
159
cacheValue , err := cache .GetSearchCache (ctx , tableName , sql , db .Statement .Vars ... )
160
160
if err != nil {
@@ -167,7 +167,7 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
167
167
cache .Logger .CtxInfo (ctx , "[BeforeQuery] get value: %s" , cacheValue )
168
168
if cacheValue == "recordNotFound" { // 应对缓存穿透
169
169
db .Error = util .RecordNotFoundCacheHit
170
- hitted = true
170
+ hit = true
171
171
return
172
172
}
173
173
rowsAffectedPos := strings .Index (cacheValue , "|" )
@@ -184,19 +184,19 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
184
184
return
185
185
}
186
186
db .Error = util .SearchCacheHit
187
- hitted = true
187
+ hit = true
188
188
return
189
189
}
190
190
191
191
if cache .Config .CacheLevel == config .CacheLevelAll || cache .Config .CacheLevel == config .CacheLevelOnlyPrimary {
192
192
if tryPrimaryCache () {
193
- hitted = true
193
+ hit = true
194
194
return
195
195
}
196
196
}
197
197
if cache .Config .CacheLevel == config .CacheLevelAll || cache .Config .CacheLevel == config .CacheLevelOnlySearch {
198
- if ! hitted && trySearchCache () {
199
- hitted = true
198
+ if ! hit && trySearchCache () {
199
+ hit = true
200
200
}
201
201
}
202
202
}
0 commit comments