-
Notifications
You must be signed in to change notification settings - Fork 37
/
cache.go
162 lines (129 loc) · 3.49 KB
/
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"errors"
"fmt"
"sync"
"time"
"github.com/dgraph-io/ristretto"
starlark_time "go.starlark.net/lib/time"
"go.starlark.net/starlark"
)
func init() {
starlark.Universe["Cache"] = starlark.NewBuiltin("Cache", newCache)
}
var (
// caches provides a collection of named caches for Starlark scripts to
// store data in.
caches = make(map[string]*ristretto.Cache)
cacheLock sync.RWMutex
)
// getCache gets or creates a cache with the specified name, and sets its capacity.
func getCache(name string, capacity int64) *ristretto.Cache {
cacheLock.RLock()
c, ok := caches[name]
cacheLock.RUnlock()
if ok {
c.UpdateMaxCost(capacity)
return c
}
cacheLock.Lock()
defer cacheLock.Unlock()
// It's possible that another goroutine has created the cache by now.
// If so, we don't want to create another one.
c, ok = caches[name]
if ok {
c.UpdateMaxCost(capacity)
return c
}
c, err := ristretto.NewCache(&ristretto.Config{
NumCounters: capacity * 10,
MaxCost: capacity,
BufferItems: 64,
})
if err != nil {
panic(err)
}
caches[name] = c
return c
}
func newCache(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var name string
var capacity int64
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 2, &name, &capacity); err != nil {
return nil, err
}
c := getCache(name, capacity)
return &Cache{
name: name,
cache: c,
}, nil
}
// A Cache is a Starlark wrapper for a ristretto.Cache.
type Cache struct {
name string
cache *ristretto.Cache
}
func (c *Cache) String() string {
return fmt.Sprintf("Cache(%q)", c.name)
}
func (c *Cache) Type() string {
return "Cache"
}
func (c *Cache) Freeze() {
return
}
func (c *Cache) Truth() starlark.Bool {
return true
}
func (c *Cache) Hash() (uint32, error) {
return 0, errors.New("unhashable type: Cache")
}
var cacheAttrNames = []string{"del", "get", "set"}
func (c *Cache) AttrNames() []string {
return cacheAttrNames
}
func (c *Cache) Attr(name string) (starlark.Value, error) {
switch name {
case "del":
return starlark.NewBuiltin(name, cacheDel).BindReceiver(c), nil
case "get":
return starlark.NewBuiltin(name, cacheGet).BindReceiver(c), nil
case "set":
return starlark.NewBuiltin(name, cacheSet).BindReceiver(c), nil
default:
return nil, nil
}
}
func cacheDel(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
c := fn.Receiver().(*Cache)
var key string
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &key); err != nil {
return nil, err
}
c.cache.Del(key)
return starlark.None, nil
}
func cacheGet(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
c := fn.Receiver().(*Cache)
var key string
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &key); err != nil {
return nil, err
}
val, ok := c.cache.Get(key)
if !ok {
return starlark.None, nil
}
return val.(starlark.Value), nil
}
func cacheSet(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
c := fn.Receiver().(*Cache)
var key string
var val starlark.Value
var ttl starlark_time.Duration
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 2, &key, &val, &ttl); err != nil {
return nil, err
}
val.Freeze()
c.cache.SetWithTTL(key, val, 1, time.Duration(ttl))
return starlark.None, nil
}