-
-
Notifications
You must be signed in to change notification settings - Fork 528
/
cache.go
107 lines (89 loc) · 2.18 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package provider
import (
"errors"
"math"
"sync"
"time"
"github.com/asaskevich/EventBus"
"github.com/benbjohnson/clock"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
)
var (
bus = EventBus.New()
log = util.NewLogger("cache")
)
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
}