stub of tiered cache
This commit is contained in:
parent
7cf53a2f2d
commit
859849bd47
6
cache/interface.go
vendored
Normal file
6
cache/interface.go
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
package cache
|
||||
|
||||
type Interface[K comparable, V any] interface {
|
||||
Get(key K) V
|
||||
Put(key K, value V)
|
||||
}
|
34
cache/tiered.go
vendored
Normal file
34
cache/tiered.go
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
package cache
|
||||
|
||||
import "reflect"
|
||||
|
||||
type tieredCache[K comparable, V any] struct {
|
||||
inner Interface[K, V]
|
||||
outer Interface[K, V]
|
||||
}
|
||||
|
||||
func NewTieredCache[K comparable, V any](inner, outer Interface[K, V]) Interface[K, V] {
|
||||
return &tieredCache[K, V]{
|
||||
inner: inner,
|
||||
outer: outer,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *tieredCache[K, V]) Get(key K) V {
|
||||
var zero, value V
|
||||
value = t.inner.Get(key)
|
||||
if reflect.DeepEqual(value, zero) {
|
||||
value = t.outer.Get(key)
|
||||
// if required, add value to inner cache for future requests
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (t *tieredCache[K, V]) Put(key K, value V) {
|
||||
t.inner.Put(key, value)
|
||||
|
||||
// add key to outer cache asynchronously
|
||||
go func(key K) {
|
||||
t.outer.Put(key, value)
|
||||
}(key)
|
||||
}
|
Loading…
Reference in New Issue
Block a user