Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
hit/miss stats for matchcache
Browse files Browse the repository at this point in the history
  • Loading branch information
Dieterbe committed Mar 9, 2017
1 parent a0938a9 commit 3bae47f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions mdata/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ func ConfigProcess() {
}

func Cache(cleanInterval, expireAfter time.Duration) {
schemasCache = matchcache.New(cleanInterval, expireAfter)
aggsCache = matchcache.New(cleanInterval, expireAfter)
schemasCache = matchcache.New("schemas", cleanInterval, expireAfter)
aggsCache = matchcache.New("aggs", cleanInterval, expireAfter)
}
14 changes: 12 additions & 2 deletions mdata/matchcache/matchcache.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package matchcache

import (
"fmt"
"sync"
"time"

"github.com/raintank/metrictank/stats"
)

// Cache caches key to uint16 lookups (for schemas and aggregations)
Expand All @@ -15,18 +18,22 @@ type Cache struct {

cleanInterval time.Duration
expireAfter time.Duration
hits *stats.Counter32
miss *stats.Counter32
}

type Item struct {
val uint16
seen int64
}

func New(cleanInterval, expireAfter time.Duration) *Cache {
func New(name string, cleanInterval, expireAfter time.Duration) *Cache {
m := &Cache{
data: make(map[string]Item),
cleanInterval: cleanInterval,
expireAfter: expireAfter,
hits: stats.NewCounter32(fmt.Sprintf("idx.matchcache.%s.ops.hit", name)),
miss: stats.NewCounter32(fmt.Sprintf("idx.matchcache.%s.ops.miss", name)),
}
go m.maintain()
return m
Expand All @@ -38,7 +45,10 @@ type AddFunc func(key string) uint16
func (m *Cache) Get(key string, fn AddFunc) uint16 {
m.Lock()
item, ok := m.data[key]
if !ok {
if ok {
m.hits.Inc()
} else {
m.miss.Inc()
item.val = fn(key)
}
item.seen = time.Now().Unix()
Expand Down

0 comments on commit 3bae47f

Please sign in to comment.