Skip to content

Commit 8d69dda

Browse files
committed
fixed cache.Get
1 parent a74ffe4 commit 8d69dda

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

cache.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
cmap "github.com/orcaman/concurrent-map/v2"
1010
)
1111

12+
// Cache is a simple but performant in-memory cache.
1213
type Cache struct {
1314
enabled bool
1415
store cmap.ConcurrentMap[string, *cachedValue]
@@ -22,7 +23,10 @@ type cachedValue struct {
2223
}
2324

2425
func newCache() *Cache {
25-
return &Cache{store: cmap.New[*cachedValue]()}
26+
return &Cache{
27+
store: cmap.New[*cachedValue](),
28+
enabled: true,
29+
}
2630
}
2731

2832
// UseCache sets whether to use cache.
@@ -41,14 +45,20 @@ func (h *Client) SetCacheTime(d time.Duration) {
4145
h.cache.cacheTime = d
4246
}
4347

48+
// Get gets a value from the cache, and a boolean indicating whether the value was found.
4449
func (c *Cache) Get(key string) ([]byte, bool) {
4550
if !c.enabled {
4651
return nil, false
4752
}
53+
4854
value, ok := c.store.Get(key)
55+
if !ok {
56+
return nil, false
57+
}
4958
return value.data, ok
5059
}
5160

61+
// Set sets a value in the cache, with a duration after it gets removed.
5262
func (c *Cache) Set(key string, data []byte, duration time.Duration) {
5363
if !c.enabled {
5464
return

0 commit comments

Comments
 (0)