Fix system config cache expiration timing (#28072)

To avoid unnecessary database access, the `cacheTime` should always be
set if the revision has been checked.

Fix #28057
This commit is contained in:
wxiaoguang 2023-11-16 20:53:42 +08:00 committed by GitHub
parent 49dddd87b1
commit fce1d5d7dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 8 deletions

View File

@ -115,24 +115,26 @@ func (d *dbConfigCachedGetter) GetValue(ctx context.Context, key string) (v stri
func (d *dbConfigCachedGetter) GetRevision(ctx context.Context) int {
d.mu.RLock()
defer d.mu.RUnlock()
if time.Since(d.cacheTime) < time.Second {
return d.revision
}
if GetRevision(ctx) != d.revision {
cachedDuration := time.Since(d.cacheTime)
cachedRevision := d.revision
d.mu.RUnlock()
if cachedDuration < time.Second {
return cachedRevision
}
d.mu.Lock()
defer d.mu.Unlock()
if GetRevision(ctx) != d.revision {
rev, set, err := GetAllSettings(ctx)
if err != nil {
log.Error("Unable to get all settings: %v", err)
} else {
d.cacheTime = time.Now()
d.revision = rev
d.settings = set
}
d.mu.Unlock()
d.mu.RLock()
}
d.cacheTime = time.Now()
return d.revision
}