-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_cache.go
45 lines (39 loc) · 932 Bytes
/
simple_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
package utils
var cache = map[string]CacheBucket{}
// CacheBucket is a bucket of cache entries
type CacheBucket struct {
Items map[string]*interface{}
}
// CreateCacheBucket creates a cache bucket
func CreateCacheBucket(bucket string) {
if _, ok := cache[bucket]; !ok {
cache[bucket] = CacheBucket{
Items: map[string]*interface{}{},
}
}
}
// PutCache puts a cache entry
func PutCache(bucket string, key string, value interface{}) {
if b, ok := cache[bucket]; ok {
b.Items[key] = &value
} else {
BadDeveloperError("Cache bucket not found!")
}
}
// EvictCache evicts a cache entry
func EvictCache(bucket string, key string) {
if b, ok := cache[bucket]; ok {
if _, ok := b.Items[key]; ok {
delete(b.Items, key)
}
}
}
// GetCache gets a cache entry
func GetCache(bucket string, key string) *interface{} {
if b, ok := cache[bucket]; ok {
if i, ok := b.Items[key]; ok {
return i
}
}
return nil
}