-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupcache.go
More file actions
125 lines (111 loc) · 2.78 KB
/
groupcache.go
File metadata and controls
125 lines (111 loc) · 2.78 KB
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
package groupcache
import (
"fmt"
"github.com/go-needle/cache"
"github.com/go-needle/groupcache/singleflight"
"log"
"sync"
"time"
)
// A Getter loads data for a key.
type Getter interface {
Get(key string) ([]byte, error)
}
// GetterFunc loads data for a key.
type GetterFunc func(key string) ([]byte, error)
// Get implements Getter interface function
func (f GetterFunc) Get(key string) ([]byte, error) {
return f(key)
}
// A Group is a cache namespace and associated data loaded spread over
type Group struct {
name string
getter Getter
mainCache cache.Cache
peers PeerPicker
// use singleflight.Group to make sure that
// each key is only fetched once
loader *singleflight.Group
}
var (
mu sync.RWMutex
groups = make(map[string]*Group)
)
// NewGroup create a new instance of Group
func NewGroup(name string, cacheBytes int64, keySurvivalTime time.Duration, getter Getter) *Group {
if getter == nil {
panic("nil Getter")
}
mu.Lock()
defer mu.Unlock()
g := &Group{
name: name,
getter: getter,
mainCache: cache.NewLRU(cacheBytes, keySurvivalTime),
loader: &singleflight.Group{},
}
groups[name] = g
return g
}
// GetGroup returns the named group previously created with NewGroup, or
// nil if there's no such group.
func GetGroup(name string) *Group {
mu.RLock()
g := groups[name]
mu.RUnlock()
return g
}
// Get value for a key from cache
func (g *Group) Get(key string) (cache.ByteView, error) {
if key == "" {
return cache.NewByteView(nil), fmt.Errorf("key is required")
}
if v, ok := g.mainCache.Get(key); ok {
log.Println("[cache] hit")
return v, nil
}
v, err := g.load(key)
if err != nil {
return cache.NewByteView(nil), err
}
return cache.NewByteView(v), nil
}
// RegisterPeers registers a PeerPicker for choosing remote peer
func (g *Group) RegisterPeers(peers PeerPicker) {
if g.peers != nil {
panic("RegisterPeerPicker called more than once")
}
g.peers = peers
}
func (g *Group) load(key string) (value []byte, err error) {
// each key is only fetched once (either locally or remotely)
// regardless of the number of concurrent callers.
value, err = g.loader.Do(key, func() ([]byte, error) {
if g.peers != nil {
if peer, ok := g.peers.PickPeer(key); ok {
if value, err = g.getFromPeer(peer, key); err == nil {
return value, nil
}
log.Println("[GroupCache] Failed to get from peer", err)
}
}
return g.getLocally(key)
})
return
}
func (g *Group) getFromPeer(peer PeerGetter, key string) ([]byte, error) {
bytes, err := peer.Get(g.name, key)
if err != nil {
return nil, err
}
return bytes, nil
}
func (g *Group) getLocally(key string) ([]byte, error) {
bytes, err := g.getter.Get(key)
if err != nil {
return nil, err
}
v := cache.CloneBytes(bytes)
g.mainCache.Add(key, v)
return v, nil
}