forked from mirrors/statsd_exporter
Merge pull request #375 from bakins/lru-cache
Use groupcache for LRU cache
This commit is contained in:
commit
2a6bbd7827
3 changed files with 48 additions and 10 deletions
2
go.mod
2
go.mod
|
@ -2,7 +2,7 @@ module github.com/prometheus/statsd_exporter
|
|||
|
||||
require (
|
||||
github.com/go-kit/kit v0.10.0
|
||||
github.com/hashicorp/golang-lru v0.5.4
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
|
||||
github.com/prometheus/client_golang v1.9.0
|
||||
github.com/prometheus/client_model v0.2.0
|
||||
github.com/prometheus/common v0.19.0
|
||||
|
|
4
go.sum
4
go.sum
|
@ -76,6 +76,8 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
|
|||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
|
@ -123,8 +125,6 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
|
|||
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
|
||||
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
|
||||
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
|
||||
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
|
||||
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
|
||||
|
|
|
@ -14,15 +14,17 @@
|
|||
package lru
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
lru2 "github.com/hashicorp/golang-lru"
|
||||
"github.com/golang/groupcache/lru"
|
||||
|
||||
"github.com/prometheus/statsd_exporter/pkg/mappercache"
|
||||
)
|
||||
|
||||
type metricMapperLRUCache struct {
|
||||
cache *lru2.Cache
|
||||
cache *lruCache
|
||||
metrics *mappercache.CacheMetrics
|
||||
}
|
||||
|
||||
|
@ -32,10 +34,7 @@ func NewMetricMapperLRUCache(reg prometheus.Registerer, size int) (*metricMapper
|
|||
}
|
||||
|
||||
metrics := mappercache.NewCacheMetrics(reg)
|
||||
cache, err := lru2.New(size)
|
||||
if err != nil {
|
||||
return &metricMapperLRUCache{}, err
|
||||
}
|
||||
cache := newLruCache(size)
|
||||
|
||||
return &metricMapperLRUCache{metrics: metrics, cache: cache}, nil
|
||||
}
|
||||
|
@ -60,6 +59,45 @@ func (m *metricMapperLRUCache) trackCacheLength() {
|
|||
}
|
||||
|
||||
func (m *metricMapperLRUCache) Reset() {
|
||||
m.cache.Purge()
|
||||
m.cache.Clear()
|
||||
m.metrics.CacheLength.Set(0)
|
||||
}
|
||||
|
||||
type lruCache struct {
|
||||
cache *lru.Cache
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func newLruCache(maxEntries int) *lruCache {
|
||||
return &lruCache{
|
||||
cache: lru.New(maxEntries),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *lruCache) Get(key string) (interface{}, bool) {
|
||||
l.lock.RLock()
|
||||
defer l.lock.RUnlock()
|
||||
|
||||
return l.cache.Get(key)
|
||||
}
|
||||
|
||||
func (l *lruCache) Add(key string, value interface{}) {
|
||||
l.lock.Lock()
|
||||
defer l.lock.Unlock()
|
||||
|
||||
l.cache.Add(key, value)
|
||||
}
|
||||
|
||||
func (l *lruCache) Len() int {
|
||||
l.lock.RLock()
|
||||
defer l.lock.RUnlock()
|
||||
|
||||
return l.cache.Len()
|
||||
}
|
||||
|
||||
func (l *lruCache) Clear() {
|
||||
l.lock.Lock()
|
||||
defer l.lock.Unlock()
|
||||
|
||||
l.cache.Clear()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue