mirror of
https://github.com/go-gitea/gitea.git
synced 2024-08-25 21:44:20 -04:00
Related to: #22294 #23186 #23054 Replace: #23218 Some discussion is in the comments of #23218. Highlights: - Add Expiration for cache context. If a cache context has been used for more than 10s, the cache data will be ignored, and warning logs will be printed. - Add `discard` field to `cacheContext`, a `cacheContext` with `discard` true will drop all cached data and won't store any new one. - Introduce `WithNoCacheContext`, if one wants to run long-life tasks, but the parent context is a cache context, `WithNoCacheContext(perentCtx)` will discard the cache data, so it will be safe to keep the context for a long time. - It will be fine to treat an original context as a cache context, like `GetContextData(context.Backgraud())`, no warning logs will be printed. Some cases about nesting: 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` means `WithCacheContext` - `WithNo` means `WithNoCacheContext` So: - `With(ctx)` -> *A(ctx)* - `With(With(ctx))` -> *A(ctx)*, not *B(A(ctx))* - `With(With(With(ctx)))` -> *A(ctx)*, not *C(B(A(ctx)))* - `WithNo(ctx)` -> *ctx*, not *~A~(ctx)* - `WithNo(With(ctx))` -> *~A~(ctx)* - `WithNo(WithNo(With(ctx)))` -> *~A~(ctx)*, not *~B~(~A~(ctx))* - `With(WithNo(With(ctx)))` -> *B(~A~(ctx))* - `WithNo(With(WithNo(With(ctx))))` -> *~B~(~A~(ctx))* - `With(WithNo(With(WithNo(With(ctx)))))` -> *C(~B~(~A~(ctx)))*
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestWithCacheContext(t *testing.T) {
|
|
ctx := WithCacheContext(context.Background())
|
|
|
|
v := GetContextData(ctx, "empty_field", "my_config1")
|
|
assert.Nil(t, v)
|
|
|
|
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.NotNil(t, v)
|
|
assert.EqualValues(t, 1, v.(int))
|
|
|
|
RemoveContextData(ctx, field, "my_config1")
|
|
RemoveContextData(ctx, field, "my_config2") // remove a non-exist key
|
|
|
|
v = GetContextData(ctx, field, "my_config1")
|
|
assert.Nil(t, v)
|
|
|
|
vInt, err := GetWithContextCache(ctx, field, "my_config1", func() (int, error) {
|
|
return 1, nil
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 1, vInt)
|
|
|
|
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) {
|
|
ctx := context.Background()
|
|
|
|
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")
|
|
assert.Nil(t, v)
|
|
SetContextData(ctx, field, "my_config1", 1)
|
|
v = GetContextData(ctx, field, "my_config1")
|
|
assert.Nil(t, v) // still no cache
|
|
}
|