Skip to content

Commit 97bfeb2

Browse files
authored
feat: add delete & exists for generic typed cache (#75)
1 parent 7631f05 commit 97bfeb2

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

cache_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ var _ = Describe("Cache", func() {
202202
Expect(n).To(Equal(int64(124)))
203203
})
204204

205-
Describe("Generic Set/Get/MGet func", func() {
205+
Describe("Generic Set/Get/MGet/Delete/Exists func", func() {
206206
It("cache hit with set first", func() {
207207
cacheT := NewT[int, *object](cache)
208208

@@ -373,6 +373,20 @@ var _ = Describe("Cache", func() {
373373
Expect(ret).To(Equal(map[int]*object{1: {Str: "str1", Num: 1}, 2: {Str: "str2", Num: 2}}))
374374
}
375375
})
376+
377+
It("delete key and not exists", func() {
378+
cacheT := NewT[int, *object](cache)
379+
380+
err := cacheT.Set(context.Background(), "key", 1, &object{Str: "str1", Num: 1})
381+
Expect(err).NotTo(HaveOccurred())
382+
Expect(cache.Exists(ctx, "key:1")).To(BeTrue())
383+
Expect(cacheT.Exists(ctx, "key", 1)).To(BeTrue())
384+
385+
err = cacheT.Delete(ctx, "key", 1)
386+
Expect(err).NotTo(HaveOccurred())
387+
Expect(cache.Exists(ctx, "key:1")).To(BeFalse())
388+
Expect(cacheT.Exists(ctx, "key", 1)).To(BeFalse())
389+
})
376390
})
377391

378392
Describe("Once func", func() {

cachegeneric.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ func NewT[K constraints.Ordered, V any](cache Cache) *T[K, V] {
2727
// The expiration time of the cached value is determined by the cache configuration.
2828
func (w *T[K, V]) Set(ctx context.Context, key string, id K, v V) error {
2929
c := w.Cache.(*jetCache)
30-
31-
combKey := fmt.Sprintf("%s%s%v", key, c.separator, id)
32-
return w.Cache.Set(ctx, combKey, Value(v))
30+
return w.Cache.Set(ctx, w.combKey(c, key, id), Value(v))
3331
}
3432

3533
// Get retrieves the value associated with the given `key` and `id`.
@@ -44,8 +42,7 @@ func (w *T[K, V]) Get(ctx context.Context, key string, id K, fn func(context.Con
4442
c := w.Cache.(*jetCache)
4543

4644
var varT V
47-
combKey := fmt.Sprintf("%s%s%v", key, c.separator, id)
48-
err := w.Once(ctx, combKey, Value(&varT), Do(func(ctx context.Context) (any, error) {
45+
err := w.Once(ctx, w.combKey(c, key, id), Value(&varT), Do(func(ctx context.Context) (any, error) {
4946
return fn(ctx, id)
5047
}))
5148

@@ -78,8 +75,7 @@ func (w *T[K, V]) MGetWithErr(ctx context.Context, key string, ids []K, fn func(
7875

7976
miss := make(map[string]K, len(ids))
8077
for _, missId := range ids {
81-
missKey := fmt.Sprintf("%s%s%v", key, c.separator, missId)
82-
miss[missKey] = missId
78+
miss[w.combKey(c, key, missId)] = missId
8379
}
8480

8581
if c.local != nil {
@@ -279,3 +275,19 @@ func (w *T[K, V]) mQueryAndSetCache(ctx context.Context, miss map[string]K, fn f
279275

280276
return
281277
}
278+
279+
// Delete deletes cached val with the given `key` and `id`.
280+
func (w *T[K, V]) Delete(ctx context.Context, key string, id K) error {
281+
c := w.Cache.(*jetCache)
282+
return c.Delete(ctx, w.combKey(c, key, id))
283+
}
284+
285+
// Exists reports whether val for the given `key` and `id` exists.
286+
func (w *T[K, V]) Exists(ctx context.Context, key string, id K) bool {
287+
c := w.Cache.(*jetCache)
288+
return c.Exists(ctx, w.combKey(c, key, id))
289+
}
290+
291+
func (w *T[K, V]) combKey(c *jetCache, key string, id K) string {
292+
return fmt.Sprintf("%s%s%v", key, c.separator, id)
293+
}

0 commit comments

Comments
 (0)