Skip to content

Commit 3bc9436

Browse files
authored
fix: fix key of singleflight not contains vars
1 parent 8f13c82 commit 3bc9436

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- 即插即用
77
- 旁路缓存
88
- 穿透防护
9+
- 击穿防护
910
- 多存储介质(内存/redis)
1011

1112
## 使用说明

cache/query.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
5757
db.InstanceSet("gorm:cache:vars", db.Statement.Vars)
5858

5959
if util.ShouldCache(tableName, cache.Config.Tables) {
60-
hitted := false
60+
hit := false
6161
defer func() {
62-
if hitted {
62+
if hit {
6363
cache.IncrHitCount()
6464
} else {
6565
cache.IncrMissCount()
6666
}
6767
}()
6868

6969
// singleFlight Check
70-
singleFlightKey := fmt.Sprintf("%s:%s", tableName, sql) // todo: key with vars
70+
singleFlightKey := util.GenSingleFlightKey(tableName, sql, db.Statement.Vars...)
7171
h.singleFlight.mu.Lock()
7272
if h.singleFlight.m == nil {
7373
h.singleFlight.m = make(map[string]*call)
@@ -88,7 +88,7 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
8888
_ = db.AddError(err)
8989
return
9090
}
91-
hitted = true
91+
hit = true
9292
db.RowsAffected = c.rowsAffected
9393
db.Error = multierror.Append(util.SingleFlightHit) // 为保证后续流程不走,必须设一个error
9494
if c.err != nil {
@@ -103,7 +103,7 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
103103
h.singleFlight.mu.Unlock()
104104
db.InstanceSet("gorm:cache:query:single_flight_call", c)
105105

106-
tryPrimaryCache := func() (hitted bool) {
106+
tryPrimaryCache := func() (hit bool) {
107107
primaryKeys := getPrimaryKeysFromWhereClause(db)
108108
cache.Logger.CtxInfo(ctx, "[BeforeQuery] parse primary keys = %v", primaryKeys)
109109

@@ -150,11 +150,11 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
150150
return
151151
}
152152
db.Error = util.PrimaryCacheHit
153-
hitted = true
153+
hit = true
154154
return
155155
}
156156

157-
trySearchCache := func() (hitted bool) {
157+
trySearchCache := func() (hit bool) {
158158
// search cache hit
159159
cacheValue, err := cache.GetSearchCache(ctx, tableName, sql, db.Statement.Vars...)
160160
if err != nil {
@@ -167,7 +167,7 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
167167
cache.Logger.CtxInfo(ctx, "[BeforeQuery] get value: %s", cacheValue)
168168
if cacheValue == "recordNotFound" { // 应对缓存穿透
169169
db.Error = util.RecordNotFoundCacheHit
170-
hitted = true
170+
hit = true
171171
return
172172
}
173173
rowsAffectedPos := strings.Index(cacheValue, "|")
@@ -184,19 +184,19 @@ func (h *queryHandler) BeforeQuery() func(db *gorm.DB) {
184184
return
185185
}
186186
db.Error = util.SearchCacheHit
187-
hitted = true
187+
hit = true
188188
return
189189
}
190190

191191
if cache.Config.CacheLevel == config.CacheLevelAll || cache.Config.CacheLevel == config.CacheLevelOnlyPrimary {
192192
if tryPrimaryCache() {
193-
hitted = true
193+
hit = true
194194
return
195195
}
196196
}
197197
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
200200
}
201201
}
202202
}

util/key.go

+14
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ func GenSearchCacheKey(instanceId string, tableName string, sql string, vars ...
4444
func GenSearchCachePrefix(instanceId string, tableName string) string {
4545
return GormCachePrefix + ":" + instanceId + ":s:" + tableName
4646
}
47+
48+
func GenSingleFlightKey(tableName string, sql string, vars ...interface{}) string {
49+
buf := strings.Builder{}
50+
buf.WriteString(sql)
51+
for _, v := range vars {
52+
pv := reflect.ValueOf(v)
53+
if pv.Kind() == reflect.Ptr {
54+
buf.WriteString(fmt.Sprintf(":%v", pv.Elem()))
55+
} else {
56+
buf.WriteString(fmt.Sprintf(":%v", v))
57+
}
58+
}
59+
return fmt.Sprintf("%s:%s", tableName, buf.String())
60+
}

0 commit comments

Comments
 (0)