Skip to content

Commit

Permalink
feat: support async update cache
Browse files Browse the repository at this point in the history
  • Loading branch information
asjdf committed Feb 21, 2023
1 parent e9b2c0f commit 4d082c2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
4. Update (Update/Updates/UpdateColumn/UpdateColumns/Save)
5. Row (Row/Rows/Scan)

本库不支持Row操作的缓存。
本库不支持Row操作的缓存。(WIP)

## 存储介质细节

Expand Down
25 changes: 16 additions & 9 deletions cache/afterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ func AfterCreate(cache *Gorm2Cache) func(db *gorm.DB) {

if db.Error == nil && cache.Config.InvalidateWhenUpdate && util.ShouldCache(tableName, cache.Config.Tables) {
if cache.Config.CacheLevel == config.CacheLevelAll || cache.Config.CacheLevel == config.CacheLevelOnlySearch {
// We invalidate search cache here,
// because any newly created objects may cause search cache results to be outdated and invalid.
cache.Logger.CtxInfo(ctx, "[AfterCreate] now start to invalidate search cache for table: %s", tableName)
err := cache.InvalidateSearchCache(ctx, tableName)
if err != nil {
cache.Logger.CtxError(ctx, "[AfterCreate] invalidating search cache for table %s error: %v",
tableName, err)
return
invalidSearchCache := func() {
// We invalidate search cache here,
// because any newly created objects may cause search cache results to be outdated and invalid.
cache.Logger.CtxInfo(ctx, "[AfterCreate] now start to invalidate search cache for table: %s", tableName)
err := cache.InvalidateSearchCache(ctx, tableName)
if err != nil {
cache.Logger.CtxError(ctx, "[AfterCreate] invalidating search cache for table %s error: %v",
tableName, err)
return
}
cache.Logger.CtxInfo(ctx, "[AfterCreate] invalidating search cache for table: %s finished.", tableName)
}
if cache.Config.AsyncWrite {
go invalidSearchCache()
} else {
invalidSearchCache()
}
cache.Logger.CtxInfo(ctx, "[AfterCreate] invalidating search cache for table: %s finished.", tableName)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion cache/afterDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func AfterDelete(cache *Gorm2Cache) func(db *gorm.DB) {
}
}()

wg.Wait()
if !cache.Config.AsyncWrite {
wg.Wait()
}
}
}
}
4 changes: 3 additions & 1 deletion cache/afterQuery.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func AfterQuery(cache *Gorm2Cache) func(db *gorm.DB) {
}
}
}()
wg.Wait()
if !cache.Config.AsyncWrite {
wg.Wait()
}
return
}

Expand Down
4 changes: 3 additions & 1 deletion cache/afterUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func AfterUpdate(cache *Gorm2Cache) func(db *gorm.DB) {
}
}()

wg.Wait()
if !cache.Config.AsyncWrite {
wg.Wait()
}
}
}
}
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type CacheConfig struct {
// else we do nothing to outdated cache.
InvalidateWhenUpdate bool

// AsyncWrite if true, then we will write cache in async mode
AsyncWrite bool

// CacheTTL cache ttl in ms, where 0 represents forever
CacheTTL int64

Expand Down

0 comments on commit 4d082c2

Please sign in to comment.