-
Notifications
You must be signed in to change notification settings - Fork 175
/
cache.go
167 lines (143 loc) · 4.01 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package pebble
import (
"errors"
"fmt"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/storage"
)
const DefaultCacheSize = uint(10_000)
type CacheType int
const (
CacheTypeLRU CacheType = iota + 1
CacheTypeTwoQueue
)
func ParseCacheType(s string) (CacheType, error) {
switch s {
case CacheTypeLRU.String():
return CacheTypeLRU, nil
case CacheTypeTwoQueue.String():
return CacheTypeTwoQueue, nil
default:
return 0, errors.New("invalid cache type")
}
}
func (m CacheType) String() string {
switch m {
case CacheTypeLRU:
return "lru"
case CacheTypeTwoQueue:
return "2q"
default:
return ""
}
}
type CacheBackend interface {
Get(key string) (value flow.RegisterValue, ok bool)
Add(key string, value flow.RegisterValue)
Contains(key string) bool
Len() int
Remove(key string)
}
// wrapped is a wrapper around lru.Cache to implement CacheBackend
// this is needed because the standard lru cache implementation provides additional features that
// the 2Q cache do not. This standardizes the interface to allow swapping between types.
type wrapped struct {
cache *lru.Cache[string, flow.RegisterValue]
}
func (c *wrapped) Get(key string) (value flow.RegisterValue, ok bool) {
return c.cache.Get(key)
}
func (c *wrapped) Add(key string, value flow.RegisterValue) {
_ = c.cache.Add(key, value)
}
func (c *wrapped) Contains(key string) bool {
return c.cache.Contains(key)
}
func (c *wrapped) Len() int {
return c.cache.Len()
}
func (c *wrapped) Remove(key string) {
_ = c.cache.Remove(key)
}
type ReadCache struct {
metrics module.CacheMetrics
resource string
cache CacheBackend
retrieve func(key string) (flow.RegisterValue, error)
}
func newReadCache(
collector module.CacheMetrics,
resourceName string,
cacheType CacheType,
cacheSize uint,
retrieve func(key string) (flow.RegisterValue, error),
) (*ReadCache, error) {
cache, err := getCache(cacheType, int(cacheSize))
if err != nil {
return nil, fmt.Errorf("could not create cache: %w", err)
}
c := ReadCache{
metrics: collector,
resource: resourceName,
cache: cache,
retrieve: retrieve,
}
c.metrics.CacheEntries(c.resource, uint(c.cache.Len()))
return &c, nil
}
func getCache(cacheType CacheType, size int) (CacheBackend, error) {
switch cacheType {
case CacheTypeLRU:
cache, err := lru.New[string, flow.RegisterValue](size)
if err != nil {
return nil, err
}
return &wrapped{cache: cache}, nil
case CacheTypeTwoQueue:
return lru.New2Q[string, flow.RegisterValue](size)
default:
return nil, fmt.Errorf("unknown cache type: %d", cacheType)
}
}
// IsCached returns true if the key exists in the cache.
// It DOES NOT check whether the key exists in the underlying data store.
func (c *ReadCache) IsCached(key string) bool {
return c.cache.Contains(key)
}
// Get will try to retrieve the resource from cache first, and then from the
// injected. During normal operations, the following error returns are expected:
// - `storage.ErrNotFound` if key is unknown.
func (c *ReadCache) Get(key string) (flow.RegisterValue, error) {
resource, cached := c.cache.Get(key)
if cached {
c.metrics.CacheHit(c.resource)
if resource == nil {
return nil, storage.ErrNotFound
}
return resource, nil
}
// get it from the database
resource, err := c.retrieve(key)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
c.cache.Add(key, nil)
c.metrics.CacheEntries(c.resource, uint(c.cache.Len()))
c.metrics.CacheNotFound(c.resource)
}
return nil, fmt.Errorf("could not retrieve resource: %w", err)
}
c.metrics.CacheMiss(c.resource)
c.cache.Add(key, resource)
c.metrics.CacheEntries(c.resource, uint(c.cache.Len()))
return resource, nil
}
func (c *ReadCache) Remove(key string) {
c.cache.Remove(key)
}
// Insert will add a resource directly to the cache with the given ID
func (c *ReadCache) Insert(key string, resource flow.RegisterValue) {
c.cache.Add(key, resource)
c.metrics.CacheEntries(c.resource, uint(c.cache.Len()))
}