mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-03 20:20:52 +00:00
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:
Vendored
+1
-3
@@ -13,8 +13,6 @@ import (
|
||||
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/util"
|
||||
|
||||
_ "gitea.com/go-chi/cache/memcache" //nolint:depguard // memcache plugin for cache, it is required for config "ADAPTER=memcache"
|
||||
)
|
||||
|
||||
var defaultCache StringCache
|
||||
@@ -83,7 +81,7 @@ func GetCache() StringCache {
|
||||
|
||||
// GetString returns the key value from cache with callback when no key exists in cache
|
||||
func GetString(key string, getFunc func() (string, error)) (string, error) {
|
||||
if defaultCache == nil || setting.CacheService.TTL == 0 {
|
||||
if defaultCache == nil || setting.CacheService.TTL <= 0 {
|
||||
return getFunc()
|
||||
}
|
||||
cached, exist := defaultCache.Get(key)
|
||||
|
||||
Vendored
+70
@@ -0,0 +1,70 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/bradfitz/gomemcache/memcache"
|
||||
)
|
||||
|
||||
const memcacheMaxRelativeTTL = 30 * 24 * 60 * 60
|
||||
|
||||
type memcacheCache struct {
|
||||
client *memcache.Client
|
||||
}
|
||||
|
||||
func newMemcacheCache(conn string) (backend, error) {
|
||||
if conn == "" {
|
||||
return nil, errors.New("cache adapter memcache requires [cache] HOST, e.g. 127.0.0.1:11211")
|
||||
}
|
||||
servers := &memcache.ServerList{}
|
||||
if err := servers.SetServers(strings.Split(conn, ";")...); err != nil {
|
||||
return nil, fmt.Errorf("invalid memcache HOST %q: %w", conn, err)
|
||||
}
|
||||
return &memcacheCache{client: memcache.NewFromSelector(servers)}, nil
|
||||
}
|
||||
|
||||
// memcacheExpiration converts a TTL beyond memcached's 30-day relative limit into an absolute unix time
|
||||
func memcacheExpiration(ttl int64) int32 {
|
||||
if ttl > memcacheMaxRelativeTTL {
|
||||
ttl += timeNow().Unix()
|
||||
}
|
||||
return int32(min(ttl, math.MaxInt32))
|
||||
}
|
||||
|
||||
func (c *memcacheCache) Get(key string) (string, bool) {
|
||||
item, err := c.client.Get(key)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return string(item.Value), true
|
||||
}
|
||||
|
||||
func (c *memcacheCache) Put(key, value string, ttl int64) error {
|
||||
return c.client.Set(&memcache.Item{Key: key, Value: []byte(value), Expiration: memcacheExpiration(ttl)})
|
||||
}
|
||||
|
||||
func (c *memcacheCache) Delete(key string) error {
|
||||
if err := c.client.Delete(key); err != nil && !errors.Is(err, memcache.ErrCacheMiss) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *memcacheCache) IsExist(key string) bool {
|
||||
_, err := c.client.Get(key)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (c *memcacheCache) Ping() error {
|
||||
const key = "__gitea_cache_ping"
|
||||
if err := c.Put(key, "ping", 10); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Delete(key)
|
||||
}
|
||||
Vendored
+19
-134
@@ -4,159 +4,44 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gitea.dev/modules/graceful"
|
||||
"gitea.dev/modules/nosql"
|
||||
|
||||
"gitea.com/go-chi/cache" //nolint:depguard // we wrap this package here
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// RedisCacher represents a redis cache adapter implementation.
|
||||
type RedisCacher struct {
|
||||
c redis.UniversalClient
|
||||
prefix string
|
||||
hsetName string
|
||||
occupyMode bool
|
||||
type redisCache struct {
|
||||
client redis.UniversalClient
|
||||
prefix string
|
||||
}
|
||||
|
||||
// toStr convert string/int/int64 interface to string. it's only used by the RedisCacher.Put internally
|
||||
func toStr(v any) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []byte:
|
||||
return string(v)
|
||||
case int:
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
case int64:
|
||||
return strconv.FormatInt(v, 10)
|
||||
default:
|
||||
return fmt.Sprint(v) // as what the old com.ToStr does in most cases
|
||||
func newRedisCache(conn string) backend {
|
||||
uri := nosql.ToRedisURI(conn)
|
||||
return &redisCache{
|
||||
client: nosql.GetManager().GetRedisClient(uri.String()),
|
||||
prefix: uri.Query().Get("prefix"),
|
||||
}
|
||||
}
|
||||
|
||||
// Put puts value (string type) into cache with key and expire time.
|
||||
// If expired is 0, it lives forever.
|
||||
func (c *RedisCacher) Put(key string, val any, expire int64) error {
|
||||
// this function is not well-designed, it only puts string values into cache
|
||||
key = c.prefix + key
|
||||
if expire == 0 {
|
||||
if err := c.c.Set(graceful.GetManager().HammerContext(), key, toStr(val), 0).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
dur := time.Duration(expire) * time.Second
|
||||
if err := c.c.Set(graceful.GetManager().HammerContext(), key, toStr(val), dur).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if c.occupyMode {
|
||||
return nil
|
||||
}
|
||||
return c.c.HSet(graceful.GetManager().HammerContext(), c.hsetName, key, "0").Err()
|
||||
func (c *redisCache) Get(key string) (string, bool) {
|
||||
value, err := c.client.Get(graceful.GetManager().HammerContext(), c.prefix+key).Result()
|
||||
return value, err == nil
|
||||
}
|
||||
|
||||
// Get gets cached value by given key.
|
||||
func (c *RedisCacher) Get(key string) any {
|
||||
val, err := c.c.Get(graceful.GetManager().HammerContext(), c.prefix+key).Result()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return val
|
||||
func (c *redisCache) Put(key, value string, ttl int64) error {
|
||||
return c.client.Set(graceful.GetManager().HammerContext(), c.prefix+key, value, time.Duration(ttl)*time.Second).Err()
|
||||
}
|
||||
|
||||
// Delete deletes cached value by given key.
|
||||
func (c *RedisCacher) Delete(key string) error {
|
||||
key = c.prefix + key
|
||||
if err := c.c.Del(graceful.GetManager().HammerContext(), key).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.occupyMode {
|
||||
return nil
|
||||
}
|
||||
return c.c.HDel(graceful.GetManager().HammerContext(), c.hsetName, key).Err()
|
||||
func (c *redisCache) Delete(key string) error {
|
||||
return c.client.Del(graceful.GetManager().HammerContext(), c.prefix+key).Err()
|
||||
}
|
||||
|
||||
// Incr increases cached int-type value by given key as a counter.
|
||||
func (c *RedisCacher) Incr(key string) error {
|
||||
if !c.IsExist(key) {
|
||||
return fmt.Errorf("key '%s' not exist", key)
|
||||
}
|
||||
return c.c.Incr(graceful.GetManager().HammerContext(), c.prefix+key).Err()
|
||||
func (c *redisCache) IsExist(key string) bool {
|
||||
return c.client.Exists(graceful.GetManager().HammerContext(), c.prefix+key).Val() == 1
|
||||
}
|
||||
|
||||
// Decr decreases cached int-type value by given key as a counter.
|
||||
func (c *RedisCacher) Decr(key string) error {
|
||||
if !c.IsExist(key) {
|
||||
return fmt.Errorf("key '%s' not exist", key)
|
||||
}
|
||||
return c.c.Decr(graceful.GetManager().HammerContext(), c.prefix+key).Err()
|
||||
}
|
||||
|
||||
// IsExist returns true if cached value exists.
|
||||
func (c *RedisCacher) IsExist(key string) bool {
|
||||
if c.c.Exists(graceful.GetManager().HammerContext(), c.prefix+key).Val() == 1 {
|
||||
return true
|
||||
}
|
||||
|
||||
if !c.occupyMode {
|
||||
c.c.HDel(graceful.GetManager().HammerContext(), c.hsetName, c.prefix+key)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Flush deletes all cached data.
|
||||
func (c *RedisCacher) Flush() error {
|
||||
if c.occupyMode {
|
||||
return c.c.FlushDB(graceful.GetManager().HammerContext()).Err()
|
||||
}
|
||||
|
||||
keys, err := c.c.HKeys(graceful.GetManager().HammerContext(), c.hsetName).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = c.c.Del(graceful.GetManager().HammerContext(), keys...).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.c.Del(graceful.GetManager().HammerContext(), c.hsetName).Err()
|
||||
}
|
||||
|
||||
// StartAndGC starts GC routine based on config string settings.
|
||||
// AdapterConfig: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180,hset_name=MacaronCache,prefix=cache:
|
||||
func (c *RedisCacher) StartAndGC(opts cache.Options) error {
|
||||
c.hsetName = "MacaronCache"
|
||||
c.occupyMode = opts.OccupyMode
|
||||
|
||||
uri := nosql.ToRedisURI(opts.AdapterConfig)
|
||||
|
||||
c.c = nosql.GetManager().GetRedisClient(uri.String())
|
||||
|
||||
for k, v := range uri.Query() {
|
||||
switch k {
|
||||
case "hset_name":
|
||||
c.hsetName = v[0]
|
||||
case "prefix":
|
||||
c.prefix = v[0]
|
||||
}
|
||||
}
|
||||
|
||||
return c.c.Ping(graceful.GetManager().HammerContext()).Err()
|
||||
}
|
||||
|
||||
// Ping tests if the cache is alive.
|
||||
func (c *RedisCacher) Ping() error {
|
||||
return c.c.Ping(graceful.GetManager().HammerContext()).Err()
|
||||
}
|
||||
|
||||
func init() {
|
||||
cache.Register("redis", &RedisCacher{})
|
||||
func (c *redisCache) Ping() error {
|
||||
return c.client.Ping(graceful.GetManager().HammerContext()).Err()
|
||||
}
|
||||
|
||||
Vendored
+95
@@ -8,9 +8,12 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.dev/modules/nosql"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func createTestCache() {
|
||||
@@ -32,6 +35,98 @@ func TestNewContext(t *testing.T) {
|
||||
})
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, con)
|
||||
|
||||
_, err = NewStringCache(setting.Cache{Adapter: "memcache"})
|
||||
assert.ErrorContains(t, err, "requires [cache] HOST")
|
||||
}
|
||||
|
||||
func TestStringCacheAdapters(t *testing.T) {
|
||||
now := time.Now()
|
||||
defer test.MockVariableValue(&timeNow, func() time.Time { return now })()
|
||||
cases := []struct {
|
||||
adapter string
|
||||
conns func(t *testing.T) (string, string)
|
||||
}{
|
||||
{adapter: "memory", conns: func(*testing.T) (string, string) { return "", "" }},
|
||||
{adapter: "twoqueue", conns: func(*testing.T) (string, string) { return "100", `{"size":100}` }},
|
||||
{adapter: "redis", conns: func(t *testing.T) (string, string) {
|
||||
conn := test.PrepareTestRedis(t) + "?prefix=gitea-test-cache-"
|
||||
return conn + "first:", conn + "second:"
|
||||
}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.adapter, func(t *testing.T) {
|
||||
firstConn, secondConn := tc.conns(t)
|
||||
first, err := NewStringCache(setting.Cache{Adapter: tc.adapter, Conn: firstConn, Interval: -1})
|
||||
require.NoError(t, err)
|
||||
second, err := NewStringCache(setting.Cache{Adapter: tc.adapter, Conn: secondConn, Interval: -1})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, first.Ping())
|
||||
|
||||
require.NoError(t, first.Put("key", "value", 0))
|
||||
value, ok := first.Get("key")
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "value", value)
|
||||
assert.True(t, first.IsExist("key"))
|
||||
assert.False(t, second.IsExist("key"))
|
||||
|
||||
require.NoError(t, first.Delete("key"))
|
||||
_, ok = first.Get("key")
|
||||
assert.False(t, ok)
|
||||
assert.False(t, first.IsExist("key"))
|
||||
require.NoError(t, first.Delete("key"))
|
||||
|
||||
require.NoError(t, first.Put("expiring", "value", 10))
|
||||
if tc.adapter == "redis" {
|
||||
uri := nosql.ToRedisURI(firstConn)
|
||||
ttl := nosql.GetManager().GetRedisClient(uri.String()).TTL(t.Context(), uri.Query().Get("prefix")+"expiring").Val()
|
||||
assert.Positive(t, ttl)
|
||||
assert.LessOrEqual(t, ttl, 10*time.Second)
|
||||
require.NoError(t, first.Delete("expiring"))
|
||||
return
|
||||
}
|
||||
now = now.Add(9 * time.Second)
|
||||
assert.True(t, first.IsExist("expiring"))
|
||||
now = now.Add(time.Second)
|
||||
assert.False(t, first.IsExist("expiring"))
|
||||
_, ok = first.Get("expiring")
|
||||
assert.False(t, ok)
|
||||
require.NoError(t, first.Put("expiring", "renewed", 0))
|
||||
value, ok = first.Get("expiring")
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "renewed", value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNegativeItemTTLDisablesOnlyItemTTLCaching(t *testing.T) {
|
||||
createTestCache()
|
||||
defer test.MockVariableValue(&setting.CacheService.TTL, -1)()
|
||||
|
||||
require.NoError(t, defaultCache.Put("key", "stale", 0))
|
||||
require.NoError(t, defaultCache.Put("key", "value", setting.CacheService.TTLSeconds()))
|
||||
assert.False(t, defaultCache.IsExist("key"))
|
||||
|
||||
calls := 0
|
||||
for range 2 {
|
||||
data, err := GetString("key", func() (string, error) {
|
||||
calls++
|
||||
return "value", nil
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "value", data)
|
||||
}
|
||||
assert.Equal(t, 2, calls)
|
||||
assert.False(t, defaultCache.IsExist("key"))
|
||||
|
||||
require.NoError(t, defaultCache.Put("captcha", "value", 600))
|
||||
assert.True(t, defaultCache.IsExist("captcha"))
|
||||
}
|
||||
|
||||
func TestMemcacheExpiration(t *testing.T) {
|
||||
defer test.MockVariableValue(&timeNow, func() time.Time { return time.Unix(1000, 0) })()
|
||||
assert.EqualValues(t, memcacheMaxRelativeTTL, memcacheExpiration(memcacheMaxRelativeTTL))
|
||||
assert.EqualValues(t, 1000+memcacheMaxRelativeTTL+1, memcacheExpiration(memcacheMaxRelativeTTL+1))
|
||||
}
|
||||
|
||||
func TestTest(t *testing.T) {
|
||||
|
||||
Vendored
+89
-171
@@ -4,205 +4,123 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.dev/modules/json"
|
||||
|
||||
mc "gitea.com/go-chi/cache" //nolint:depguard // we wrap this package here
|
||||
lru "github.com/hashicorp/golang-lru/v2"
|
||||
)
|
||||
|
||||
// TwoQueueCache represents a LRU 2Q cache adapter implementation
|
||||
type TwoQueueCache struct {
|
||||
lock sync.Mutex
|
||||
cache *lru.TwoQueueCache[string, any]
|
||||
interval int
|
||||
const twoQueueDefaultSize = 50000
|
||||
|
||||
type twoQueueCache struct {
|
||||
cache *lru.TwoQueueCache[string, memoryItem] // wrap the existing thread-safe 2Q (two-queue) cache directly
|
||||
}
|
||||
|
||||
// TwoQueueCacheConfig describes the configuration for TwoQueueCache
|
||||
type TwoQueueCacheConfig struct {
|
||||
Size int `ini:"SIZE" json:"size"`
|
||||
RecentRatio float64 `ini:"RECENT_RATIO" json:"recent_ratio"`
|
||||
GhostRatio float64 `ini:"GHOST_RATIO" json:"ghost_ratio"`
|
||||
type twoQueueCacheConfig struct {
|
||||
Size int `json:"size"`
|
||||
RecentRatio float64 `json:"recent_ratio"`
|
||||
GhostRatio float64 `json:"ghost_ratio"`
|
||||
}
|
||||
|
||||
// MemoryItem represents a memory cache item.
|
||||
type MemoryItem struct {
|
||||
Val any
|
||||
Created int64
|
||||
Timeout int64
|
||||
}
|
||||
|
||||
func (item *MemoryItem) hasExpired() bool {
|
||||
return item.Timeout > 0 &&
|
||||
(time.Now().Unix()-item.Created) >= item.Timeout
|
||||
}
|
||||
|
||||
var _ mc.Cache = &TwoQueueCache{}
|
||||
|
||||
// Put puts value into cache with key and expire time.
|
||||
func (c *TwoQueueCache) Put(key string, val any, timeout int64) error {
|
||||
item := &MemoryItem{
|
||||
Val: val,
|
||||
Created: time.Now().Unix(),
|
||||
Timeout: timeout,
|
||||
func newTwoQueueCache(conn string, gcInterval time.Duration) (backend, error) {
|
||||
lruCache, err := newTwoQueueLRU(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
cache := &twoQueueCache{cache: lruCache}
|
||||
startGC(gcInterval, cache.deleteExpired)
|
||||
return cache, nil
|
||||
}
|
||||
|
||||
func newTwoQueueLRU(conn string) (*lru.TwoQueueCache[string, memoryItem], error) {
|
||||
if conn == "" {
|
||||
return lru.New2Q[string, memoryItem](twoQueueDefaultSize)
|
||||
}
|
||||
if size, err := strconv.Atoi(conn); err == nil {
|
||||
return lru.New2Q[string, memoryItem](size)
|
||||
}
|
||||
if !json.Valid([]byte(conn)) {
|
||||
return nil, fmt.Errorf("invalid two-queue cache HOST %q, expected a size or a JSON config", conn)
|
||||
}
|
||||
config := twoQueueCacheConfig{
|
||||
Size: twoQueueDefaultSize,
|
||||
RecentRatio: lru.Default2QRecentRatio,
|
||||
GhostRatio: lru.Default2QGhostEntries,
|
||||
}
|
||||
_ = json.Unmarshal([]byte(conn), &config)
|
||||
return lru.New2QParams[string, memoryItem](config.Size, config.RecentRatio, config.GhostRatio)
|
||||
}
|
||||
|
||||
func (c *twoQueueCache) Get(key string) (string, bool) {
|
||||
item, ok := c.cache.Get(key)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
if item.expired(timeNow()) {
|
||||
c.cache.Remove(key)
|
||||
return "", false
|
||||
}
|
||||
return item.value, true
|
||||
}
|
||||
|
||||
func (c *twoQueueCache) Put(key, value string, ttl int64) error {
|
||||
item := newMemoryItem(value, ttl)
|
||||
c.cache.Add(key, item)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get gets cached value by given key.
|
||||
func (c *TwoQueueCache) Get(key string) any {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
cached, ok := c.cache.Get(key)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
item, ok := cached.(*MemoryItem)
|
||||
|
||||
if !ok || item.hasExpired() {
|
||||
c.cache.Remove(key)
|
||||
return nil
|
||||
}
|
||||
|
||||
return item.Val
|
||||
}
|
||||
|
||||
// Delete deletes cached value by given key.
|
||||
func (c *TwoQueueCache) Delete(key string) error {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
func (c *twoQueueCache) Delete(key string) error {
|
||||
c.cache.Remove(key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Incr increases cached int-type value by given key as a counter.
|
||||
func (c *TwoQueueCache) Incr(key string) error {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
cached, ok := c.cache.Get(key)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
item, ok := cached.(*MemoryItem)
|
||||
|
||||
if !ok || item.hasExpired() {
|
||||
c.cache.Remove(key)
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
item.Val, err = mc.Incr(item.Val)
|
||||
return err
|
||||
func (c *twoQueueCache) IsExist(key string) bool {
|
||||
item, ok := c.cache.Peek(key)
|
||||
return ok && !item.expired(timeNow())
|
||||
}
|
||||
|
||||
// Decr decreases cached int-type value by given key as a counter.
|
||||
func (c *TwoQueueCache) Decr(key string) error {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
cached, ok := c.cache.Get(key)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
item, ok := cached.(*MemoryItem)
|
||||
|
||||
if !ok || item.hasExpired() {
|
||||
c.cache.Remove(key)
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
item.Val, err = mc.Decr(item.Val)
|
||||
return err
|
||||
}
|
||||
|
||||
// IsExist returns true if cached value exists.
|
||||
func (c *TwoQueueCache) IsExist(key string) bool {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
cached, ok := c.cache.Peek(key)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
item, ok := cached.(*MemoryItem)
|
||||
if !ok || item.hasExpired() {
|
||||
c.cache.Remove(key)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Flush deletes all cached data.
|
||||
func (c *TwoQueueCache) Flush() error {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.cache.Purge()
|
||||
func (c *twoQueueCache) Ping() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TwoQueueCache) checkAndInvalidate(key string) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
cached, ok := c.cache.Peek(key)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
item, ok := cached.(*MemoryItem)
|
||||
if !ok || item.hasExpired() {
|
||||
c.cache.Remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TwoQueueCache) startGC() {
|
||||
if c.interval < 0 {
|
||||
return
|
||||
}
|
||||
func (c *twoQueueCache) deleteExpired() {
|
||||
now := timeNow()
|
||||
for _, key := range c.cache.Keys() {
|
||||
c.checkAndInvalidate(key)
|
||||
}
|
||||
time.AfterFunc(time.Duration(c.interval)*time.Second, c.startGC)
|
||||
}
|
||||
|
||||
// StartAndGC starts GC routine based on config string settings.
|
||||
func (c *TwoQueueCache) StartAndGC(opts mc.Options) error {
|
||||
var err error
|
||||
size := 50000
|
||||
if opts.AdapterConfig != "" {
|
||||
size, err = strconv.Atoi(opts.AdapterConfig)
|
||||
}
|
||||
if err != nil {
|
||||
if !json.Valid([]byte(opts.AdapterConfig)) {
|
||||
return err
|
||||
if item, ok := c.cache.Peek(key); ok && item.expired(now) {
|
||||
// here might be a slight data-race: the item might have been removed or updated by another goroutine between the Peek and Remove calls,
|
||||
// but it's acceptable since the cache is not guaranteed to be 100% accurate and any item can be evicted at any time
|
||||
c.cache.Remove(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cfg := &TwoQueueCacheConfig{
|
||||
Size: 50000,
|
||||
RecentRatio: lru.Default2QRecentRatio,
|
||||
GhostRatio: lru.Default2QGhostEntries,
|
||||
type memoryItem struct {
|
||||
value string
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
func newMemoryItem(value string, ttl int64) memoryItem {
|
||||
item := memoryItem{value: value}
|
||||
if ttl > 0 {
|
||||
item.expiresAt = timeNow().Add(time.Duration(ttl) * time.Second)
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
func (item memoryItem) expired(now time.Time) bool {
|
||||
return !item.expiresAt.IsZero() && !now.Before(item.expiresAt)
|
||||
}
|
||||
|
||||
func startGC(interval time.Duration, deleteExpired func()) {
|
||||
if interval <= 0 {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
for range time.Tick(interval) {
|
||||
deleteExpired()
|
||||
}
|
||||
_ = json.Unmarshal([]byte(opts.AdapterConfig), cfg)
|
||||
c.cache, err = lru.New2QParams[string, any](cfg.Size, cfg.RecentRatio, cfg.GhostRatio)
|
||||
} else {
|
||||
c.cache, err = lru.New2Q[string, any](size)
|
||||
}
|
||||
c.interval = opts.Interval
|
||||
if c.interval > 0 {
|
||||
go c.startGC()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Ping tests if the cache is alive.
|
||||
func (c *TwoQueueCache) Ping() error {
|
||||
return mc.GenericPing(c)
|
||||
}
|
||||
|
||||
func init() {
|
||||
mc.Register("twoqueue", &TwoQueueCache{})
|
||||
}()
|
||||
}
|
||||
|
||||
Vendored
+34
-37
@@ -5,13 +5,14 @@ package cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.dev/modules/json"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/util"
|
||||
|
||||
chi_cache "gitea.com/go-chi/cache" //nolint:depguard // we wrap this package here
|
||||
)
|
||||
|
||||
type GetJSONError struct {
|
||||
@@ -30,57 +31,57 @@ type StringCache interface {
|
||||
Ping() error
|
||||
|
||||
Get(key string) (string, bool)
|
||||
Put(key, value string, ttl int64) error
|
||||
Put(key, value string, ttl int64) error // ttl in seconds, 0 never expires, negative removes the key
|
||||
Delete(key string) error
|
||||
IsExist(key string) bool
|
||||
|
||||
PutJSON(key string, v any, ttl int64) error
|
||||
GetJSON(key string, ptr any) (exist bool, err *GetJSONError)
|
||||
}
|
||||
|
||||
ChiCache() chi_cache.Cache
|
||||
type backend interface {
|
||||
Get(key string) (string, bool)
|
||||
Put(key, value string, ttl int64) error
|
||||
Delete(key string) error
|
||||
IsExist(key string) bool
|
||||
Ping() error
|
||||
}
|
||||
|
||||
type stringCache struct {
|
||||
chiCache chi_cache.Cache
|
||||
backend
|
||||
}
|
||||
|
||||
func NewStringCache(cacheConfig setting.Cache) (StringCache, error) {
|
||||
adapter := util.IfZero(cacheConfig.Adapter, "memory")
|
||||
interval := util.IfZero(cacheConfig.Interval, 60)
|
||||
cc, err := chi_cache.NewCacher(chi_cache.Options{
|
||||
Adapter: adapter,
|
||||
AdapterConfig: cacheConfig.Conn,
|
||||
Interval: interval,
|
||||
})
|
||||
cacheBackend, err := newBackend(cacheConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &stringCache{chiCache: cc}, nil
|
||||
return &stringCache{backend: cacheBackend}, nil
|
||||
}
|
||||
|
||||
func (sc *stringCache) Ping() error {
|
||||
return sc.chiCache.Ping()
|
||||
}
|
||||
|
||||
func (sc *stringCache) Get(key string) (string, bool) {
|
||||
v := sc.chiCache.Get(key)
|
||||
if v == nil {
|
||||
return "", false
|
||||
func newBackend(cacheConfig setting.Cache) (backend, error) {
|
||||
gcInterval := time.Duration(util.IfZero(cacheConfig.Interval, 60)) * time.Second
|
||||
switch adapter := util.IfZero(cacheConfig.Adapter, "memory"); adapter {
|
||||
case "memory":
|
||||
// the old "memory" adapter doesn't have a limit, which can lead to OOM
|
||||
// now, use two-queue cache for in-memory cache with items limit, at most a few GB of memory will be used
|
||||
return newTwoQueueCache(strconv.FormatInt(10*1024*1024, 10), gcInterval)
|
||||
case "twoqueue":
|
||||
return newTwoQueueCache(cacheConfig.Conn, gcInterval)
|
||||
case "redis":
|
||||
return newRedisCache(cacheConfig.Conn), nil
|
||||
case "memcache":
|
||||
return newMemcacheCache(cacheConfig.Conn)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown cache adapter %q", adapter)
|
||||
}
|
||||
s, ok := v.(string)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
func (sc *stringCache) Put(key, value string, ttl int64) error {
|
||||
return sc.chiCache.Put(key, value, ttl)
|
||||
}
|
||||
|
||||
func (sc *stringCache) Delete(key string) error {
|
||||
return sc.chiCache.Delete(key)
|
||||
}
|
||||
|
||||
func (sc *stringCache) IsExist(key string) bool {
|
||||
return sc.chiCache.IsExist(key)
|
||||
if ttl < 0 {
|
||||
return sc.backend.Delete(key)
|
||||
}
|
||||
return sc.backend.Put(key, value, ttl)
|
||||
}
|
||||
|
||||
const cachedErrorPrefix = "<CACHED-ERROR>:"
|
||||
@@ -97,7 +98,7 @@ func (sc *stringCache) PutJSON(key string, v any, ttl int64) error {
|
||||
}
|
||||
s = util.UnsafeBytesToString(b)
|
||||
}
|
||||
return sc.chiCache.Put(key, s, ttl)
|
||||
return sc.Put(key, s, ttl)
|
||||
}
|
||||
|
||||
func (sc *stringCache) GetJSON(key string, ptr any) (exist bool, getErr *GetJSONError) {
|
||||
@@ -114,7 +115,3 @@ func (sc *stringCache) GetJSON(key string, ptr any) (exist bool, getErr *GetJSON
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (sc *stringCache) ChiCache() chi_cache.Cache {
|
||||
return sc.chiCache
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user