@@ -40,7 +40,8 @@ const (
40
40
)
41
41
42
42
// Hardcap for memory cache TTL, changeable for testing.
43
- var memCacheMaxTTLSeconds int64 = 5
43
+ var defaultMemCacheMaxTTLSeconds int64 = 5
44
+ var memCacheMaxTTLHardCapSeconds int64 = 120
44
45
45
46
// compression constants
46
47
const (
@@ -90,15 +91,16 @@ type DCache struct {
90
91
tracer * tracer
91
92
92
93
// 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
102
104
}
103
105
104
106
// NewDCache creates a new cache client with in-memory cache if not @p inMemCache not nil.
@@ -131,17 +133,18 @@ func NewDCache(
131
133
132
134
ctx , cancel := context .WithCancel (context .Background ())
133
135
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 ,
145
148
}
146
149
if inMemCache != nil {
147
150
c .pubsub = c .conn .Subscribe (ctx , redisCacheInvalidateTopic )
@@ -182,6 +185,15 @@ func (c *DCache) Close() {
182
185
}
183
186
}
184
187
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
+
185
197
func (c * DCache ) makeHitRecorder (label metricHitLabel , startedAt time.Time ) func () {
186
198
if c .stats != nil {
187
199
return c .stats .MakeHitObserver (label , startedAt )
@@ -277,8 +289,8 @@ func (c *DCache) updateMemoryCache(
277
289
// update memory cache.
278
290
// sub-second TTL will be ignored for memory cache.
279
291
ttl := time .UnixMilli (ve .ExpiredAt ).Unix () - getNow ().Unix ()
280
- if ttl > memCacheMaxTTLSeconds {
281
- ttl = memCacheMaxTTLSeconds
292
+ if ttl > c . memCacheMaxTTLSeconds {
293
+ ttl = c . memCacheMaxTTLSeconds
282
294
}
283
295
if c .inMemCache != nil && ttl > 0 {
284
296
memValue , err := c .inMemCache .Get ([]byte (storeKey (key )))
0 commit comments