-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachemanager.go
71 lines (63 loc) · 1.34 KB
/
cachemanager.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
package gcache
import (
"context"
"sync"
"time"
)
type MemoryCacheManager struct {
*sync.RWMutex
ctx context.Context
CacheMap map[string]*MemoryCache
}
func (m *MemoryCacheManager) AddCache(mCacheName string) {
m.Lock()
defer m.Unlock()
m.CacheMap[mCacheName] = &MemoryCache{
Cache: make(map[string]*cacheST, 0),
Lock: new(sync.RWMutex),
InitHashKey: "000102030405060708090A0B0C0D0E0FF0E0D0C0B0A090807060504030201000",
}
}
func (m *MemoryCacheManager) RemoveCache(mCacheName string) {
m.Lock()
defer m.Unlock()
delete(m.CacheMap, mCacheName)
}
func (m *MemoryCacheManager) FlushAll() {
m.Lock()
defer m.Unlock()
m.CacheMap = make(map[string]*MemoryCache, 0)
}
func (m *MemoryCacheManager) GetCache(mCacheName string) CacheInterface {
m.RLock()
defer m.RUnlock()
return m.CacheMap[mCacheName]
}
func (m *MemoryCacheManager) Check() {
ticker := time.Tick(1 * time.Second)
for {
select {
case <-m.ctx.Done():
{
break
}
case <-ticker:
{
m.RLock()
for _, item := range m.CacheMap {
item.Check()
}
m.RUnlock()
}
}
}
}
// NewMemoryCacheManager 新的Cache控管中心
func NewMemoryCacheManager() CacheManager {
manager := &MemoryCacheManager{
RWMutex: new(sync.RWMutex),
ctx: context.Background(),
CacheMap: make(map[string]*MemoryCache, 0),
}
return manager
}