-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver_ristretto.go
110 lines (88 loc) · 2.25 KB
/
driver_ristretto.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package microcache
import (
"unsafe"
"github.com/dgraph-io/ristretto"
)
var (
requestOptsSize = int64(unsafe.Sizeof(RequestOpts{}))
responseSize = int64(unsafe.Sizeof(Response{}))
)
// DriverRistretto is a driver implementation using github.com/dgraph-io/ristretto
type DriverRistretto struct {
Driver
Cache *ristretto.Cache
}
func calculateResponseCost(res Response) int64 {
s := responseSize
// Estimate size of the map itself.
s += 5*8 + int64(len(res.header)*8)
for k, vv := range res.header {
s += int64(len(k))
for _, v := range vv {
s += int64(len(v))
}
}
s += int64(cap(res.body))
return s
}
func calculateRequestOptCost(req RequestOpts) int64 {
s := requestOptsSize
for _, v := range req.vary {
s += int64(len(v))
}
for _, v := range req.varyQuery {
s += int64(len(v))
}
return s
}
// NewDriverRistretto returns the default Ristretto driver configuration.
// requests should be the number of items you expect to keep in the cache when full.
// Estimating this on the higher side is better.
// size determines the maximum number of bytes in the cache.
func NewDriverRistretto(requests, size int64) DriverRistretto {
if size == 0 {
size = 1
}
if requests == 0 {
requests = size
}
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: requests * 10,
MaxCost: size,
BufferItems: 64,
Metrics: true, // Required to implement Driver.GetSize()
})
if err != nil {
panic(err)
}
return DriverRistretto{Cache: cache}
}
func (d DriverRistretto) SetRequestOpts(hash string, req RequestOpts) error {
d.Cache.Set(hash, req, calculateRequestOptCost(req))
return nil
}
func (d DriverRistretto) GetRequestOpts(hash string) (req RequestOpts) {
r, ok := d.Cache.Get(hash)
if ok && r != nil {
req = r.(RequestOpts)
}
return req
}
func (d DriverRistretto) Set(hash string, res Response) error {
d.Cache.Set(hash, res, calculateResponseCost(res))
return nil
}
func (d DriverRistretto) Get(hash string) (res Response) {
r, ok := d.Cache.Get(hash)
if ok && r != nil {
res = r.(Response)
}
return res
}
func (d DriverRistretto) Remove(hash string) error {
d.Cache.Del(hash)
return nil
}
func (d DriverRistretto) GetSize() int {
return int(d.Cache.Metrics.KeysAdded() - d.Cache.Metrics.KeysEvicted())
}