]> source.dussan.org Git - gitea.git/commitdiff
Fix system config cache expiration timing (#28072)
authorwxiaoguang <wxiaoguang@gmail.com>
Thu, 16 Nov 2023 12:53:42 +0000 (20:53 +0800)
committerGitHub <noreply@github.com>
Thu, 16 Nov 2023 12:53:42 +0000 (12:53 +0000)
To avoid unnecessary database access, the `cacheTime` should always be
set if the revision has been checked.

Fix #28057

models/system/setting.go

index 1ae6f5c652836b8a1da8678c316d1f8240aef16b..507d23cff6bbfc132bb2926f772d62922396d55f 100644 (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
+       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 {
-               d.mu.RUnlock()
-               d.mu.Lock()
                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
 }