-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemoryCache.go
More file actions
85 lines (75 loc) · 1.79 KB
/
Copy pathMemoryCache.go
File metadata and controls
85 lines (75 loc) · 1.79 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
package go2cache
import (
"sync"
)
type MemoryCache struct {
//存储读写锁
lock sync.RWMutex
//存储元数据
cacheObjectMap map[string]* CacheObject
}
//初始化 MemoryCache
//当前容器不限制大小
func BuildMemoryCache() *MemoryCache {
return &MemoryCache{cacheObjectMap: make(map[string]*CacheObject)}
}
////周期检查cacheObject对象是否过期,限制大小,清理过期对象
//func (cache *MemoryCache) check() {
// go func() {
// for true {
// <-time.After(DefaultInternal * time.Second)
// //check
// log.Println("begin to clear expired cache object")
// cache.clearExpiredCacheObject()
// }
// }()
//}
//
////清理过期对象
//func (cache *MemoryCache) clearExpiredCacheObject() {
// lock := cache.lock
// lock.Lock()
// defer lock.Unlock()
// for key, cacheObject := range cache.cacheObjectMap {
// if cacheObject.IsExpired() {
// delete(cache.cacheObjectMap, key)
// }
// }
//}
//内存中获取数据
func (cache *MemoryCache) Get(key string) *CacheObject {
lock := cache.lock
lock.RLock()
defer lock.RUnlock()
cacheObj := cache.cacheObjectMap[key]
if cacheObj == nil {
return nil
}
return cacheObj
}
//存储数据到当前cache中
//timeout 对象有效期 0 永不过期
func (cache *MemoryCache) Put(key string, value interface{}) error {
lock := cache.lock
lock.Lock()
defer lock.Unlock()
cache.cacheObjectMap[key] =
&CacheObject{
Value: value} //设置过期时间
return nil
}
//删除缓存数据
func (cache *MemoryCache) Delete(key string) error {
lock := cache.lock
lock.Lock()
defer lock.Unlock()
delete(cache.cacheObjectMap, key)
return nil
}
//检查当前key 是否存在
func (cache *MemoryCache) IsExist(key string) bool {
lock := cache.lock
lock.RLock()
defer lock.RUnlock()
return cache.cacheObjectMap[key] != nil
}