/
cache.go
124 lines (108 loc) · 2.39 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
package util
import (
"hash/fnv"
"sync"
)
type Cache struct {
data map[string]interface{}
lock sync.Mutex
}
func NewCache() *Cache {
return &Cache{
data: make(map[string]interface{}),
}
}
func (this_ *Cache) Get(key string) (interface{}, bool) {
this_.lock.Lock()
v, find := this_.data[key]
this_.lock.Unlock()
return v, find
}
// Put 设置值
func (this_ *Cache) Put(key string, v interface{}) {
this_.lock.Lock()
this_.data[key] = v
this_.lock.Unlock()
}
// PutIfAbsent 不存在则新增
func (this_ *Cache) PutIfAbsent(key string, v interface{}) {
this_.lock.Lock()
_, find := this_.data[key]
if !find {
this_.data[key] = v
}
this_.lock.Unlock()
}
// Delete 删除
func (this_ *Cache) Delete(keys ...string) {
this_.lock.Lock()
for _, key := range keys {
delete(this_.data, key)
}
this_.lock.Unlock()
}
// Clear 清理
func (this_ *Cache) Clear() {
this_.lock.Lock()
this_.data = make(map[string]interface{})
this_.lock.Unlock()
}
type CacheGroup struct {
caches []*Cache
cacheSize uint32
}
func NewCacheGroup(cacheSize int) *CacheGroup {
g := &CacheGroup{
cacheSize: uint32(cacheSize),
}
g.init()
return g
}
func (this_ *CacheGroup) init() {
var caches []*Cache
var i uint32
for i = 0; i < this_.cacheSize; i++ {
var cache = NewCache()
caches = append(caches, cache)
}
this_.caches = caches
}
func (this_ *CacheGroup) GetStringHashCode(str string) uint32 {
h := fnv.New32a()
_, _ = h.Write([]byte(str))
return h.Sum32()
}
func (this_ *CacheGroup) GetCache(key string) *Cache {
hashCode := this_.GetStringHashCode(key)
return this_.GetCacheByHash(hashCode)
}
func (this_ *CacheGroup) GetCacheByHash(hashCode uint32) *Cache {
index := hashCode % this_.cacheSize
return this_.caches[index]
}
func (this_ *CacheGroup) Get(key string) (interface{}, bool) {
return this_.GetCache(key).Get(key)
}
// Put 设置值
func (this_ *CacheGroup) Put(key string, v interface{}) {
this_.GetCache(key).Put(key, v)
}
// PutIfAbsent 不存在则新增
func (this_ *CacheGroup) PutIfAbsent(key string, v interface{}) {
this_.GetCache(key).PutIfAbsent(key, v)
}
// Delete 删除
func (this_ *CacheGroup) Delete(keys ...string) {
ks := map[uint32][]string{}
for _, key := range keys {
c := this_.GetStringHashCode(key)
ks[c] = append(ks[c], key)
}
for c, k := range ks {
this_.GetCacheByHash(c).Delete(k...)
}
}
// Clear 清理
func (this_ *CacheGroup) Clear() {
this_.init()
}