Skip to content

Commit bb4579a

Browse files
committed
add goburrow adapter
1 parent 917058d commit bb4579a

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,30 @@ var strlen = memoize.NewWithCache(
148148
)
149149
```
150150

151+
### [goburrow/cache](https://github.com/goburrow/cache)
152+
153+
```go
154+
import (
155+
"github.com/emad-elsaid/memoize"
156+
"github.com/emad-elsaid/memoize/cache/adapters/goburrow"
157+
"github.com/goburrow/cache"
158+
)
159+
160+
var strlen = memoize.NewWithCache(
161+
goburrow.Mango[cache.Key, cache.Value, string, int](
162+
cache.New(
163+
cache.WithMaximumSize(100), // Limit number of entries in the cache.
164+
cache.WithExpireAfterAccess(1*time.Minute), // Expire entries after 1 minute since last accessed.
165+
cache.WithRefreshAfterWrite(2*time.Minute), // Expire entries after 2 minutes since last created.
166+
),
167+
),
168+
func(s string) int {
169+
fmt.Println(s)
170+
return len(s)
171+
},
172+
)
173+
```
174+
151175
### To be implemented
152176

153177
* https://github.com/bradfitz/gomemcache
@@ -157,7 +181,6 @@ var strlen = memoize.NewWithCache(
157181
* https://pegasus.apache.org/
158182
* https://github.com/hazelcast/hazelcast-go-client
159183
* https://github.com/allegro/bigcache
160-
* https://github.com/goburrow/cache
161184

162185
## Brenchmarks
163186

cache/adapters/goburrow/cache.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package goburrow
2+
3+
import "github.com/emad-elsaid/memoize/cache"
4+
5+
type MangoCache[K, V any] interface {
6+
GetIfPresent(K) (V, bool)
7+
Put(K, V)
8+
}
9+
10+
func Mango[K, V, OK, OV any](c MangoCache[K, V]) cache.Cacher[OK, OV] {
11+
return &mangoCache[K, V, OK, OV]{c}
12+
}
13+
14+
type mangoCache[K, V, OK, OV any] struct {
15+
MangoCache[K, V]
16+
}
17+
18+
func (h *mangoCache[K, V, OK, OV]) Load(key OK) (value OV, loaded bool) {
19+
k, ok := any(key).(K)
20+
if !ok {
21+
return value, false
22+
}
23+
24+
v, loaded := h.MangoCache.GetIfPresent(k)
25+
if !loaded {
26+
return value, false
27+
}
28+
29+
value, loaded = any(v).(OV)
30+
31+
return value, loaded
32+
}
33+
34+
func (h *mangoCache[K, V, OK, OV]) Store(key OK, value OV) {
35+
k, ok := any(key).(K)
36+
if !ok {
37+
return
38+
}
39+
40+
v, ok := any(value).(V)
41+
if !ok {
42+
return
43+
}
44+
45+
h.MangoCache.Put(k, v)
46+
}

0 commit comments

Comments
 (0)