Skip to content

Commit d0f4239

Browse files
committed
Perf: Switch from expired to found for Get contract.
Signed-off-by: kash2104 <kparikh1104@gmail.com>
1 parent ac62542 commit d0f4239

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

‎cache/cache.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "time"
55
// Cache interface defines the methods for a cache implementation.
66
type Cache[K comparable] interface {
77
// Get retrieves a value from the cache by key.
8-
Get(key K) (value interface{}, expired bool)
8+
Get(key K) (value interface{}, found bool)
99

1010
// Put adds a value to the cache with the specified key and expiration time.
1111
Put(key K, value interface{}, expiration time.Duration)

‎cache/map.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (m *MemoryCacheStore[K]) run(ctx context.Context) {
6262
case <-ticker.C:
6363
m.store.Range(func(key, value interface{}) bool {
6464
if value.(*memoryCache).IsExpired() {
65-
m.store.Delete(key)
65+
m.store.CompareAndDelete(key, value)
6666
}
6767
return true
6868
})
@@ -71,12 +71,12 @@ func (m *MemoryCacheStore[K]) run(ctx context.Context) {
7171
}
7272

7373
// Get cache data from store, if cache data is expired, return nil
74-
func (m *MemoryCacheStore[K]) Get(key K) (value interface{}, expired bool) {
74+
func (m *MemoryCacheStore[K]) Get(key K) (value interface{}, found bool) {
7575
mc, ok := m.store.Load(key)
7676
if ok && !mc.(*memoryCache).IsExpired() {
77-
return mc.(*memoryCache).GetData(), false
77+
return mc.(*memoryCache).GetData(), true
7878
}
79-
return nil, true
79+
return nil, false
8080
}
8181

8282
// Put cache data, if cacheDuration>0, store will clear data after timeout.

‎cache/map_test.go‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,26 @@ var _ = Describe("Test cache utils", func() {
4343
store.Put("test2", "test data", 0)
4444
store.Put("test3", "test data", -1)
4545
time.Sleep(3 * time.Second)
46-
value, expired := store.Get("test")
46+
value, found := store.Get("test")
4747
Expect(value).Should(BeNil())
48-
Expect(expired).Should(BeTrue())
48+
Expect(found).Should(BeFalse())
4949

50-
value, expired = store.Get("test2")
50+
value, found = store.Get("test2")
5151
Expect(value).Should(Equal("test data"))
52-
Expect(expired).Should(BeFalse())
52+
Expect(found).Should(BeTrue())
5353

54-
value, expired = store.Get("test3")
54+
value, found = store.Get("test3")
5555
Expect(value).Should(Equal("test data"))
56-
Expect(expired).Should(BeFalse())
56+
Expect(found).Should(BeTrue())
5757
})
5858

5959
It("test cache store delete key", func() {
6060
store := NewMemoryCacheStore[string](context.TODO())
6161
store.Put("test", "test data", time.Minute*2)
6262
store.Delete("test")
63-
value, expired := store.Get("test")
63+
value, found := store.Get("test")
6464
Expect(value).Should(BeNil())
65-
Expect(expired).Should(BeTrue())
65+
Expect(found).Should(BeFalse())
6666
})
6767

6868
It("test cache store with multiple keys", func() {
@@ -74,22 +74,22 @@ var _ = Describe("Test cache utils", func() {
7474

7575
for i := 0; i < 100; i++ {
7676
key := "key-" + strconv.Itoa(i)
77-
value, expired := store.Get(key)
78-
Expect(expired).Should(BeFalse())
77+
value, found := store.Get(key)
78+
Expect(found).Should(BeTrue())
7979
Expect(value).Should(Equal(i))
8080
}
8181
})
8282

8383
It("test cache store overwrite value", func() {
8484
store := NewMemoryCacheStore[string](context.TODO())
8585
store.Put("rw", "v1", time.Second)
86-
value, expired := store.Get("rw")
87-
Expect(expired).Should(BeFalse())
86+
value, found := store.Get("rw")
87+
Expect(found).Should(BeTrue())
8888
Expect(value).Should(Equal("v1"))
8989

9090
store.Put("rw", "v2", time.Second)
91-
value, expired = store.Get("rw")
92-
Expect(expired).Should(BeFalse())
91+
value, found = store.Get("rw")
92+
Expect(found).Should(BeTrue())
9393
Expect(value).Should(Equal("v2"))
9494
})
9595
})

0 commit comments

Comments
 (0)