github.com/go-graphite/carbonapi@v0.17.0/pathcache/pathcache.go (about) 1 package pathcache 2 3 import ( 4 "github.com/dgryski/go-expirecache" 5 "github.com/go-graphite/carbonapi/zipper/types" 6 7 "time" 8 ) 9 10 // PathCache provides general interface to cache find and search queries 11 type PathCache struct { 12 ec *expirecache.Cache 13 14 expireDelaySec int32 15 } 16 17 // NewPathCache initializes PathCache structure 18 func NewPathCache(ExpireDelaySec int32) PathCache { 19 20 p := PathCache{ 21 ec: expirecache.New(0), 22 expireDelaySec: ExpireDelaySec, 23 } 24 25 go p.ec.ApproximateCleaner(10 * time.Second) 26 27 return p 28 } 29 30 // ECItems returns amount of items in the cache 31 func (p *PathCache) ECItems() int { 32 return p.ec.Items() 33 } 34 35 // ECSize returns size of the cache 36 func (p *PathCache) ECSize() uint64 { 37 return p.ec.Size() 38 } 39 40 // Set allows to set a key (k) to value (v). 41 func (p *PathCache) Set(k string, v []types.BackendServer) { 42 43 var size uint64 44 for _, vv := range v { 45 size += uint64(len(vv.Backends())) 46 } 47 48 p.ec.Set(k, v, size, p.expireDelaySec) 49 } 50 51 // Get returns an an element by key. If not successful - returns also false in second var. 52 func (p *PathCache) Get(k string) ([]types.BackendServer, bool) { 53 if v, ok := p.ec.Get(k); ok { 54 return v.([]types.BackendServer), true 55 } 56 57 return nil, false 58 }