Skip to content

Commit c231a3b

Browse files
committed
Reduce the public interface of cache
1 parent 9add11d commit c231a3b

6 files changed

+30
-19
lines changed

cache/with_fallback.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package cache
22

33
// WithFallback On read it will return the first available value in the
4-
// cachers then write to all the caches missing the value in the way. On write
5-
// it will write to all caches.
6-
type WithFallback[K, V any] struct {
4+
// cachers then write to all the caches missing the value in the way.
5+
//
6+
// On store it will store to all caches.
7+
func WithFallback[K, V any](c ...Cacher[K, V]) Cacher[K, V] {
8+
return &withFallback[K, V]{
9+
Caches: c,
10+
}
11+
}
12+
13+
type withFallback[K, V any] struct {
714
Caches []Cacher[K, V]
815
}
916

1017
// Load will ask every cacher for the key in order. keep track of all caches missing the key.
1118
// When the key is found it writes the value to all the caches that was asked for the key and didn't have it
1219
// effectively bringing the key to cachers at the top of the list.
13-
func (c *WithFallback[K, V]) Load(key K) (value V, ok bool) {
20+
func (c *withFallback[K, V]) Load(key K) (value V, ok bool) {
1421
for i := range c.Caches {
1522
if value, ok = c.Caches[i].Load(key); ok {
1623
// From the previous cache backward until we hit the head of the slice
@@ -27,7 +34,7 @@ func (c *WithFallback[K, V]) Load(key K) (value V, ok bool) {
2734
}
2835

2936
// Store will store the key, value pair in all the cachers in order
30-
func (c *WithFallback[K, V]) Store(key K, value V) {
37+
func (c *withFallback[K, V]) Store(key K, value V) {
3138
for i := range c.Caches {
3239
c.Caches[i].Store(key, value)
3340
}

cache/with_fallback_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import "testing"
55
func TestWithFallback(t *testing.T) {
66
t.Run("Load", func(t *testing.T) {
77
var c1, c2 Cache[string, int]
8-
c := WithFallback[string, int]{
9-
Caches: []Cacher[string, int]{&c1, &c2},
10-
}
8+
c := WithFallback(&c1, &c2)
119

1210
// Looking up non-existent
1311
v, ok := c.Load("non-existent")
@@ -33,9 +31,7 @@ func TestWithFallback(t *testing.T) {
3331

3432
t.Run("Store", func(t *testing.T) {
3533
var c1, c2 Cache[string, int]
36-
c := WithFallback[string, int]{
37-
Caches: []Cacher[string, int]{&c1, &c2},
38-
}
34+
c := WithFallback(&c1, &c2)
3935

4036
c.Store("k1", 1)
4137

cache/with_read_only.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ package cache
33
// WithReadOnly wraps another cacher making it's Store method Noop. useful with
44
// combination of CacheWithFallback. you can supress writes to some levels of
55
// the caches for example a list of [memory, redis, database] will store values
6-
// in all of them on write, wrapping `database` in WithReadOnly will stop it
6+
// in all of them on write, wrapping `database` in withReadOnly will stop it
77
// from being modified. So the CacheWithFallback will bring up the values from
88
// the database to faster cachers without modifying it. Also useful if your
99
// database is readonly.
10-
type WithReadOnly[K, V any] struct {
10+
func WithReadOnly[K, V any](c Loader[K, V]) Cacher[K, V] {
11+
return &withReadOnly[K, V]{c}
12+
}
13+
14+
type withReadOnly[K, V any] struct {
1115
Loader[K, V]
1216
}
1317

1418
// Store method in this case is a Noop
15-
func (r *WithReadOnly[K, V]) Store(key K, value V) {}
19+
func (r *withReadOnly[K, V]) Store(key K, value V) {}

cache/with_read_only_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "testing"
44

55
func TestWithReadOnly(t *testing.T) {
66
var c1 Cache[string, int]
7-
ro := WithReadOnly[string, int]{&c1}
7+
ro := WithReadOnly(&c1)
88

99
c1.Store("k1", 1)
1010
v, ok := ro.Load("k1")

cache/with_write_only.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package cache
22

3-
// WithWriteOnly wraps another cacher making it's Load method Noop. useful for
3+
// withWriteOnly wraps another cacher making it's Load method Noop. useful for
44
// systems that you would like to write to without reading from. For example you
55
// can have the last cache of your system write to Kafka. But doesn't attempt to
66
// read from it.
7-
type WithWriteOnly[K, V any] struct {
7+
func WithWriteOnly[K, V any](c Storer[K, V]) Cacher[K, V] {
8+
return &withWriteOnly[K, V]{c}
9+
}
10+
11+
type withWriteOnly[K, V any] struct {
812
Storer[K, V]
913
}
1014

1115
// Load always respond with false
12-
func (w *WithWriteOnly[K, V]) Load(key K) (value V, ok bool) { return value, false }
16+
func (w *withWriteOnly[K, V]) Load(key K) (value V, ok bool) { return value, false }

cache/with_write_only_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "testing"
44

55
func TestWithWriteOnly(t *testing.T) {
66
var c1 Cache[string, int]
7-
wo := WithWriteOnly[string, int]{&c1}
7+
wo := WithWriteOnly(&c1)
88

99
c1.Store("k1", 1)
1010
v, ok := wo.Load("k1")

0 commit comments

Comments
 (0)