-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathgcache_adapter_memory.go
489 lines (453 loc) · 16.2 KB
/
gcache_adapter_memory.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gcache
import (
"context"
"math"
"time"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/container/gtype"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/os/gtimer"
)
// AdapterMemory is an adapter implements using memory.
type AdapterMemory struct {
data *memoryData // data is the underlying cache data which is stored in a hash table.
expireTimes *memoryExpireTimes // expireTimes is the expiring key to its timestamp mapping, which is used for quick indexing and deleting.
expireSets *memoryExpireSets // expireSets is the expiring timestamp to its key set mapping, which is used for quick indexing and deleting.
lru *memoryLru // lru is the LRU manager, which is enabled when attribute cap > 0.
eventList *glist.List // eventList is the asynchronous event list for internal data synchronization.
closed *gtype.Bool // closed controls the cache closed or not.
}
// Internal event item.
type adapterMemoryEvent struct {
k interface{} // Key.
e int64 // Expire time in milliseconds.
}
const (
// defaultMaxExpire is the default expire time for no expiring items.
// It equals to math.MaxInt64/1000000.
defaultMaxExpire = 9223372036854
)
// NewAdapterMemory creates and returns a new adapter_memory cache object.
func NewAdapterMemory() *AdapterMemory {
return doNewAdapterMemory()
}
// NewAdapterMemoryLru creates and returns a new adapter_memory cache object with LRU.
func NewAdapterMemoryLru(cap int) *AdapterMemory {
c := doNewAdapterMemory()
c.lru = newMemoryLru(cap)
return c
}
// doNewAdapterMemory creates and returns a new adapter_memory cache object.
func doNewAdapterMemory() *AdapterMemory {
c := &AdapterMemory{
data: newMemoryData(),
expireTimes: newMemoryExpireTimes(),
expireSets: newMemoryExpireSets(),
eventList: glist.New(true),
closed: gtype.NewBool(),
}
// Here may be a "timer leak" if adapter is manually changed from adapter_memory adapter.
// Do not worry about this, as adapter is less changed, and it does nothing if it's not used.
gtimer.AddSingleton(context.Background(), time.Second, c.syncEventAndClearExpired)
return c
}
// Set sets cache with `key`-`value` pair, which is expired after `duration`.
//
// It does not expire if `duration` == 0.
// It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
func (c *AdapterMemory) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error {
defer c.handleLruKey(ctx, key)
expireTime := c.getInternalExpire(duration)
c.data.Set(key, memoryDataItem{
v: value,
e: expireTime,
})
c.eventList.PushBack(&adapterMemoryEvent{
k: key,
e: expireTime,
})
return nil
}
// SetMap batch sets cache with key-value pairs by `data` map, which is expired after `duration`.
//
// It does not expire if `duration` == 0.
// It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
func (c *AdapterMemory) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error {
var (
expireTime = c.getInternalExpire(duration)
err = c.data.SetMap(data, expireTime)
)
if err != nil {
return err
}
for k := range data {
c.eventList.PushBack(&adapterMemoryEvent{
k: k,
e: expireTime,
})
}
if c.lru != nil {
for key := range data {
c.handleLruKey(ctx, key)
}
}
return nil
}
// SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration`
// if `key` does not exist in the cache. It returns true the `key` does not exist in the
// cache, and it sets `value` successfully to the cache, or else it returns false.
//
// It does not expire if `duration` == 0.
// It deletes the `key` if `duration` < 0 or given `value` is nil.
func (c *AdapterMemory) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (bool, error) {
defer c.handleLruKey(ctx, key)
isContained, err := c.Contains(ctx, key)
if err != nil {
return false, err
}
if !isContained {
if _, err = c.doSetWithLockCheck(ctx, key, value, duration); err != nil {
return false, err
}
return true, nil
}
return false, nil
}
// SetIfNotExistFunc sets `key` with result of function `f` and returns true
// if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
//
// The parameter `value` can be type of `func() interface{}`, but it does nothing if its
// result is nil.
//
// It does not expire if `duration` == 0.
// It deletes the `key` if `duration` < 0 or given `value` is nil.
func (c *AdapterMemory) SetIfNotExistFunc(ctx context.Context, key interface{}, f Func, duration time.Duration) (bool, error) {
defer c.handleLruKey(ctx, key)
isContained, err := c.Contains(ctx, key)
if err != nil {
return false, err
}
if !isContained {
value, err := f(ctx)
if err != nil {
return false, err
}
if _, err = c.doSetWithLockCheck(ctx, key, value, duration); err != nil {
return false, err
}
return true, nil
}
return false, nil
}
// SetIfNotExistFuncLock sets `key` with result of function `f` and returns true
// if `key` does not exist in the cache, or else it does nothing and returns false if `key` already exists.
//
// It does not expire if `duration` == 0.
// It deletes the `key` if `duration` < 0 or given `value` is nil.
//
// Note that it differs from function `SetIfNotExistFunc` is that the function `f` is executed within
// writing mutex lock for concurrent safety purpose.
func (c *AdapterMemory) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f Func, duration time.Duration) (bool, error) {
defer c.handleLruKey(ctx, key)
isContained, err := c.Contains(ctx, key)
if err != nil {
return false, err
}
if !isContained {
if _, err = c.doSetWithLockCheck(ctx, key, f, duration); err != nil {
return false, err
}
return true, nil
}
return false, nil
}
// Get retrieves and returns the associated value of given `key`.
// It returns nil if it does not exist, or its value is nil, or it's expired.
// If you would like to check if the `key` exists in the cache, it's better using function Contains.
func (c *AdapterMemory) Get(ctx context.Context, key interface{}) (*gvar.Var, error) {
item, ok := c.data.Get(key)
if ok && !item.IsExpired() {
c.handleLruKey(ctx, key)
return gvar.New(item.v), nil
}
return nil, nil
}
// GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and
// returns `value` if `key` does not exist in the cache. The key-value pair expires
// after `duration`.
//
// It does not expire if `duration` == 0.
// It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
// if `value` is a function and the function result is nil.
func (c *AdapterMemory) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (*gvar.Var, error) {
defer c.handleLruKey(ctx, key)
v, err := c.Get(ctx, key)
if err != nil {
return nil, err
}
if v == nil {
return c.doSetWithLockCheck(ctx, key, value, duration)
}
return v, nil
}
// GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of
// function `f` and returns its result if `key` does not exist in the cache. The key-value
// pair expires after `duration`.
//
// It does not expire if `duration` == 0.
// It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
// if `value` is a function and the function result is nil.
func (c *AdapterMemory) GetOrSetFunc(ctx context.Context, key interface{}, f Func, duration time.Duration) (*gvar.Var, error) {
defer c.handleLruKey(ctx, key)
v, err := c.Get(ctx, key)
if err != nil {
return nil, err
}
if v == nil {
value, err := f(ctx)
if err != nil {
return nil, err
}
if value == nil {
return nil, nil
}
return c.doSetWithLockCheck(ctx, key, value, duration)
}
return v, nil
}
// GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of
// function `f` and returns its result if `key` does not exist in the cache. The key-value
// pair expires after `duration`.
//
// It does not expire if `duration` == 0.
// It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
// if `value` is a function and the function result is nil.
//
// Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within
// writing mutex lock for concurrent safety purpose.
func (c *AdapterMemory) GetOrSetFuncLock(ctx context.Context, key interface{}, f Func, duration time.Duration) (*gvar.Var, error) {
defer c.handleLruKey(ctx, key)
v, err := c.Get(ctx, key)
if err != nil {
return nil, err
}
if v == nil {
return c.doSetWithLockCheck(ctx, key, f, duration)
}
return v, nil
}
// Contains checks and returns true if `key` exists in the cache, or else returns false.
func (c *AdapterMemory) Contains(ctx context.Context, key interface{}) (bool, error) {
v, err := c.Get(ctx, key)
if err != nil {
return false, err
}
return v != nil, nil
}
// GetExpire retrieves and returns the expiration of `key` in the cache.
//
// Note that,
// It returns 0 if the `key` does not expire.
// It returns -1 if the `key` does not exist in the cache.
func (c *AdapterMemory) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) {
if item, ok := c.data.Get(key); ok {
c.handleLruKey(ctx, key)
return time.Duration(item.e-gtime.TimestampMilli()) * time.Millisecond, nil
}
return -1, nil
}
// Remove deletes one or more keys from cache, and returns its value.
// If multiple keys are given, it returns the value of the last deleted item.
func (c *AdapterMemory) Remove(ctx context.Context, keys ...interface{}) (*gvar.Var, error) {
defer c.lru.Remove(keys...)
value, err := c.doRemove(ctx, keys...)
if err != nil {
return nil, err
}
return gvar.New(value), nil
}
func (c *AdapterMemory) doRemove(_ context.Context, keys ...interface{}) (*gvar.Var, error) {
var removedKeys []interface{}
removedKeys, value, err := c.data.Remove(keys...)
if err != nil {
return nil, err
}
for _, key := range removedKeys {
c.eventList.PushBack(&adapterMemoryEvent{
k: key,
e: gtime.TimestampMilli() - 1000,
})
}
return gvar.New(value), nil
}
// Update updates the value of `key` without changing its expiration and returns the old value.
// The returned value `exist` is false if the `key` does not exist in the cache.
//
// It deletes the `key` if given `value` is nil.
// It does nothing if `key` does not exist in the cache.
func (c *AdapterMemory) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
v, exist, err := c.data.Update(key, value)
if exist {
c.handleLruKey(ctx, key)
}
return gvar.New(v), exist, err
}
// UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
//
// It returns -1 and does nothing if the `key` does not exist in the cache.
// It deletes the `key` if `duration` < 0.
func (c *AdapterMemory) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
newExpireTime := c.getInternalExpire(duration)
oldDuration, err = c.data.UpdateExpire(key, newExpireTime)
if err != nil {
return
}
if oldDuration != -1 {
c.eventList.PushBack(&adapterMemoryEvent{
k: key,
e: newExpireTime,
})
c.handleLruKey(ctx, key)
}
return
}
// Size returns the size of the cache.
func (c *AdapterMemory) Size(ctx context.Context) (size int, err error) {
return c.data.Size()
}
// Data returns a copy of all key-value pairs in the cache as map type.
func (c *AdapterMemory) Data(ctx context.Context) (map[interface{}]interface{}, error) {
return c.data.Data()
}
// Keys returns all keys in the cache as slice.
func (c *AdapterMemory) Keys(ctx context.Context) ([]interface{}, error) {
return c.data.Keys()
}
// Values returns all values in the cache as slice.
func (c *AdapterMemory) Values(ctx context.Context) ([]interface{}, error) {
return c.data.Values()
}
// Clear clears all data of the cache.
// Note that this function is sensitive and should be carefully used.
func (c *AdapterMemory) Clear(ctx context.Context) error {
c.data.Clear()
c.lru.Clear()
return nil
}
// Close closes the cache.
func (c *AdapterMemory) Close(ctx context.Context) error {
c.closed.Set(true)
return nil
}
// doSetWithLockCheck sets cache with `key`-`value` pair if `key` does not exist in the
// cache, which is expired after `duration`.
//
// It does not expire if `duration` == 0.
// The parameter `value` can be type of <func() interface{}>, but it does nothing if the
// function result is nil.
//
// It doubly checks the `key` whether exists in the cache using mutex writing lock
// before setting it to the cache.
func (c *AdapterMemory) doSetWithLockCheck(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) {
expireTimestamp := c.getInternalExpire(duration)
v, err := c.data.SetWithLock(ctx, key, value, expireTimestamp)
c.eventList.PushBack(&adapterMemoryEvent{k: key, e: expireTimestamp})
return gvar.New(v), err
}
// getInternalExpire converts and returns the expiration time with given expired duration in milliseconds.
func (c *AdapterMemory) getInternalExpire(duration time.Duration) int64 {
if duration == 0 {
return defaultMaxExpire
}
return gtime.TimestampMilli() + duration.Nanoseconds()/1000000
}
// makeExpireKey groups the `expire` in milliseconds to its according seconds.
func (c *AdapterMemory) makeExpireKey(expire int64) int64 {
return int64(math.Ceil(float64(expire/1000)+1) * 1000)
}
// syncEventAndClearExpired does the asynchronous task loop:
// 1. Asynchronously process the data in the event list,
// and synchronize the results to the `expireTimes` and `expireSets` properties.
// 2. Clean up the expired key-value pair data.
func (c *AdapterMemory) syncEventAndClearExpired(ctx context.Context) {
if c.closed.Val() {
gtimer.Exit()
return
}
var (
event *adapterMemoryEvent
oldExpireTime int64
newExpireTime int64
)
// ================================
// Data expiration synchronization.
// ================================
for {
v := c.eventList.PopFront()
if v == nil {
break
}
event = v.(*adapterMemoryEvent)
// Fetching the old expire set.
oldExpireTime = c.expireTimes.Get(event.k)
// Calculating the new expiration time set.
newExpireTime = c.makeExpireKey(event.e)
// Expiration changed for this key.
if newExpireTime != oldExpireTime {
c.expireSets.GetOrNew(newExpireTime).Add(event.k)
if oldExpireTime != 0 {
c.expireSets.GetOrNew(oldExpireTime).Remove(event.k)
}
// Updating the expired time for `event.k`.
c.expireTimes.Set(event.k, newExpireTime)
}
}
// =================================
// Data expiration auto cleaning up.
// =================================
var (
expireSet *gset.Set
expireTime int64
currentEk = c.makeExpireKey(gtime.TimestampMilli())
)
// auto removing expiring key set for latest seconds.
for i := int64(1); i <= 5; i++ {
expireTime = currentEk - i*1000
if expireSet = c.expireSets.Get(expireTime); expireSet != nil {
// Iterating the set to delete all keys in it.
expireSet.Iterator(func(key interface{}) bool {
c.deleteExpiredKey(key)
// remove auto expired key for lru.
c.lru.Remove(key)
return true
})
// Deleting the set after all of its keys are deleted.
c.expireSets.Delete(expireTime)
}
}
}
func (c *AdapterMemory) handleLruKey(ctx context.Context, keys ...interface{}) {
if c.lru == nil {
return
}
if evictedKeys := c.lru.SaveAndEvict(keys...); len(evictedKeys) > 0 {
_, _ = c.doRemove(ctx, evictedKeys...)
return
}
return
}
// clearByKey deletes the key-value pair with given `key`.
// The parameter `force` specifies whether doing this deleting forcibly.
func (c *AdapterMemory) deleteExpiredKey(key interface{}) {
// Doubly check before really deleting it from cache.
c.data.Delete(key)
// Deleting its expiration time from `expireTimes`.
c.expireTimes.Delete(key)
}