chore: refactor caching (#18378)

This commit is contained in:
andig 2025-01-25 13:22:10 +01:00 • committed by GitHub
parent 2410e957cf
commit fe388db66a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
60 changed files with 138 additions and 174 deletions

View file

@ -1,139 +0,0 @@
package plugin
import (
"errors"
"math"
"sync"
"time"
"github.com/asaskevich/EventBus"
"github.com/benbjohnson/clock"
"github.com/evcc-io/evcc/api"
)
var bus = EventBus.New()
const (
reset = "reset"
backoffDuration = 5 * time.Second
)
func ResetCached() {
bus.Publish(reset)
}
// cached wraps a getter with a cache
type cached[T any] struct {
mux sync.Mutex
clock clock.Clock
updated time.Time
retried time.Time
cache time.Duration
backoffCounter int
g func() (T, error)
val T
err error
}
// Cached wraps a getter with a cache
func Cached[T any](g func() (T, error), cache time.Duration) func() (T, error) {
c := ResettableCached(g, cache)
return c.Get
}
// Cacheable is the interface for a resettable cache
type Cacheable[T any] interface {
Get() (T, error)
Reset()
}
var _ Cacheable[int64] = (*cached[int64])(nil)
// ResettableCached wraps a getter with a cache. It returns a `Cacheable`.
// Instead of the cached getter, the `Get()` and `Reset()` methods are exposed.
func ResettableCached[T any](g func() (T, error), cache time.Duration) *cached[T] {
clock := clock.New()
c := &cached[T]{
clock: clock,
cache: cache,
g: g,
}
_ = bus.Subscribe(reset, c.Reset)
return c
}
func (c *cached[T]) Get() (T, error) {
c.mux.Lock()
defer c.mux.Unlock()
if c.mustUpdate() {
c.val, c.err = c.g()
c.updated = c.clock.Now()
c.retried = c.clock.Now()
if c.err == nil {
c.backoffCounter = 0
}
}
return c.val, c.err
}
func (c *cached[T]) Reset() {
c.mux.Lock()
c.updated = time.Time{}
c.retried = time.Time{}
c.mux.Unlock()
}
func (c *cached[T]) mustUpdate() bool {
return c.clock.Since(c.updated) > c.cache ||
errors.Is(c.err, api.ErrMustRetry) ||
c.err != nil && c.shouldRetryWithBackoff()
}
// shouldRetryWithBackoff returns true when exponential back-off duration has elapsed since last retry
func (c *cached[T]) shouldRetryWithBackoff() bool {
if c.clock.Since(c.retried) > backoffDuration*time.Duration(math.Pow(2, float64(c.backoffCounter))) {
c.backoffCounter++
return true
}
return false
}
// Value is a cacheable value that can expire
type Value[T any] struct {
mux sync.RWMutex
clock clock.Clock
updated time.Time
cache time.Duration
val T
}
func NewValue[T any](cache time.Duration) *Value[T] {
return &Value[T]{
clock: clock.New(),
cache: cache,
}
}
func (v *Value[T]) Get() (T, error) {
v.mux.RLock()
defer v.mux.RUnlock()
if v.clock.Since(v.updated) > v.cache {
var zero T
return zero, api.ErrTimeout
}
return v.val, nil
}
func (v *Value[T]) Set(val T) {
v.mux.Lock()
defer v.mux.Unlock()
v.val = val
v.updated = v.clock.Now()
}

View file

@ -1,130 +0,0 @@
package plugin
import (
"errors"
"testing"
"time"
"github.com/benbjohnson/clock"
"github.com/evcc-io/evcc/api"
"github.com/stretchr/testify/assert"
)
func TestCachedGetter(t *testing.T) {
var idx int
cases := []struct {
f float64
e error
}{
{f: 1, e: nil},
{f: 2, e: nil},
{f: 3, e: errors.New("3")},
}
g := func() (float64, error) {
f := cases[idx].f
e := cases[idx].e
idx++
return f, e
}
duration := time.Second
c := ResettableCached(g, duration)
clock := clock.NewMock()
c.clock = clock
expect := func(s struct {
f float64
e error
},
) {
f, e := c.Get()
if f != s.f || e != s.e {
t.Errorf("unexpected cache value: %f, %v\n", f, e)
}
}
expect(cases[0])
expect(cases[0])
clock.Add(2 * duration)
expect(cases[1])
clock.Add(2 * duration)
expect(cases[2])
}
func TestCacheReset(t *testing.T) {
var i int64
g := func() (int64, error) {
i++
return i, nil
}
c := ResettableCached(g, 10*time.Minute)
clock := clock.NewMock()
c.clock = clock
test := func(exp int64) {
v, _ := c.Get()
if exp != v {
t.Errorf("expected %d, got %d", exp, v)
}
}
test(1)
test(1)
c.Reset()
test(2)
test(2)
clock.Add(10*time.Minute + 1)
test(3)
}
func TestRetryWithBackoff(t *testing.T) {
tests := []struct {
deltaTime time.Duration
returnError bool
functionCalled bool
}{
{0 * time.Second, true, true},
{4 * time.Second, true, false},
{6 * time.Second, true, true},
{9 * time.Second, true, false},
{11 * time.Second, true, true},
{14 * time.Second, true, false},
{16 * time.Second, false, true},
{16 * time.Minute, true, true},
{4 * time.Second, true, false},
{6 * time.Second, true, true},
}
var returnError, functionCalled bool
g := func() (int64, error) {
functionCalled = true
if returnError {
return 1, api.ErrTimeout
}
return 1, nil
}
c := ResettableCached(g, 15*time.Minute)
clock := clock.NewMock()
c.clock = clock
for _, tt := range tests {
functionCalled = false
returnError = tt.returnError
clock.Add(tt.deltaTime)
_, err := c.Get()
if returnError {
assert.Error(t, err)
}
assert.Equal(t, tt.functionCalled, functionCalled)
}
}