-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_evict_use_rate.go
164 lines (137 loc) · 3.49 KB
/
store_evict_use_rate.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package dm
import (
"errors"
"fmt"
"reflect"
"time"
"github.com/bluele/gcache"
)
// LRUStore LRU模式的淘汰存储
type LRUStore struct {
baseEvictStore
}
func NewLRUStore(size int, expire time.Duration) *LRUStore {
e := gcache.New(size).LRU().Clock(clocker).Build()
p := &LRUStore{
baseEvictStore: baseEvictStore{
cacheEngine: e,
checkExpired: false,
},
}
return p
}
// LFUStore LFU模式的淘汰存储
type LFUStore struct {
baseEvictStore
}
func NewLFUStore(size int) *LFUStore {
e := gcache.New(size).LFU().Clock(clocker).Build()
p := &LFUStore{
baseEvictStore: baseEvictStore{
cacheEngine: e,
checkExpired: false,
},
}
return p
}
// ARCStore ARC模式的淘汰存储
type ARCStore struct {
baseEvictStore
}
func NewARCStore(size int) *ARCStore {
e := gcache.New(size).ARC().Clock(clocker).Build()
p := &ARCStore{
baseEvictStore: baseEvictStore{
cacheEngine: e,
checkExpired: false,
},
}
return p
}
// 基础的淘汰存储
type baseEvictStore struct {
BaseStore
cacheEngine gcache.Cache
checkExpired bool
}
func (b *baseEvictStore) BackFill(key KeyType, value interface{}) error {
// 回填操作只需要Set
return b.Set(key, value)
}
func (b *baseEvictStore) Size() int {
return b.cacheEngine.Len(b.checkExpired)
}
func (b *baseEvictStore) HasKey(key KeyType) bool {
return b.cacheEngine.Has(key)
}
func (b *baseEvictStore) Keys() (keys []KeyType) {
ikeys := b.cacheEngine.Keys(b.checkExpired)
if size := len(ikeys); size > 0 {
keys = make([]KeyType, 0, size)
for i := 0; i < len(ikeys); i++ {
if k, ok := ikeys[i].(KeyType); ok {
keys = append(keys, k)
}
}
}
return
}
func (b *baseEvictStore) Get(key KeyType) (value interface{}, err error) {
value, err = b.cacheEngine.GetIFPresent(key)
if err == gcache.KeyNotFoundError {
return nil, KeyNotExists
}
return
}
func (b *baseEvictStore) Set(key KeyType, value interface{}) error {
return b.cacheEngine.Set(key, value)
}
func (b *baseEvictStore) Del(key KeyType) (has bool) {
return b.cacheEngine.Remove(key)
}
func (b *baseEvictStore) Fetch(key KeyType, valuePtr interface{}) error {
if typ := reflect.TypeOf(valuePtr); typ.Kind() != reflect.Ptr {
return fmt.Errorf("input type:%s not ptr", typ.Name())
}
data, err := b.Get(key)
if err != nil {
return err
}
vv := reflect.ValueOf(valuePtr)
if !vv.IsValid() {
return fmt.Errorf("input value unvalid")
}
v := reflect.ValueOf(data)
if v.Kind() == reflect.Ptr {
vv.Elem().Set(v.Elem())
} else {
vv.Elem().Set(v)
}
return nil
}
func (b *baseEvictStore) Iterator(iter func(keyType KeyType, value interface{}) bool) error {
if iter == nil {
return errors.New("input iter is nil")
}
kvs := b.cacheEngine.GetALL(b.checkExpired)
for key, value := range kvs {
if k, ok := key.(KeyType); ok {
c := iter(k, value)
if !c {
break
}
}
}
return nil
}
func (b *baseEvictStore) Capacity() int {
return b.cacheEngine.Len(b.checkExpired) * b.SeedSize()
}
func (b *baseEvictStore) Purge() {
b.cacheEngine.Purge()
}
var clocker = &clock{}
type clock struct {}
func (*clock) Now() time.Time {
return time.Now()
}