-
Notifications
You must be signed in to change notification settings - Fork 726
/
maps.go
68 lines (56 loc) · 1.51 KB
/
maps.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
package memory
import (
"encoding/json"
"sync"
"github.com/coocood/freecache"
"github.com/golang/glog"
)
// This file contains an interface and some wrapper types for various types of "map-like" structures
// so that we can mix and match them inside the Cache implementation in cache.go.
// Interface which abstracts the common operations of sync.Map and the freecache.Cache
type mapLike interface {
Get(id string) (json.RawMessage, bool)
Set(id string, value json.RawMessage)
Delete(id string)
}
// sync.Map wrapper which implements the interface
type pbsSyncMap struct {
*sync.Map
}
func (m *pbsSyncMap) Get(id string) (json.RawMessage, bool) {
val, ok := m.Map.Load(id)
if ok {
return val.(json.RawMessage), ok
} else {
return nil, ok
}
}
func (m *pbsSyncMap) Set(id string, value json.RawMessage) {
m.Map.Store(id, value)
}
func (m *pbsSyncMap) Delete(id string) {
m.Map.Delete(id)
}
// lruCache wrapper which implements the interface
type pbsLRUCache struct {
*freecache.Cache
ttlSeconds int
}
func (m *pbsLRUCache) Get(id string) (json.RawMessage, bool) {
val, err := m.Cache.Get([]byte(id))
if err == nil {
return val, true
}
if err != freecache.ErrNotFound {
glog.Errorf("unexpected error from freecache: %v", err)
}
return val, false
}
func (m *pbsLRUCache) Set(id string, value json.RawMessage) {
if err := m.Cache.Set([]byte(id), value, m.ttlSeconds); err != nil {
glog.Errorf("error saving value in freecache: %v", err)
}
}
func (m *pbsLRUCache) Delete(id string) {
m.Cache.Del([]byte(id))
}