forked from mirrors/statsd_exporter
0099df7f71
Signed-off-by: glightfoot <glightfoot@rsglab.com>
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package lru
|
|
|
|
import (
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
lru2 "github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/prometheus/statsd_exporter/pkg/mapper_cache"
|
|
)
|
|
|
|
type metricMapperLRUCache struct {
|
|
cache *lru2.Cache
|
|
metrics *mapper_cache.CacheMetrics
|
|
}
|
|
|
|
func NewMetricMapperLRUCache(reg prometheus.Registerer, size int) (*metricMapperLRUCache, error) {
|
|
if size <= 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
metrics := mapper_cache.NewCacheMetrics(reg)
|
|
cache, err := lru2.New(size)
|
|
if err != nil {
|
|
return &metricMapperLRUCache{}, err
|
|
}
|
|
|
|
return &metricMapperLRUCache{metrics: metrics, cache: cache}, nil
|
|
}
|
|
|
|
func (m *metricMapperLRUCache) Get(metricKey string) (interface{}, bool) {
|
|
m.metrics.CacheGetsTotal.Inc()
|
|
if result, ok := m.cache.Get(metricKey); ok {
|
|
m.metrics.CacheHitsTotal.Inc()
|
|
return result, true
|
|
} else {
|
|
return nil, false
|
|
}
|
|
}
|
|
|
|
func (m *metricMapperLRUCache) Add(metricKey string, result interface{}) {
|
|
go m.trackCacheLength()
|
|
m.cache.Add(metricKey, result)
|
|
}
|
|
|
|
func (m *metricMapperLRUCache) trackCacheLength() {
|
|
m.metrics.CacheLength.Set(float64(m.cache.Len()))
|
|
}
|
|
|
|
func (m *metricMapperLRUCache) Reset() {
|
|
m.cache.Purge()
|
|
m.metrics.CacheLength.Set(0)
|
|
}
|