forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.go
61 lines (51 loc) · 1.21 KB
/
default.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
package agcache
import (
"errors"
"sync"
)
//DefaultCache 默认缓存
type DefaultCache struct {
defaultCache sync.Map
}
//Set 获取缓存
func (d *DefaultCache) Set(key string, value []byte, expireSeconds int) (err error) {
d.defaultCache.Store(key, value)
return nil
}
//EntryCount 获取实体数量
func (d *DefaultCache) EntryCount() (entryCount int64) {
count := int64(0)
d.defaultCache.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}
//Get 获取缓存
func (d *DefaultCache) Get(key string) (value []byte, err error) {
v, ok := d.defaultCache.Load(key)
if !ok {
return nil, errors.New("load default cache fail")
}
return v.([]byte), nil
}
//Range 遍历缓存
func (d *DefaultCache) Range(f func(key, value interface{}) bool) {
d.defaultCache.Range(f)
}
//Del 删除缓存
func (d *DefaultCache) Del(key string) (affected bool) {
d.defaultCache.Delete(key)
return true
}
//Clear 清除所有缓存
func (d *DefaultCache) Clear() {
d.defaultCache = sync.Map{}
}
//DefaultCacheFactory 构造默认缓存组件工厂类
type DefaultCacheFactory struct {
}
//Create 创建默认缓存组件
func (d *DefaultCacheFactory) Create() CacheInterface {
return &DefaultCache{}
}