Skip to content

Commit 1705743

Browse files
committed
Add data setter to cache package
1 parent 1576377 commit 1705743

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

cache/cache.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ type GetOpts[T any] struct {
4646
Generator func() (T, error)
4747
}
4848

49+
type SetOpts[T any] struct {
50+
Key string
51+
TTL int64
52+
Grace int64
53+
Data T
54+
}
55+
4956
var DefaultOpts = &Opts{
5057
DefaultTTL: time.Minute,
5158
DefaultGrace: 0,
@@ -265,3 +272,45 @@ func (c *Cache[T]) Get(key string, generator func() (T, error)) (T, error) {
265272
Generator: generator,
266273
})
267274
}
275+
276+
//
277+
// Cache setter with opts
278+
//
279+
280+
func (c *Cache[T]) SetWithOpts(opts *SetOpts[T]) {
281+
getOpts := &GetOpts[T]{
282+
Key: opts.Key,
283+
TTL: opts.TTL,
284+
Grace: opts.Grace,
285+
Generator: func() (T, error) {
286+
return opts.Data, nil
287+
},
288+
}
289+
290+
c.mu.RLock()
291+
item, exists := c.items[opts.Key]
292+
c.mu.RUnlock()
293+
294+
// Update data if container exists, otherwise create
295+
if exists {
296+
c.updateCacheItem(getOpts)
297+
} else {
298+
item = c.createCacheItem(getOpts)
299+
}
300+
301+
// Wait for data to be generated
302+
<-item.ready.signal
303+
}
304+
305+
//
306+
// Cache setter with default opts
307+
//
308+
309+
func (c *Cache[T]) Set(key string, data T) {
310+
c.SetWithOpts(&SetOpts[T]{
311+
Key: key,
312+
Data: data,
313+
TTL: c.defaultTTL,
314+
Grace: c.defaultGrace,
315+
})
316+
}

cache/cache_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,61 @@ func TestCacheGrace(t *testing.T) {
131131
assert.Equal(t, d1, d2)
132132
}
133133

134+
func TestCacheSet(t *testing.T) {
135+
cache := New[int64]()
136+
cache.Set("test", 42)
137+
138+
data, err := cache.Get("test", func() (int64, error) {
139+
return 123, nil
140+
})
141+
142+
assert.NoError(t, err)
143+
assert.Equal(t, int64(42), data)
144+
}
145+
146+
func TestCacheSetUpdate(t *testing.T) {
147+
cache := New[int64]()
148+
generator := func() (int64, error) {
149+
return cache.Get("test", func() (int64, error) {
150+
return 123, nil
151+
})
152+
}
153+
154+
d1, e1 := generator()
155+
assert.NoError(t, e1)
156+
assert.Equal(t, int64(123), d1)
157+
158+
cache.Set("test", 42)
159+
160+
d2, e2 := generator()
161+
assert.NoError(t, e2)
162+
assert.Equal(t, int64(42), d2)
163+
}
164+
165+
func TestCacheSetWithOpts(t *testing.T) {
166+
cache := New[int64]()
167+
generator := func() (int64, error) {
168+
return cache.Get("test", func() (int64, error) {
169+
return 123, nil
170+
})
171+
}
172+
173+
d1, e1 := generator()
174+
assert.NoError(t, e1)
175+
assert.Equal(t, int64(123), d1)
176+
177+
cache.SetWithOpts(&SetOpts[int64]{
178+
Key: "test",
179+
Data: 42,
180+
TTL: time.Minute.Nanoseconds(),
181+
Grace: time.Minute.Nanoseconds(),
182+
})
183+
184+
d2, e2 := generator()
185+
assert.NoError(t, e2)
186+
assert.Equal(t, int64(42), d2)
187+
}
188+
134189
func TestCachePurgeExpired(t *testing.T) {
135190
cache := NewWithOpts[int64](&Opts{
136191
DefaultTTL: 0,

0 commit comments

Comments
 (0)