forked from weaveworks/scope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
41 lines (32 loc) · 757 Bytes
/
cache.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
package process
import (
"time"
"github.com/armon/go-metrics"
"github.com/coocood/freecache"
"github.com/weaveworks/scope/common/fs"
)
const (
generalTimeout = 30 // seconds
statsTimeout = 10 //seconds
)
var (
hitMetricsKey = []string{"process", "cache", "hit"}
missMetricsKey = []string{"process", "cache", "miss"}
)
var fileCache = freecache.NewCache(1024 * 16)
type entry struct {
buf []byte
err error
ts time.Time
}
func cachedReadFile(path string) ([]byte, error) {
key := []byte(path)
if v, err := fileCache.Get(key); err == nil {
metrics.IncrCounter(hitMetricsKey, 1.0)
return v, nil
}
buf, err := fs.ReadFile(path)
fileCache.Set(key, buf, generalTimeout)
metrics.IncrCounter(missMetricsKey, 1.0)
return buf, err
}