Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove expire from context cache #32001

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 1 addition & 35 deletions modules/cache/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +14,6 @@ import (
type cacheContext struct {
data map[any]map[any]any
lock sync.RWMutex
created time.Time
discard bool
}

Expand Down Expand Up @@ -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{}{}

/*
Expand Down Expand Up @@ -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),
})
}

Expand All @@ -128,38 +112,20 @@ 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
}

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
}
}

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)
}
}
Expand Down
11 changes: 0 additions & 11 deletions modules/cache/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -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) {
Expand Down