Skip to content

Commit dc72bd7

Browse files
bryanchriswhitered-0ne
authored andcommitted
[Code Health] fix: support zero kvcache TTL (#1108)
## Summary Fix the usage of `isTTLEnabled` to properly support zero TTL (mainly for testing). ## Issue - Issue: #1034 ## Type of change Select one or more from the following: - [ ] New feature, functionality or library - [ ] Consensus breaking; add the `consensus-breaking` label if so. See #791 for details - [ ] Bug fix - [x] Code health or cleanup - [ ] Documentation - [ ] Other (specify) ## Sanity Checklist - [x] I have updated the GitHub Issue `assignees`, `reviewers`, `labels`, `project`, `iteration` and `milestone` - [ ] For docs, I have run `make docusaurus_start` - [x] For code, I have run `make go_develop_and_test` and `make test_e2e` - [ ] For code, I have added the `devnet-test-e2e` label to run E2E tests in CI - [ ] For configurations, I have update the documentation - [ ] I added TODOs where applicable
1 parent f5b39ed commit dc72bd7

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

pkg/cache/memory/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ func WithTTL(ttl time.Duration) KeyValueCacheOptionFn {
120120
}
121121
}
122122

123+
// WithNoTTL effectively disables the cache. Useful for testing.
124+
func WithNoTTL() KeyValueCacheOptionFn {
125+
return func(cfg keyValueConfigI) error {
126+
cfg.SetTTL(0)
127+
return nil
128+
}
129+
}
130+
123131
// WithMaxVersionAge sets the given maxVersionAge on the configuration; if 0, no historical pruning is performed.
124132
// It can ONLY be used in the context of a HistoricalKeyValueCache.
125133
func WithMaxVersionAge(numRetainedVersions int64) KeyValueCacheOptionFn {

pkg/cache/memory/kvcache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *keyValueCache[T]) Get(key string) (T, bool) {
6161

6262
isTTLEnabled := c.config.ttl > 0
6363
isCacheValueExpired := time.Since(cachedValue.cachedAt) > c.config.ttl
64-
if isTTLEnabled && isCacheValueExpired {
64+
if !isTTLEnabled || isCacheValueExpired {
6565
// DEV_NOTE: Not pruning here to optimize concurrent speed:
6666
// - Read lock alone would be insufficient for pruning
6767
// - Next Set() call will overwrite the value

pkg/cache/memory/kvcache_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ func TestMemoryKeyValueCache(t *testing.T) {
8585
require.True(t, isCached)
8686
require.Equal(t, "value3", val)
8787
})
88+
89+
t.Run("no TTL", func(t *testing.T) {
90+
cache, err := NewKeyValueCache[string](WithNoTTL())
91+
require.NoError(t, err)
92+
93+
// Immediately get the same key after setting.
94+
cache.Set("key1", "value1")
95+
_, wasCached := cache.Get("key1")
96+
require.False(t, wasCached)
97+
})
8898
}
8999

90100
// TestKeyValueCache_ErrorCases exercises various error conditions

pkg/client/query/querycache_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package query_test
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
"cosmossdk.io/depinject"
89
coretypes "github.com/cometbft/cometbft/rpc/core/types"
@@ -478,7 +479,7 @@ type queryClients struct {
478479

479480
// supplyCacheDeps supplies all the cache dependencies required by the query clients.
480481
func supplyCacheDeps(t *testing.T) depinject.Config {
481-
opts := memory.WithTTL(0)
482+
opts := memory.WithTTL(time.Minute)
482483

483484
serviceCache, err := memory.NewKeyValueCache[sharedtypes.Service](opts)
484485
require.NoError(t, err)

0 commit comments

Comments
 (0)