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

feat(cache): Add Cache-Control: no-cache support for Loki instant queries. #12896

Merged
merged 11 commits into from
May 19, 2024
Prev Previous commit
Next Next commit
PR remarks
Signed-off-by: Kaviraj <[email protected]>
  • Loading branch information
kavirajk committed May 16, 2024
commit ebdbc869a9437669f58bab9578a6b80d5d03d6e0
46 changes: 22 additions & 24 deletions pkg/storage/chunk/cache/resultscache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,6 @@ func Test_resultsCache_MissingData(t *testing.T) {
}

func Test_shouldCacheReq(t *testing.T) {
modelNow := model.Now()

cfg := Config{
CacheConfig: cache.Config{
Cache: cache.NewMockCache(),
Expand Down Expand Up @@ -715,45 +713,45 @@ func Test_shouldCacheReq(t *testing.T) {
ctx := user.InjectOrgID(context.Background(), "1")

// create request with start end within the key extents
req := parsedRequest.WithStartEndForCache(time.UnixMilli(int64(modelNow)-(50*1e3)), time.UnixMilli(int64(modelNow)-(10*1e3)))
req := parsedRequest.WithStartEndForCache(time.UnixMilli(50), time.UnixMilli(120))

// fill cache
key := ConstSplitter(day).GenerateCacheKey(context.Background(), "1", req)
rc.put(ctx, key, []Extent{mkExtent(int64(modelNow)-(600*1e3), int64(modelNow))})
rc.put(ctx, key, []Extent{mkExtent(50, 120)})

// Asserts (when `shouldCacheReq` is non-nil and set to return false (should not cache), resultcache should get result from upstream handler (mockHandler))
// 1. With `shouldCacheReq` non-nil and set `noCacheReq`, should get result from `next` handler
// 2. With `shouldCacheReq` non-nil and set `cacheReq`, should get result from cache
// 3. With `shouldCacheReq` nil, should get result from cache
// Asserts (when `shouldLookupCache` is non-nil and set to return false (should not cache), resultcache should get result from upstream handler (mockHandler))
// 1. With `shouldLookupCache` non-nil and set `noCacheReq`, should get result from `next` handler
// 2. With `shouldLookupCache` non-nil and set `cacheReq`, should get result from cache
// 3. With `shouldLookupCache` nil, should get result from cache

cases := []struct {
name string
shouldCache ShouldCacheReqFn
name string
shouldLookupCache ShouldCacheReqFn
// expected number of times, upstream `next` handler is called inside results cache
expCount int
}{
{
name: "noCacheReq",
shouldCache: noCacheReq,
expCount: 1,
name: "don't lookup cache",
shouldLookupCache: noLookupCache,
expCount: 1,
},
{
name: "cacheReq",
shouldCache: cacheReq,
expCount: 0,
name: "lookup cache",
shouldLookupCache: lookupCache,
expCount: 0,
},
{
name: "nil",
shouldCache: nil,
expCount: 0,
name: "nil",
shouldLookupCache: nil,
expCount: 0,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
mh := &mockHandler{}
rc.next = mh
rc.shouldCacheReq = tc.shouldCache
rc.shouldCacheReq = tc.shouldLookupCache

_, err = rc.Do(ctx, req)
require.NoError(t, err)
Expand All @@ -773,13 +771,13 @@ func (mh *mockHandler) Do(_ context.Context, _ Request) (Response, error) {
return mh.res, nil
}

// noCacheReq is of type `ShouldCacheReq` that always returns false (do not cache)
func noCacheReq(_ context.Context, _ Request) bool {
// noLookupCache is of type `ShouldCacheReq` that always returns false (do not cache)
func noLookupCache(_ context.Context, _ Request) bool {
return false
}

// cacheReq is of type `ShouldCacheReq` that always returns true (cache the result)
func cacheReq(_ context.Context, _ Request) bool {
// lookupCache is of type `ShouldCacheReq` that always returns true (cache the result)
func lookupCache(_ context.Context, _ Request) bool {
return true
}

Expand Down
Loading