-
Notifications
You must be signed in to change notification settings - Fork 139
/
pathcache.go
58 lines (43 loc) · 1.19 KB
/
pathcache.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
package pathcache
import (
"github.com/dgryski/go-expirecache"
"github.com/go-graphite/carbonapi/zipper/types"
"time"
)
// PathCache provides general interface to cache find and search queries
type PathCache struct {
ec *expirecache.Cache
expireDelaySec int32
}
// NewPathCache initializes PathCache structure
func NewPathCache(ExpireDelaySec int32) PathCache {
p := PathCache{
ec: expirecache.New(0),
expireDelaySec: ExpireDelaySec,
}
go p.ec.ApproximateCleaner(10 * time.Second)
return p
}
// ECItems returns amount of items in the cache
func (p *PathCache) ECItems() int {
return p.ec.Items()
}
// ECSize returns size of the cache
func (p *PathCache) ECSize() uint64 {
return p.ec.Size()
}
// Set allows to set a key (k) to value (v).
func (p *PathCache) Set(k string, v []types.BackendServer) {
var size uint64
for _, vv := range v {
size += uint64(len(vv.Backends()))
}
p.ec.Set(k, v, size, p.expireDelaySec)
}
// Get returns an an element by key. If not successful - returns also false in second var.
func (p *PathCache) Get(k string) ([]types.BackendServer, bool) {
if v, ok := p.ec.Get(k); ok {
return v.([]types.BackendServer), true
}
return nil, false
}