From 62adfef61287172c3024cfa5419214277bc9db73 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 6 Sep 2024 13:03:55 -0700 Subject: [PATCH 1/2] Remove expire from context cache --- modules/cache/context.go | 36 +----------------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/modules/cache/context.go b/modules/cache/context.go index 62bbf5dcba84a..d0b0e4bad15eb 100644 --- a/modules/cache/context.go +++ b/modules/cache/context.go @@ -6,9 +6,6 @@ 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 @@ -17,7 +14,6 @@ import ( type cacheContext struct { data map[any]map[any]any lock sync.RWMutex - created time.Time discard bool } @@ -62,17 +58,6 @@ func (cc *cacheContext) isDiscard() bool { return cc.discard } -// cacheContextLifetime is the max lifetime of cacheContext. -// Since cacheContext is used to cache data in a request level context, 10s is enough. -// If a cacheContext is used more than 10s, it's probably misuse. -const cacheContextLifetime = 10 * time.Second - -var timeNow = time.Now - -func (cc *cacheContext) Expired() bool { - return timeNow().Sub(cc.created) > cacheContextLifetime -} - var cacheContextKey = struct{}{} /* @@ -110,8 +95,7 @@ func WithCacheContext(ctx context.Context) context.Context { } } return context.WithValue(ctx, cacheContextKey, &cacheContext{ - data: make(map[any]map[any]any), - created: timeNow(), + data: make(map[any]map[any]any), }) } @@ -128,12 +112,6 @@ func WithNoCacheContext(ctx context.Context) context.Context { 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, may be misused for long-life tasks: %v", c) - return nil - } return c.Get(tp, key) } return nil @@ -141,12 +119,6 @@ func GetContextData(ctx context.Context, tp, key any) any { 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, may be misused for long-life tasks: %v", c) - return - } c.Put(tp, key, value) return } @@ -154,12 +126,6 @@ func SetContextData(ctx context.Context, tp, key, value any) { 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, may be misused for long-life tasks: %v", c) - return - } c.Delete(tp, key) } } From d6d9a7a88b0c7d605485ac4c1a48c067c7b888c7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 6 Sep 2024 14:53:29 -0700 Subject: [PATCH 2/2] Fix lint --- modules/cache/context_test.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/modules/cache/context_test.go b/modules/cache/context_test.go index 5315547865e19..bb72adb79dbff 100644 --- a/modules/cache/context_test.go +++ b/modules/cache/context_test.go @@ -6,7 +6,6 @@ package cache import ( "context" "testing" - "time" "github.com/stretchr/testify/assert" ) @@ -39,16 +38,6 @@ func TestWithCacheContext(t *testing.T) { v = GetContextData(ctx, field, "my_config1") assert.EqualValues(t, 1, v) - - now := timeNow - defer func() { - timeNow = now - }() - timeNow = func() time.Time { - return now().Add(10 * time.Second) - } - v = GetContextData(ctx, field, "my_config1") - assert.Nil(t, v) } func TestWithNoCacheContext(t *testing.T) {