refactor: move go-chi/cache into Gitea (#39530)

The `gitea.com/go-chi/cache` package only exists for Gitea, so it moves
into `modules/cache`.

- `ITEM_TTL = -1` disables caching as documented
- Sub-second TTLs round up instead of never expiring
- The Redis adapter no longer grows a `MacaronCache` hash
- Use two-queue cache for in-memory cache

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
silverwind
2026-10-02 18:16:34 +02:00
committed by GitHub
parent 873efed5a6
commit bea6fcaa84
14 changed files with 342 additions and 381 deletions
+12 -13
View File
@@ -4,6 +4,7 @@
package setting
import (
"math"
"time"
"gitea.dev/modules/log"
@@ -37,9 +38,6 @@ var CacheService = struct {
},
}
// MemcacheMaxTTL represents the maximum memcache TTL
const MemcacheMaxTTL = 30 * 24 * time.Hour
func loadCacheFrom(rootCfg ConfigProvider) {
sec := rootCfg.Section("cache")
if err := sec.MapTo(&CacheService); err != nil {
@@ -60,18 +58,19 @@ func loadCacheFrom(rootCfg ConfigProvider) {
}
}
// TTLSeconds returns the TTLSeconds or unix timestamp for memcache
// TTLSeconds returns the item TTL in seconds, negative when ITEM_TTL disables caching
func (c Cache) TTLSeconds() int64 {
if c.Adapter == "memcache" && c.TTL > MemcacheMaxTTL {
return time.Now().Add(c.TTL).Unix()
}
return int64(c.TTL.Seconds())
return ttlSeconds(c.TTL)
}
// LastCommitCacheTTLSeconds returns the TTLSeconds or unix timestamp for memcache
// LastCommitCacheTTLSeconds returns the last commit item TTL in seconds, negative when ITEM_TTL disables caching
func LastCommitCacheTTLSeconds() int64 {
if CacheService.Adapter == "memcache" && CacheService.LastCommit.TTL > MemcacheMaxTTL {
return time.Now().Add(CacheService.LastCommit.TTL).Unix()
}
return int64(CacheService.LastCommit.TTL.Seconds())
return ttlSeconds(CacheService.LastCommit.TTL)
}
func ttlSeconds(ttl time.Duration) int64 {
if ttl < 0 {
return -1
}
return int64(math.Ceil(ttl.Seconds()))
}
+11
View File
@@ -69,3 +69,14 @@ ADAPTER = memcache
})
}
}
func TestCacheItemTTLSeconds(t *testing.T) {
defer test.MockVariableValue(&CacheService)()
for ttl, want := range map[string]int64{"-1": -1, "500ms": 1} {
cfg, err := NewConfigProviderFromData("[cache]\nITEM_TTL = " + ttl + "\n[cache.last_commit]\nITEM_TTL = " + ttl)
assert.NoError(t, err)
loadCacheFrom(cfg)
assert.Equal(t, want, CacheService.TTLSeconds())
assert.Equal(t, want, LastCommitCacheTTLSeconds())
}
}