Skip to content

Commit 5d11298

Browse files
committed
feat: add SetMemCacheMaxTTLSeconds to allow longer memcache
1 parent 09eb5f1 commit 5d11298

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ linters:
1919
enable:
2020
- asciicheck
2121
- bodyclose
22-
- depguard
2322
- dogsled
2423
# next-time
2524
# - dupl

cache.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const (
4040
)
4141

4242
// Hardcap for memory cache TTL, changeable for testing.
43-
var memCacheMaxTTLSeconds int64 = 5
43+
var defaultMemCacheMaxTTLSeconds int64 = 5
44+
var memCacheMaxTTLHardCapSeconds int64 = 120
4445

4546
// compression constants
4647
const (
@@ -90,15 +91,16 @@ type DCache struct {
9091
tracer *tracer
9192

9293
// In memory cache related
93-
inMemCache *freecache.Cache
94-
pubsub *redis.PubSub
95-
id string
96-
invalidateKeys map[string]struct{}
97-
invalidateMu *sync.Mutex
98-
invalidateCh chan struct{}
99-
ctx context.Context
100-
cancel context.CancelFunc
101-
wg sync.WaitGroup
94+
inMemCache *freecache.Cache
95+
memCacheMaxTTLSeconds int64
96+
pubsub *redis.PubSub
97+
id string
98+
invalidateKeys map[string]struct{}
99+
invalidateMu *sync.Mutex
100+
invalidateCh chan struct{}
101+
ctx context.Context
102+
cancel context.CancelFunc
103+
wg sync.WaitGroup
102104
}
103105

104106
// NewDCache creates a new cache client with in-memory cache if not @p inMemCache not nil.
@@ -131,17 +133,18 @@ func NewDCache(
131133

132134
ctx, cancel := context.WithCancel(context.Background())
133135
c := &DCache{
134-
conn: primaryClient,
135-
stats: stats,
136-
tracer: tracer,
137-
id: uuid.NewV4().String(),
138-
invalidateKeys: make(map[string]struct{}),
139-
invalidateMu: &sync.Mutex{},
140-
invalidateCh: make(chan struct{}, invalidateChSize),
141-
inMemCache: inMemCache,
142-
readInterval: readInterval,
143-
ctx: ctx,
144-
cancel: cancel,
136+
conn: primaryClient,
137+
stats: stats,
138+
tracer: tracer,
139+
id: uuid.NewV4().String(),
140+
invalidateKeys: make(map[string]struct{}),
141+
invalidateMu: &sync.Mutex{},
142+
invalidateCh: make(chan struct{}, invalidateChSize),
143+
inMemCache: inMemCache,
144+
memCacheMaxTTLSeconds: defaultMemCacheMaxTTLSeconds,
145+
readInterval: readInterval,
146+
ctx: ctx,
147+
cancel: cancel,
145148
}
146149
if inMemCache != nil {
147150
c.pubsub = c.conn.Subscribe(ctx, redisCacheInvalidateTopic)
@@ -182,6 +185,15 @@ func (c *DCache) Close() {
182185
}
183186
}
184187

188+
func (c *DCache) SetMemCacheMaxTTLSeconds(ttl int64) error {
189+
if (ttl <= 0) || (ttl > memCacheMaxTTLHardCapSeconds) {
190+
return fmt.Errorf(
191+
"invalid ttl: %d, should be in range (0, %d]", ttl, memCacheMaxTTLHardCapSeconds)
192+
}
193+
c.memCacheMaxTTLSeconds = ttl
194+
return nil
195+
}
196+
185197
func (c *DCache) makeHitRecorder(label metricHitLabel, startedAt time.Time) func() {
186198
if c.stats != nil {
187199
return c.stats.MakeHitObserver(label, startedAt)
@@ -277,8 +289,8 @@ func (c *DCache) updateMemoryCache(
277289
// update memory cache.
278290
// sub-second TTL will be ignored for memory cache.
279291
ttl := time.UnixMilli(ve.ExpiredAt).Unix() - getNow().Unix()
280-
if ttl > memCacheMaxTTLSeconds {
281-
ttl = memCacheMaxTTLSeconds
292+
if ttl > c.memCacheMaxTTLSeconds {
293+
ttl = c.memCacheMaxTTLSeconds
282294
}
283295
if c.inMemCache != nil && ttl > 0 {
284296
memValue, err := c.inMemCache.Get([]byte(storeKey(key)))

cache_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,3 +783,9 @@ func (suite *testSuite) TestInvalidateKeyAcrossPods() {
783783
_, e = suite.inMemCache2.Get([]byte(storeKey(queryKey)))
784784
suite.Equal(freecache.ErrNotFound, e)
785785
}
786+
787+
func (suite *testSuite) TestSetMemCacheMaxTTlSeconds() {
788+
suite.Require().Nil(suite.cacheRepo.SetMemCacheMaxTTLSeconds(1))
789+
suite.Require().Error(suite.cacheRepo.SetMemCacheMaxTTLSeconds(0))
790+
suite.Require().Error(suite.cacheRepo.SetMemCacheMaxTTLSeconds(1000000))
791+
}

0 commit comments

Comments
 (0)