-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
316 lines (275 loc) · 9.82 KB
/
cache.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package rw_freecache
import (
"encoding/binary"
"errors"
"fmt"
"github.com/cespare/xxhash"
"math"
"reflect"
"sync/atomic"
"time"
"unsafe"
)
const (
minBufSize = 512 * 1024
maxSegmentItemCount = int64(9223372036854775800 / 256)
)
var ErrLargeCounterValue = errors.New(fmt.Sprintf("The counter value is larger than %d", maxSegmentItemCount))
type CacheSummaryStatus struct {
hit_count int64 `json:"-"`
lookup_count int64 `json:"-"`
expired_count int64 `json:"-"`
evacuate_count int64 `json:"-"`
TimeStamp int64 //时间片
TimeSlice int64 //时间段 (两次读取之间的时间间隔)
ItemsCount int64 //缓存中对象数量
ItemsHitRate float64 //缓存对象命中率
AvgAccessTime float64 //缓存对象平均访问时间
AvgLookupPerSecond float64 //缓存每秒查询多少次
AvgHitPerSecond float64 //缓存每秒击中对象多少次
AvgExpiredPerSecond float64 //缓存每秒超时多少个对象
AvgEvacuatePerSecond float64 //缓存每秒因为空间不够导致清理的次数 (如果太大表示创建缓存的空间不够)
}
//fix float64 length
func float64ToFixed(f float64, places int) float64 {
shift := math.Pow(10, float64(places))
fv := 0.0000000001 + f //对浮点数产生.xxx999999999 计算不准进行处理
return math.Floor(fv*shift+.5) / shift
}
func stringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&s))))
}
func getCurrTimestamp() int64 {
return int64(time.Now().Unix())
}
type Cache struct {
segments [256]segment
lastStatus CacheSummaryStatus
}
func hashFunc(data []byte) uint64 {
return xxhash.Sum64(data)
}
// The cache size will be set to 512KB at minimum.
// If the size is set relatively large, you should call
// `debug.SetGCPercent()`, set it to a much smaller value
// to limit the memory consumption and GC pause time.
func NewCache(size int) (cache *Cache) {
if size < minBufSize {
size = minBufSize
}
cache = new(Cache)
for i := 0; i < 256; i++ {
cache.segments[i] = newSegment(size/256, i)
}
cache.lastStatus = CacheSummaryStatus{TimeStamp: getCurrTimestamp()}
return
}
// ===============================================================
// byte当key
// If the key is larger than 65535 or value is larger than 1/1024 of the cache size,
// the entry will not be written to the cache. expireSeconds <= 0 means no expire,
// but it can be evicted when cache is full.
func (cache *Cache) Set(key, value []byte, expireSeconds int) (err error) {
hashVal := hashFunc(key)
segId := hashVal & 255
if cache.segments[segId].totalCount >= maxSegmentItemCount ||
cache.segments[segId].overwrites >= maxSegmentItemCount ||
cache.segments[segId].totalEvacuate >= maxSegmentItemCount {
cache.segments[segId].clear()
}
err = cache.segments[segId].set(key, value, hashVal, expireSeconds)
return
}
// Get the value or not found error.
func (cache *Cache) Get(key []byte) (value []byte, err error) {
hashVal := hashFunc(key)
segId := hashVal & 255
if cache.segments[segId].totalExpired >= maxSegmentItemCount ||
cache.segments[segId].missCount >= maxSegmentItemCount ||
cache.segments[segId].hitCount >= maxSegmentItemCount {
cache.segments[segId].clear()
return nil, ErrLargeCounterValue
}
value, _, err = cache.segments[segId].get(key, hashVal)
return
}
// Get the value or not found error.
func (cache *Cache) GetWithExpiration(key []byte) (value []byte, expireAt uint32, err error) {
hashVal := hashFunc(key)
segId := hashVal & 255
if cache.segments[segId].totalExpired >= maxSegmentItemCount ||
cache.segments[segId].missCount >= maxSegmentItemCount ||
cache.segments[segId].hitCount >= maxSegmentItemCount {
cache.segments[segId].clear()
return nil, 0, ErrLargeCounterValue
}
value, expireAt, err = cache.segments[segId].get(key, hashVal)
return
}
func (cache *Cache) TTL(key []byte) (timeLeft uint32, err error) {
hashVal := hashFunc(key)
segId := hashVal & 255
timeLeft, err = cache.segments[segId].ttl(key, hashVal)
return
}
func (cache *Cache) Del(key []byte) (affected bool) {
hashVal := hashFunc(key)
segId := hashVal & 255
affected = cache.segments[segId].del(key, hashVal)
return
}
// ===============================================================
//字符串当key
func (cache *Cache) SetStr(key string, value []byte, expireSeconds int) error {
return cache.Set(stringToBytes(key), value, expireSeconds)
}
func (cache *Cache) SetStrEx(key, value string, expireSeconds int) error {
return cache.Set(stringToBytes(key), stringToBytes(value), expireSeconds)
}
func (cache *Cache) GetStr(key string) ([]byte, error) {
return cache.Get(stringToBytes(key))
}
func (cache *Cache) GetStrWithExpiration(key string) ([]byte, uint32, error) {
return cache.GetWithExpiration(stringToBytes(key))
}
func (cache *Cache) TTLStr(key string) (uint32, error) {
return cache.TTL(stringToBytes(key))
}
func (cache *Cache) DelStr(key string) bool {
return cache.Del(stringToBytes(key))
}
// ===============================================================
//数字当key
func (cache *Cache) SetInt(key int64, value []byte, expireSeconds int) error {
var bKey [8]byte
binary.LittleEndian.PutUint64(bKey[:], uint64(key))
return cache.Set(bKey[:], value, expireSeconds)
}
func (cache *Cache) GetInt(key int64) ([]byte, error) {
var bKey [8]byte
binary.LittleEndian.PutUint64(bKey[:], uint64(key))
return cache.Get(bKey[:])
}
func (cache *Cache) GetIntWithExpiration(key int64) ([]byte, uint32, error) {
var bKey [8]byte
binary.LittleEndian.PutUint64(bKey[:], uint64(key))
return cache.GetWithExpiration(bKey[:])
}
func (cache *Cache) DelInt(key int64) bool {
var bKey [8]byte
binary.LittleEndian.PutUint64(bKey[:], uint64(key))
return cache.Del(bKey[:])
}
func (cache *Cache) TTLInt(key int64) (uint32, error) {
var bKey [8]byte
binary.LittleEndian.PutUint64(bKey[:], uint64(key))
return cache.TTL(bKey[:])
}
// ===============================================================
func (cache *Cache) EvacuateCount() (count int64) {
for i := 0; i < 256; i++ {
count += atomic.LoadInt64(&cache.segments[i].totalEvacuate)
}
return
}
func (cache *Cache) ExpiredCount() (count int64) {
for i := 0; i < 256; i++ {
count += atomic.LoadInt64(&cache.segments[i].totalExpired)
}
return
}
func (cache *Cache) EntryCount() (entryCount int64) {
for i := 0; i < 256; i++ {
entryCount += atomic.LoadInt64(&cache.segments[i].entryCount)
}
return
}
func (cache *Cache) HitCount() (count int64) {
for i := range cache.segments {
count += atomic.LoadInt64(&cache.segments[i].hitCount)
}
return
}
func (cache *Cache) MissCount() (count int64) {
for i := range cache.segments {
count += atomic.LoadInt64(&cache.segments[i].missCount)
}
return
}
func (cache *Cache) LookupCount() int64 {
return cache.HitCount() + cache.MissCount()
}
func (cache *Cache) HitRate() float64 {
hitCount, missCount := cache.HitCount(), cache.MissCount()
lookupCount := hitCount + missCount
if lookupCount == 0 {
return 0
} else {
return float64ToFixed(float64(hitCount)/float64(lookupCount), 3)
}
}
func (cache *Cache) OverwriteCount() (overwriteCount int64) {
for i := 0; i < 256; i++ {
overwriteCount += atomic.LoadInt64(&cache.segments[i].overwrites)
}
return
}
func (cache *Cache) Clear() {
for i := 0; i < 256; i++ {
cache.segments[i].clear()
}
cache.lastStatus.TimeStamp = getCurrTimestamp()
cache.lastStatus.TimeSlice = 0
cache.lastStatus.ItemsCount = 0
cache.lastStatus.ItemsHitRate = 0
cache.lastStatus.hit_count = 0
cache.lastStatus.lookup_count = 0
cache.lastStatus.evacuate_count = 0
cache.lastStatus.expired_count = 0
}
func (cache *Cache) ResetStatistics() {
for i := 0; i < 256; i++ {
cache.segments[i].resetStatistics()
}
cache.lastStatus.TimeStamp = getCurrTimestamp()
cache.lastStatus.TimeSlice = 0
cache.lastStatus.ItemsCount = 0
cache.lastStatus.ItemsHitRate = 0
cache.lastStatus.hit_count = 0
cache.lastStatus.lookup_count = 0
cache.lastStatus.evacuate_count = 0
cache.lastStatus.expired_count = 0
}
func (cache *Cache) GetSummaryStatus() CacheSummaryStatus {
now := getCurrTimestamp()
currentStatus := CacheSummaryStatus{TimeStamp: now, TimeSlice: now - cache.lastStatus.TimeStamp}
itemsCount := cache.EntryCount()
hit_count := cache.HitCount()
lookup_count := cache.LookupCount()
expired_count := cache.ExpiredCount()
evacuate_count := cache.EvacuateCount()
currentStatus.ItemsCount = itemsCount
if currentStatus.TimeSlice > 0 {
currentStatus.hit_count = hit_count - cache.lastStatus.hit_count
currentStatus.lookup_count = lookup_count - cache.lastStatus.lookup_count
currentStatus.evacuate_count = evacuate_count - cache.lastStatus.evacuate_count
currentStatus.expired_count = expired_count - cache.lastStatus.expired_count
currentStatus.AvgLookupPerSecond = float64ToFixed(float64(currentStatus.lookup_count)/float64(currentStatus.TimeSlice), 3)
currentStatus.AvgHitPerSecond = float64ToFixed(float64(currentStatus.hit_count)/float64(currentStatus.TimeSlice), 3)
currentStatus.AvgExpiredPerSecond = float64ToFixed(float64(currentStatus.expired_count)/float64(currentStatus.TimeSlice), 3)
currentStatus.AvgEvacuatePerSecond = float64ToFixed(float64(currentStatus.evacuate_count)/float64(currentStatus.TimeSlice), 3)
if currentStatus.lookup_count > 0 {
currentStatus.ItemsHitRate = float64ToFixed(float64(currentStatus.hit_count)/float64(currentStatus.lookup_count), 3)
currentStatus.AvgAccessTime = float64ToFixed((float64(currentStatus.TimeSlice)/float64(currentStatus.lookup_count))*1000, 3) //Microsecond
} else {
currentStatus.ItemsHitRate = 0
currentStatus.AvgAccessTime = 0
}
cache.lastStatus.TimeStamp = now
cache.lastStatus.hit_count = hit_count
cache.lastStatus.lookup_count = lookup_count
cache.lastStatus.expired_count = expired_count
cache.lastStatus.evacuate_count = evacuate_count
}
return currentStatus
}