From 4d082c2dce4e7a1cb6e8a24a28e8c24a7e29871f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=88=90=E9=94=B4?= Date: Tue, 21 Feb 2023 14:27:47 +0800 Subject: [PATCH] feat: support async update cache --- README.md | 2 +- cache/afterCreate.go | 25 ++++++++++++++++--------- cache/afterDelete.go | 4 +++- cache/afterQuery.go | 4 +++- cache/afterUpdate.go | 4 +++- config/config.go | 3 +++ 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 97e35ef..8ccc180 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ func main() { 4. Update (Update/Updates/UpdateColumn/UpdateColumns/Save) 5. Row (Row/Rows/Scan) -本库不支持Row操作的缓存。 +本库不支持Row操作的缓存。(WIP) ## 存储介质细节 diff --git a/cache/afterCreate.go b/cache/afterCreate.go index 97906bb..831cc38 100644 --- a/cache/afterCreate.go +++ b/cache/afterCreate.go @@ -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) } } } diff --git a/cache/afterDelete.go b/cache/afterDelete.go index ddbf024..cd3f3f7 100644 --- a/cache/afterDelete.go +++ b/cache/afterDelete.go @@ -70,7 +70,9 @@ func AfterDelete(cache *Gorm2Cache) func(db *gorm.DB) { } }() - wg.Wait() + if !cache.Config.AsyncWrite { + wg.Wait() + } } } } diff --git a/cache/afterQuery.go b/cache/afterQuery.go index bab20ab..3c27269 100644 --- a/cache/afterQuery.go +++ b/cache/afterQuery.go @@ -88,7 +88,9 @@ func AfterQuery(cache *Gorm2Cache) func(db *gorm.DB) { } } }() - wg.Wait() + if !cache.Config.AsyncWrite { + wg.Wait() + } return } diff --git a/cache/afterUpdate.go b/cache/afterUpdate.go index fa89bcc..0deb1c2 100644 --- a/cache/afterUpdate.go +++ b/cache/afterUpdate.go @@ -71,7 +71,9 @@ func AfterUpdate(cache *Gorm2Cache) func(db *gorm.DB) { } }() - wg.Wait() + if !cache.Config.AsyncWrite { + wg.Wait() + } } } } diff --git a/config/config.go b/config/config.go index b71f3a7..250a4c3 100644 --- a/config/config.go +++ b/config/config.go @@ -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