Skip to content

Commit 5605c5e

Browse files
committed
Add a noop cache
1 parent 7e0286a commit 5605c5e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

cache/noop.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cache
2+
3+
// Noop discards all writes and load always fails. useful for tests for example.
4+
type Noop[K, V any] struct{}
5+
6+
func (_ Noop[K, V]) Load(_ K) (value V, loaded bool) { return }
7+
func (_ Noop[K, V]) Store(_ K, _ V) {}

cache/noop_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cache
2+
3+
import "testing"
4+
5+
func TestNoop(t *testing.T) {
6+
t.Run("Implements Cacher", func(t *testing.T) {
7+
var _ Cacher[any, any] = Noop[any, any]{}
8+
})
9+
10+
t.Run("Discards writes", func(t *testing.T) {
11+
c := Noop[string, int]{}
12+
c.Store("k1", 1)
13+
v, loaded := c.Load("k1")
14+
assertEqual(t, 0, v)
15+
assertEqual(t, false, loaded)
16+
})
17+
}

0 commit comments

Comments
 (0)