forked from grafana/groupcache_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupcache.go
69 lines (48 loc) · 1.28 KB
/
groupcache.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"context"
"log"
"net/http"
"os"
"time"
"github.com/modernprogram/groupcache/v2"
)
func startGroupcache() *groupcache.Group {
ttl := time.Minute
log.Printf("groupcache ttl: %v", ttl)
//
// create groupcache pool
//
groupcachePort := ":5000"
myURL := "http:https://127.0.0.1" + groupcachePort
log.Printf("groupcache my URL: %s", myURL)
pool := groupcache.NewHTTPPoolOpts(myURL, &groupcache.HTTPPoolOptions{})
//
// start groupcache server
//
serverGroupCache := &http.Server{Addr: groupcachePort, Handler: pool}
go func() {
log.Printf("groupcache server: listening on %s", groupcachePort)
err := serverGroupCache.ListenAndServe()
log.Printf("groupcache server: exited: %v", err)
}()
pool.Set(myURL)
//
// create cache
//
var groupcacheSizeBytes int64 = 1_000_000
// https://talks.golang.org/2013/oscon-dl.slide#46
//
// 64 MB max per-node memory usage
cache := groupcache.NewGroup("files", groupcacheSizeBytes, groupcache.GetterFunc(
func(ctx context.Context, key string, dest groupcache.Sink) error {
log.Printf("getter: loading: key:%s, ttl:%v", key, ttl)
data, errFile := os.ReadFile(key)
if errFile != nil {
return errFile
}
expire := time.Now().Add(ttl)
return dest.SetBytes(data, expire)
}))
return cache
}