-
Notifications
You must be signed in to change notification settings - Fork 9
/
sharded_cache.go
53 lines (41 loc) · 1.03 KB
/
sharded_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
package main
import (
"github.com/badoo/thunder/go-proxyd/vhash"
)
type shardedCache struct {
caches []*vhash.Vhash
}
var sh *shardedCache
func newShardedCache(shards int) *shardedCache {
scache := shardedCache{
caches: make([]*vhash.Vhash, shards),
}
for i := 0; i < len(scache.caches); i++ {
scache.caches[i] = vhash.NewVhash()
}
return &scache
}
func (scache *shardedCache) getCache(key uint64) *vhash.Vhash {
return scache.caches[key%uint64(len(scache.caches))]
}
func (scache *shardedCache) Get(key uint64) (v vhash.Value, ok bool) {
shard := scache.getCache(key)
v, ok = shard.Get(key)
return
}
func (scache *shardedCache) Set(key uint64, value vhash.Value) {
shard := scache.getCache(key)
shard.Set(key, value)
}
func (scache *shardedCache) Delete(key uint64) bool {
shard := scache.getCache(key)
return shard.Delete(key)
}
func (scache *shardedCache) Lock(key uint64) {
shard := scache.getCache(key)
shard.Lock()
}
func (scache *shardedCache) Unlock(key uint64) {
shard := scache.getCache(key)
shard.Unlock()
}