aboutsummaryrefslogtreecommitdiffstats
path: root/modules/cache
diff options
context:
space:
mode:
Diffstat (limited to 'modules/cache')
-rw-r--r--modules/cache/cache.go12
-rw-r--r--modules/cache/cache_redis.go2
-rw-r--r--modules/cache/cache_test.go16
-rw-r--r--modules/cache/cache_twoqueue.go2
-rw-r--r--modules/cache/context.go179
-rw-r--r--modules/cache/context_test.go65
-rw-r--r--modules/cache/ephemeral.go90
-rw-r--r--modules/cache/string_cache.go2
8 files changed, 148 insertions, 220 deletions
diff --git a/modules/cache/cache.go b/modules/cache/cache.go
index f7828e3cae..039caa9fbc 100644
--- a/modules/cache/cache.go
+++ b/modules/cache/cache.go
@@ -4,6 +4,8 @@
package cache
import (
+ "encoding/hex"
+ "errors"
"fmt"
"strconv"
"time"
@@ -22,7 +24,7 @@ func Init() error {
if err != nil {
return err
}
- for i := 0; i < 10; i++ {
+ for range 10 {
if err = c.Ping(); err == nil {
break
}
@@ -48,10 +50,10 @@ const (
// returns
func Test() (time.Duration, error) {
if defaultCache == nil {
- return 0, fmt.Errorf("default cache not initialized")
+ return 0, errors.New("default cache not initialized")
}
- testData := fmt.Sprintf("%x", make([]byte, 500))
+ testData := hex.EncodeToString(make([]byte, 500))
start := time.Now()
@@ -63,10 +65,10 @@ func Test() (time.Duration, error) {
}
testVal, hit := defaultCache.Get(testCacheKey)
if !hit {
- return 0, fmt.Errorf("expect cache hit but got none")
+ return 0, errors.New("expect cache hit but got none")
}
if testVal != testData {
- return 0, fmt.Errorf("expect cache to return same value as stored but got other")
+ return 0, errors.New("expect cache to return same value as stored but got other")
}
return time.Since(start), nil
diff --git a/modules/cache/cache_redis.go b/modules/cache/cache_redis.go
index c5b52a2086..7473c938af 100644
--- a/modules/cache/cache_redis.go
+++ b/modules/cache/cache_redis.go
@@ -11,7 +11,7 @@ import (
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/nosql"
- "gitea.com/go-chi/cache" //nolint:depguard
+ "gitea.com/go-chi/cache" //nolint:depguard // we wrap this package here
"github.com/redis/go-redis/v9"
)
diff --git a/modules/cache/cache_test.go b/modules/cache/cache_test.go
index 5408020306..d6ea2032ee 100644
--- a/modules/cache/cache_test.go
+++ b/modules/cache/cache_test.go
@@ -4,7 +4,7 @@
package cache
import (
- "fmt"
+ "errors"
"testing"
"time"
@@ -57,22 +57,22 @@ func TestGetString(t *testing.T) {
createTestCache()
data, err := GetString("key", func() (string, error) {
- return "", fmt.Errorf("some error")
+ return "", errors.New("some error")
})
assert.Error(t, err)
- assert.Equal(t, "", data)
+ assert.Empty(t, data)
data, err = GetString("key", func() (string, error) {
return "", nil
})
assert.NoError(t, err)
- assert.Equal(t, "", data)
+ assert.Empty(t, data)
data, err = GetString("key", func() (string, error) {
return "some data", nil
})
assert.NoError(t, err)
- assert.Equal(t, "", data)
+ assert.Empty(t, data)
Remove("key")
data, err = GetString("key", func() (string, error) {
@@ -82,7 +82,7 @@ func TestGetString(t *testing.T) {
assert.Equal(t, "some data", data)
data, err = GetString("key", func() (string, error) {
- return "", fmt.Errorf("some error")
+ return "", errors.New("some error")
})
assert.NoError(t, err)
assert.Equal(t, "some data", data)
@@ -93,7 +93,7 @@ func TestGetInt64(t *testing.T) {
createTestCache()
data, err := GetInt64("key", func() (int64, error) {
- return 0, fmt.Errorf("some error")
+ return 0, errors.New("some error")
})
assert.Error(t, err)
assert.EqualValues(t, 0, data)
@@ -118,7 +118,7 @@ func TestGetInt64(t *testing.T) {
assert.EqualValues(t, 100, data)
data, err = GetInt64("key", func() (int64, error) {
- return 0, fmt.Errorf("some error")
+ return 0, errors.New("some error")
})
assert.NoError(t, err)
assert.EqualValues(t, 100, data)
diff --git a/modules/cache/cache_twoqueue.go b/modules/cache/cache_twoqueue.go
index 1eda2debc4..c8db686e57 100644
--- a/modules/cache/cache_twoqueue.go
+++ b/modules/cache/cache_twoqueue.go
@@ -10,7 +10,7 @@ import (
"code.gitea.io/gitea/modules/json"
- mc "gitea.com/go-chi/cache" //nolint:depguard
+ mc "gitea.com/go-chi/cache" //nolint:depguard // we wrap this package here
lru "github.com/hashicorp/golang-lru/v2"
)
diff --git a/modules/cache/context.go b/modules/cache/context.go
index 484cee659a..23f7c23a52 100644
--- a/modules/cache/context.go
+++ b/modules/cache/context.go
@@ -5,176 +5,39 @@ package cache
import (
"context"
- "sync"
"time"
-
- "code.gitea.io/gitea/modules/log"
)
-// cacheContext is a context that can be used to cache data in a request level context
-// This is useful for caching data that is expensive to calculate and is likely to be
-// used multiple times in a request.
-type cacheContext struct {
- data map[any]map[any]any
- lock sync.RWMutex
- created time.Time
- discard bool
-}
-
-func (cc *cacheContext) Get(tp, key any) any {
- cc.lock.RLock()
- defer cc.lock.RUnlock()
- return cc.data[tp][key]
-}
-
-func (cc *cacheContext) Put(tp, key, value any) {
- cc.lock.Lock()
- defer cc.lock.Unlock()
-
- if cc.discard {
- return
- }
-
- d := cc.data[tp]
- if d == nil {
- d = make(map[any]any)
- cc.data[tp] = d
- }
- d[key] = value
-}
-
-func (cc *cacheContext) Delete(tp, key any) {
- cc.lock.Lock()
- defer cc.lock.Unlock()
- delete(cc.data[tp], key)
-}
-
-func (cc *cacheContext) Discard() {
- cc.lock.Lock()
- defer cc.lock.Unlock()
- cc.data = nil
- cc.discard = true
-}
-
-func (cc *cacheContext) isDiscard() bool {
- cc.lock.RLock()
- defer cc.lock.RUnlock()
- return cc.discard
-}
-
-// cacheContextLifetime is the max lifetime of cacheContext.
-// Since cacheContext is used to cache data in a request level context, 5 minutes is enough.
-// If a cacheContext is used more than 5 minutes, it's probably misuse.
-const cacheContextLifetime = 5 * time.Minute
-
-var timeNow = time.Now
+type cacheContextKeyType struct{}
-func (cc *cacheContext) Expired() bool {
- return timeNow().Sub(cc.created) > cacheContextLifetime
-}
-
-var cacheContextKey = struct{}{}
-
-/*
-Since there are both WithCacheContext and WithNoCacheContext,
-it may be confusing when there is nesting.
-
-Some cases to explain the design:
-
-When:
-- A, B or C means a cache context.
-- A', B' or C' means a discard cache context.
-- ctx means context.Backgrand().
-- A(ctx) means a cache context with ctx as the parent context.
-- B(A(ctx)) means a cache context with A(ctx) as the parent context.
-- With is alias of WithCacheContext.
-- WithNo is alias of WithNoCacheContext.
+var cacheContextKey = cacheContextKeyType{}
-So:
-- With(ctx) -> A(ctx)
-- With(With(ctx)) -> A(ctx), not B(A(ctx)), always reuse parent cache context if possible.
-- With(With(With(ctx))) -> A(ctx), not C(B(A(ctx))), ditto.
-- WithNo(ctx) -> ctx, not A'(ctx), don't create new cache context if we don't have to.
-- WithNo(With(ctx)) -> A'(ctx)
-- WithNo(WithNo(With(ctx))) -> A'(ctx), not B'(A'(ctx)), don't create new cache context if we don't have to.
-- With(WithNo(With(ctx))) -> B(A'(ctx)), not A(ctx), never reuse a discard cache context.
-- WithNo(With(WithNo(With(ctx)))) -> B'(A'(ctx))
-- With(WithNo(With(WithNo(With(ctx))))) -> C(B'(A'(ctx))), so there's always only one not-discard cache context.
-*/
+// contextCacheLifetime is the max lifetime of context cache.
+// Since context cache is used to cache data in a request level context, 5 minutes is enough.
+// If a context cache is used more than 5 minutes, it's probably abused.
+const contextCacheLifetime = 5 * time.Minute
func WithCacheContext(ctx context.Context) context.Context {
- if c, ok := ctx.Value(cacheContextKey).(*cacheContext); ok {
- if !c.isDiscard() {
- // reuse parent context
- return ctx
- }
- }
- // FIXME: review the use of this nolint directive
- return context.WithValue(ctx, cacheContextKey, &cacheContext{ //nolint:staticcheck
- data: make(map[any]map[any]any),
- created: timeNow(),
- })
-}
-
-func WithNoCacheContext(ctx context.Context) context.Context {
- if c, ok := ctx.Value(cacheContextKey).(*cacheContext); ok {
- // The caller want to run long-life tasks, but the parent context is a cache context.
- // So we should disable and clean the cache data, or it will be kept in memory for a long time.
- c.Discard()
+ if c := GetContextCache(ctx); c != nil {
return ctx
}
-
- return ctx
-}
-
-func GetContextData(ctx context.Context, tp, key any) any {
- if c, ok := ctx.Value(cacheContextKey).(*cacheContext); ok {
- if c.Expired() {
- // The warning means that the cache context is misused for long-life task,
- // it can be resolved with WithNoCacheContext(ctx).
- log.Warn("cache context is expired, is highly likely to be misused for long-life tasks: %v", c)
- return nil
- }
- return c.Get(tp, key)
- }
- return nil
+ return context.WithValue(ctx, cacheContextKey, NewEphemeralCache(contextCacheLifetime))
}
-func SetContextData(ctx context.Context, tp, key, value any) {
- if c, ok := ctx.Value(cacheContextKey).(*cacheContext); ok {
- if c.Expired() {
- // The warning means that the cache context is misused for long-life task,
- // it can be resolved with WithNoCacheContext(ctx).
- log.Warn("cache context is expired, is highly likely to be misused for long-life tasks: %v", c)
- return
- }
- c.Put(tp, key, value)
- return
- }
-}
-
-func RemoveContextData(ctx context.Context, tp, key any) {
- if c, ok := ctx.Value(cacheContextKey).(*cacheContext); ok {
- if c.Expired() {
- // The warning means that the cache context is misused for long-life task,
- // it can be resolved with WithNoCacheContext(ctx).
- log.Warn("cache context is expired, is highly likely to be misused for long-life tasks: %v", c)
- return
- }
- c.Delete(tp, key)
- }
+func GetContextCache(ctx context.Context) *EphemeralCache {
+ c, _ := ctx.Value(cacheContextKey).(*EphemeralCache)
+ return c
}
// GetWithContextCache returns the cache value of the given key in the given context.
-func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error) {
- v := GetContextData(ctx, cacheGroupKey, cacheTargetID)
- if vv, ok := v.(T); ok {
- return vv, nil
- }
- t, err := f()
- if err != nil {
- return t, err
- }
- SetContextData(ctx, cacheGroupKey, cacheTargetID, t)
- return t, nil
+// FIXME: in some cases, the "context cache" should not be used, because it has uncontrollable behaviors
+// For example, these calls:
+// * GetWithContextCache(TargetID) -> OtherCodeCreateModel(TargetID) -> GetWithContextCache(TargetID)
+// Will cause the second call is not able to get the correct created target.
+// UNLESS it is certain that the target won't be changed during the request, DO NOT use it.
+func GetWithContextCache[T, K any](ctx context.Context, groupKey string, targetKey K, f func(context.Context, K) (T, error)) (T, error) {
+ if c := GetContextCache(ctx); c != nil {
+ return GetWithEphemeralCache(ctx, c, groupKey, targetKey, f)
+ }
+ return f(ctx, targetKey)
}
diff --git a/modules/cache/context_test.go b/modules/cache/context_test.go
index cfc186a7bd..8371c2b908 100644
--- a/modules/cache/context_test.go
+++ b/modules/cache/context_test.go
@@ -4,74 +4,47 @@
package cache
import (
+ "context"
"testing"
"time"
+ "code.gitea.io/gitea/modules/test"
+
"github.com/stretchr/testify/assert"
)
func TestWithCacheContext(t *testing.T) {
ctx := WithCacheContext(t.Context())
-
- v := GetContextData(ctx, "empty_field", "my_config1")
+ c := GetContextCache(ctx)
+ v, _ := c.Get("empty_field", "my_config1")
assert.Nil(t, v)
const field = "system_setting"
- v = GetContextData(ctx, field, "my_config1")
+ v, _ = c.Get(field, "my_config1")
assert.Nil(t, v)
- SetContextData(ctx, field, "my_config1", 1)
- v = GetContextData(ctx, field, "my_config1")
+ c.Put(field, "my_config1", 1)
+ v, _ = c.Get(field, "my_config1")
assert.NotNil(t, v)
- assert.EqualValues(t, 1, v.(int))
+ assert.Equal(t, 1, v.(int))
- RemoveContextData(ctx, field, "my_config1")
- RemoveContextData(ctx, field, "my_config2") // remove a non-exist key
+ c.Delete(field, "my_config1")
+ c.Delete(field, "my_config2") // remove a non-exist key
- v = GetContextData(ctx, field, "my_config1")
+ v, _ = c.Get(field, "my_config1")
assert.Nil(t, v)
- vInt, err := GetWithContextCache(ctx, field, "my_config1", func() (int, error) {
+ vInt, err := GetWithContextCache(ctx, field, "my_config1", func(context.Context, string) (int, error) {
return 1, nil
})
assert.NoError(t, err)
- assert.EqualValues(t, 1, vInt)
+ assert.Equal(t, 1, vInt)
- v = GetContextData(ctx, field, "my_config1")
+ v, _ = c.Get(field, "my_config1")
assert.EqualValues(t, 1, v)
- now := timeNow
- defer func() {
- timeNow = now
- }()
- timeNow = func() time.Time {
- return now().Add(5 * time.Minute)
- }
- v = GetContextData(ctx, field, "my_config1")
- assert.Nil(t, v)
-}
-
-func TestWithNoCacheContext(t *testing.T) {
- ctx := t.Context()
-
- const field = "system_setting"
-
- v := GetContextData(ctx, field, "my_config1")
- assert.Nil(t, v)
- SetContextData(ctx, field, "my_config1", 1)
- v = GetContextData(ctx, field, "my_config1")
- assert.Nil(t, v) // still no cache
-
- ctx = WithCacheContext(ctx)
- v = GetContextData(ctx, field, "my_config1")
- assert.Nil(t, v)
- SetContextData(ctx, field, "my_config1", 1)
- v = GetContextData(ctx, field, "my_config1")
- assert.NotNil(t, v)
-
- ctx = WithNoCacheContext(ctx)
- v = GetContextData(ctx, field, "my_config1")
+ defer test.MockVariableValue(&timeNow, func() time.Time {
+ return time.Now().Add(5 * time.Minute)
+ })()
+ v, _ = c.Get(field, "my_config1")
assert.Nil(t, v)
- SetContextData(ctx, field, "my_config1", 1)
- v = GetContextData(ctx, field, "my_config1")
- assert.Nil(t, v) // still no cache
}
diff --git a/modules/cache/ephemeral.go b/modules/cache/ephemeral.go
new file mode 100644
index 0000000000..6996010ac4
--- /dev/null
+++ b/modules/cache/ephemeral.go
@@ -0,0 +1,90 @@
+// Copyright 2025 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package cache
+
+import (
+ "context"
+ "sync"
+ "time"
+
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/util"
+)
+
+// EphemeralCache is a cache that can be used to store data in a request level context
+// This is useful for caching data that is expensive to calculate and is likely to be
+// used multiple times in a request.
+type EphemeralCache struct {
+ data map[any]map[any]any
+ lock sync.RWMutex
+ created time.Time
+ checkLifeTime time.Duration
+}
+
+var timeNow = time.Now
+
+func NewEphemeralCache(checkLifeTime ...time.Duration) *EphemeralCache {
+ return &EphemeralCache{
+ data: make(map[any]map[any]any),
+ created: timeNow(),
+ checkLifeTime: util.OptionalArg(checkLifeTime, 0),
+ }
+}
+
+func (cc *EphemeralCache) checkExceededLifeTime(tp, key any) bool {
+ if cc.checkLifeTime > 0 && timeNow().Sub(cc.created) > cc.checkLifeTime {
+ log.Warn("EphemeralCache is expired, is highly likely to be abused for long-life tasks: %v, %v", tp, key)
+ return true
+ }
+ return false
+}
+
+func (cc *EphemeralCache) Get(tp, key any) (any, bool) {
+ if cc.checkExceededLifeTime(tp, key) {
+ return nil, false
+ }
+ cc.lock.RLock()
+ defer cc.lock.RUnlock()
+ ret, ok := cc.data[tp][key]
+ return ret, ok
+}
+
+func (cc *EphemeralCache) Put(tp, key, value any) {
+ if cc.checkExceededLifeTime(tp, key) {
+ return
+ }
+
+ cc.lock.Lock()
+ defer cc.lock.Unlock()
+
+ d := cc.data[tp]
+ if d == nil {
+ d = make(map[any]any)
+ cc.data[tp] = d
+ }
+ d[key] = value
+}
+
+func (cc *EphemeralCache) Delete(tp, key any) {
+ if cc.checkExceededLifeTime(tp, key) {
+ return
+ }
+
+ cc.lock.Lock()
+ defer cc.lock.Unlock()
+ delete(cc.data[tp], key)
+}
+
+func GetWithEphemeralCache[T, K any](ctx context.Context, c *EphemeralCache, groupKey string, targetKey K, f func(context.Context, K) (T, error)) (T, error) {
+ v, has := c.Get(groupKey, targetKey)
+ if vv, ok := v.(T); has && ok {
+ return vv, nil
+ }
+ t, err := f(ctx, targetKey)
+ if err != nil {
+ return t, err
+ }
+ c.Put(groupKey, targetKey, t)
+ return t, nil
+}
diff --git a/modules/cache/string_cache.go b/modules/cache/string_cache.go
index 4f659616f5..3562b7a926 100644
--- a/modules/cache/string_cache.go
+++ b/modules/cache/string_cache.go
@@ -11,7 +11,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
- chi_cache "gitea.com/go-chi/cache" //nolint:depguard
+ chi_cache "gitea.com/go-chi/cache" //nolint:depguard // we wrap this package here
)
type GetJSONError struct {