1
1
package cache
2
2
3
3
// 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 {
7
14
Caches []Cacher [K , V ]
8
15
}
9
16
10
17
// Load will ask every cacher for the key in order. keep track of all caches missing the key.
11
18
// When the key is found it writes the value to all the caches that was asked for the key and didn't have it
12
19
// 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 ) {
14
21
for i := range c .Caches {
15
22
if value , ok = c .Caches [i ].Load (key ); ok {
16
23
// 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) {
27
34
}
28
35
29
36
// 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 ) {
31
38
for i := range c .Caches {
32
39
c .Caches [i ].Store (key , value )
33
40
}
0 commit comments