-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
65 lines (52 loc) · 1.07 KB
/
group.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
package sync_cache
import "sync"
const GroupCacheCapacity = 100
type GetterFunc func(key string, setCacheFunc SetCacheFunc) error
type SetCacheFunc func(i interface{})
type CacheGroup struct {
name string
sync.RWMutex
cache map[string]CacheEntity
getterFunc GetterFunc
}
type CacheEntity struct {
uuid string
object interface{}
}
type CacheStats struct {
Items int
}
func NewCacheGroup(name string, getterFunc GetterFunc) *CacheGroup {
return &CacheGroup{
name: name,
cache: make(map[string]CacheEntity, GroupCacheCapacity),
getterFunc: getterFunc,
}
}
func (g *CacheGroup) Name() string {
return g.name
}
func (g *CacheGroup) CacheStats() *CacheStats {
return &CacheStats{
Items: len(g.cache),
}
}
func (g *CacheGroup) set(key string, i interface{}, uuid string) {
g.Lock()
g.cache[key] = CacheEntity{
object: i,
uuid: uuid,
}
g.Unlock()
}
func (g *CacheGroup) get(key string) CacheEntity {
g.RLock()
k := g.cache[key]
g.RUnlock()
return k
}
func (g *CacheGroup) delete(key string) {
g.Lock()
delete(g.cache, key)
g.Unlock()
}