File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed
Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change 99 cmap "github.com/orcaman/concurrent-map/v2"
1010)
1111
12+ // Cache is a simple but performant in-memory cache.
1213type Cache struct {
1314 enabled bool
1415 store cmap.ConcurrentMap [string , * cachedValue ]
@@ -22,7 +23,10 @@ type cachedValue struct {
2223}
2324
2425func 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.
4449func (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.
5262func (c * Cache ) Set (key string , data []byte , duration time.Duration ) {
5363 if ! c .enabled {
5464 return
You can’t perform that action at this time.
0 commit comments