-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathobjectarraycache.go
More file actions
87 lines (72 loc) · 2.28 KB
/
objectarraycache.go
File metadata and controls
87 lines (72 loc) · 2.28 KB
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
package objectarraycache
import (
"context"
"time"
"github.com/stackrox/rox/pkg/sync"
)
// RefreshFunction is a function that retrieves an array of objects.
type RefreshFunction[T any] func(ctx context.Context) ([]*T, error)
// ObjectArrayCache is a cache for an array of objects.
// The object array in the cache instance are valid for a pre-defined period,
// and are retrieved from the source when they are not valid anymore.
type ObjectArrayCache[T any] struct {
mutex sync.RWMutex
lastRefreshed time.Time
validityPeriod time.Duration
objectCache []*T
refreshFn RefreshFunction[T]
}
// NewObjectArrayCache returns a cache for an object array where the cached
// objects are retrieved using refreshFn and are valid for validityPeriod.
func NewObjectArrayCache[T any](validityPeriod time.Duration, refreshFn RefreshFunction[T]) *ObjectArrayCache[T] {
return &ObjectArrayCache[T]{
validityPeriod: validityPeriod,
refreshFn: refreshFn,
}
}
// GetObjects retrieves the array of objects, either from cache (when not
// expired), or using the refresh function (when the cache is expired).
func (c *ObjectArrayCache[T]) GetObjects(ctx context.Context) ([]*T, error) {
objects, valid := c.getObjectsFromCache()
if valid {
return objects, nil
}
objects, err := c.refreshFn(ctx)
if err != nil {
return nil, err
}
c.refreshCache(objects)
return objects, nil
}
func (c *ObjectArrayCache[T]) getObjectsFromCache() ([]*T, bool) {
now := time.Now()
c.mutex.RLock()
defer c.mutex.RUnlock()
if now.After(c.lastRefreshed.Add(c.validityPeriod)) {
// Cache expired, notify that the cache should be refreshed
return nil, false
}
res := make([]*T, 0, len(c.objectCache))
res = append(res, c.objectCache...)
return res, true
}
func (c *ObjectArrayCache[T]) refreshCache(objects []*T) {
refreshTime := time.Now()
c.mutex.Lock()
defer c.mutex.Unlock()
if refreshTime.Before(c.lastRefreshed.Add(c.validityPeriod)) {
// The cache was refreshed in the meantime, skip cache refresh
return
}
c.lastRefreshed = refreshTime
c.objectCache = objects
}
// Refresh triggers a manual refresh of the cache.
func (c *ObjectArrayCache[T]) Refresh(ctx context.Context) error {
objects, err := c.refreshFn(ctx)
if err != nil {
return err
}
c.refreshCache(objects)
return nil
}