generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
68 lines (56 loc) · 1.51 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
//go:build go1.18
package brick
import (
"context"
"time"
"github.com/bool64/cache"
"github.com/bool64/ctxd"
"github.com/bool64/stats"
)
// TransferCache performs cache transfer.
func (l *BaseLocator) TransferCache(ctx context.Context) error {
if l.BaseConfig.CacheTransferURL == "" || l.cacheTransfer.CachesCount() == 0 {
return nil
}
return l.cacheTransfer.Import(ctx, l.BaseConfig.CacheTransferURL)
}
// MakeCacheOf creates an instance of failover cache and adds it to cache transfer.
func MakeCacheOf[V any](l interface {
StatsTracker() stats.Tracker
CtxdLogger() ctxd.Logger
}, name string, ttl time.Duration, options ...func(cfg *cache.FailoverConfigOf[V]),
) *cache.FailoverOf[V] {
cfg := cache.FailoverConfigOf[V]{}
cfg.Name = name
cfg.Stats = l.StatsTracker()
cfg.Logger = l.CtxdLogger()
for _, option := range options {
option(&cfg)
}
if cfg.Backend == nil {
cfg.Backend = cache.NewShardedMapOf[V](func(cfg *cache.Config) {
cfg.Name = name
cfg.Logger = l.CtxdLogger()
cfg.Stats = l.StatsTracker()
cfg.TimeToLive = ttl
})
}
fc := cache.NewFailoverOf[V](func(c *cache.FailoverConfigOf[V]) {
*c = cfg
})
if l, ok := l.(interface {
CacheTransfer() *cache.HTTPTransfer
}); ok {
if w, ok := cfg.Backend.(cache.WalkDumpRestorer); ok {
l.CacheTransfer().AddCache(name, w)
}
}
if l, ok := l.(interface {
CacheInvalidationIndex() *cache.InvalidationIndex
}); ok {
if d, ok := cfg.Backend.(cache.Deleter); ok {
l.CacheInvalidationIndex().AddCache(name, d)
}
}
return fc
}