-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a963118
commit ac1034e
Showing
4 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Package patrickmn includes cache adapter for https://github.com/patrickmn/go-cache package | ||
package patrickmn | ||
|
||
|
||
import ( | ||
"github.com/emad-elsaid/memoize/cache" | ||
) | ||
|
||
// GoCacher cache interface (the part memoize need) | ||
type GoCacher interface { | ||
Get(k string) (any, bool) | ||
SetDefault(k string, x any) | ||
} | ||
|
||
// GoCache creates a new cacher from the patrickmn/go-cache | ||
// For example: | ||
// | ||
// c := cache.New(5*time.Minute, 10*time.Minute) | ||
// memoize.NewWithCache( | ||
// patrickmn.GoCache[int](c), | ||
// func(s string) int { return len(s) }, | ||
// ) | ||
func GoCache[V any](c GoCacher) cache.Cacher[string, V] { | ||
return &goCache[V]{c} | ||
} | ||
|
||
type goCache[V any] struct { | ||
GoCacher | ||
} | ||
|
||
func (h *goCache[V]) Load(key string) (value V, loaded bool) { | ||
out, loaded := h.GoCacher.Get(key) | ||
val, ok := out.(V) | ||
|
||
if !ok { | ||
return value, false | ||
} | ||
|
||
return val, true | ||
} | ||
|
||
func (h *goCache[V]) Store(key string, value V) { | ||
h.GoCacher.SetDefault(key, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package patrickmn | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
type Mock map[string]any | ||
|
||
|
||
func (m Mock) Get(k string) (any, bool) { | ||
v, ok := m[k] | ||
return v, ok | ||
} | ||
|
||
func (m Mock) SetDefault(k string, x any) { | ||
m[k] = x | ||
} | ||
|
||
func TestGoCache(t *testing.T) { | ||
m := Mock{} | ||
cacher := GoCache[int](m) | ||
cacher.Store("k1", 1) | ||
|
||
v, ok := cacher.Load("k1") | ||
if v != 1 { | ||
t.Error("Expected", 1, "got", v) | ||
} | ||
|
||
if !ok { | ||
t.Error("Expected", true, "got", ok) | ||
} | ||
} |