generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
localcache.go
467 lines (378 loc) · 12.5 KB
/
localcache.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
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package coherence
import (
"fmt"
"golang.org/x/text/language"
"golang.org/x/text/message"
"math"
"sort"
"sync"
"sync/atomic"
"time"
"unsafe"
)
var (
_ localCache[string, string] = &localCacheImpl[string, string]{}
_ CacheStats = &localCacheImpl[string, string]{}
)
const (
KB = 1024
MB = KB * KB
GB = MB * KB
prunePercent = 20
)
// localCache implements a local cache of values.
type localCache[K comparable, V any] interface {
Put(key K, value V) *V
PutWithExpiry(key K, value V, ttl time.Duration) *V
Get(key K) *V
GetAll(keys []K) map[K]*V
Remove(key K) *V
Size() int
Clear()
Release()
GetStats() CacheStats
}
// CacheStats defines various statics for near caches.
type CacheStats interface {
GetCacheHits() int64 // the number of entries served from the near cache
GetCacheMisses() int64 // the number of entries that had to be retrieved from the cluster
GetCacheMissesDuration() time.Duration // the total duration of all misses
GetHitRate() float32 // the hit rate of the near cache
GetCachePuts() int64 // the number of entries put in the near cache
GetTotalGets() int64 // the number of gets against the near cache
GetCachePrunes() int64 // the number of times the near cache was pruned
GetCachePrunesDuration() time.Duration // the duration of all prunes
Size() int // the number of entries in the near cache
SizeBytes() int64 // the number of bytes used by the entries (keys and values) in the near cache
ResetStats() // reset the stats for the near cache, not including Size() or SizeBytes()
}
type localCacheImpl[K comparable, V any] struct {
Name string
options *localCacheOptions
sync.Mutex
data map[K]*localCacheEntry[K, V]
cacheHits int64
cacheMisses int64
cacheMissesNannos int64
cachePuts int64
cachePrunes int64
cachePrunesNannos int64
cacheMemory int64
}
type localCacheEntry[K comparable, V any] struct {
key K
value V
ttl time.Duration
insertTime time.Time
lastAccess time.Time
}
type pair[K comparable] struct {
key K
timeStamp time.Time
}
type pairList[K comparable] []pair[K]
func (p pairList[K]) Len() int {
return len(p)
}
func (p pairList[K]) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p pairList[K]) Less(i, j int) bool {
return p[i].timeStamp.Nanosecond() < p[j].timeStamp.Nanosecond()
}
// Put associates the specified value with the specified key returning the previously
// mapped value. V will be nil if there was no previous value.
func (l *localCacheImpl[K, V]) Put(key K, value V) *V {
return l.PutWithExpiry(key, value, l.options.TTL)
}
// PutWithExpiry associates the specified value with the specified key. If the cache
// previously contained a value for this key, the old value is replaced.
// This variation of the Put() function that allows the caller to specify an expiry (or "time to live")
// for the cache entry. V will be nil if there was no previous value.
func (l *localCacheImpl[K, V]) PutWithExpiry(key K, value V, ttl time.Duration) *V {
l.Lock()
defer l.Unlock()
l.registerPut()
l.pruneEntries()
newEntry := newLocalCacheEntry[K, V](key, value, ttl)
l.updateEntrySize(newEntry, 1)
prev, ok := l.data[key]
l.data[key] = newEntry
if ok {
return &prev.value
}
return nil
}
// Get returns the value to which the specified key is mapped. V will be nil if there was no mapped value.
func (l *localCacheImpl[K, V]) Get(key K) *V {
l.Lock()
defer l.Unlock()
l.expireEntries()
v, ok := l.data[key]
if !ok {
return nil
}
v.lastAccess = time.Now()
return &v.value
}
// GetAll returns the entries for each key if it exists.
func (l *localCacheImpl[K, V]) GetAll(keys []K) map[K]*V {
l.Lock()
defer l.Unlock()
l.expireEntries()
results := make(map[K]*V, 0)
for _, key := range keys {
v, ok := l.data[key]
if ok {
// have entry so add to the results
results[key] = &v.value
v.lastAccess = time.Now()
}
}
return results
}
// Remove removes the mapping for a key from the cache if it is present and returns the previously
// mapped value, if any. V will be nil if there was no previous value.
func (l *localCacheImpl[K, V]) Remove(key K) *V {
l.Lock()
defer l.Unlock()
l.expireEntries()
v, ok := l.data[key]
if ok {
delete(l.data, key)
l.updateEntrySize(v, -1)
return &v.value
}
return nil
}
// Size returns the number of mappings contained within the cache.
func (l *localCacheImpl[K, V]) Size() int {
l.Lock()
defer l.Unlock()
l.expireEntries()
return len(l.data)
}
// SizeBytes returns the number of bytes used by the entries (keys and values) in the near cache.
func (l *localCacheImpl[K, V]) SizeBytes() int64 {
l.Lock()
defer l.Unlock()
l.expireEntries()
return l.cacheMemory
}
// Clear removes all mappings from the cache.
func (l *localCacheImpl[K, V]) Clear() {
l.Lock()
defer l.Unlock()
l.data = make(map[K]*localCacheEntry[K, V], 0)
l.updateCacheMemory(0)
}
// Release releases the cache.
func (l *localCacheImpl[K, V]) Release() {
l.Clear()
}
func (l *localCacheImpl[K, V]) GetStats() CacheStats {
return l
}
// expireEntries goes through the map to see if any entries have expired due to ttl.
func (l *localCacheImpl[K, V]) expireEntries() {
var (
prunes int64
keysToDelete = make([]K, 0)
start = time.Now()
)
// check for cache expiry
for k, v := range l.data {
if v.ttl > 0 && time.Since(v.insertTime) > v.ttl {
keysToDelete = append(keysToDelete, k)
prunes++
}
}
// delete all the keys that were flagged from the expiry, this may be enough to free up space
for _, k := range keysToDelete {
l.updateEntrySize(l.data[k], -1)
delete(l.data, k)
}
if len(keysToDelete) > 0 {
l.registerPruneNanos(time.Since(start).Nanoseconds())
}
}
// pruneEntries goes through the map to see if any entries have expired or size is reached and remove them.
func (l *localCacheImpl[K, V]) pruneEntries() {
var (
start = time.Now()
currentCacheSize = int64(len(l.data))
)
l.expireEntries()
// if highUnits or highUnitsMemory are set then check
if (l.options.HighUnits > 0 && currentCacheSize+1 > l.options.HighUnits) ||
(l.options.HighUnitsMemory > 0 && l.cacheMemory+1 > l.options.HighUnitsMemory) {
defer l.registerPruneNanos(time.Since(start).Nanoseconds())
entriesToDelete := int(math.Round(float64(currentCacheSize * prunePercent / 100.0)))
// prune to default of 80% of the cache size.
// we first sort the map by lastAccess time / then insert time, so we remove all
// entries firstly that have never been accessed.
index := 0
sortData := make(pairList[K], len(l.data))
for k, v := range l.data {
var timestamp = v.lastAccess
if timestamp.IsZero() {
// has not been accessed so set the timestamp to the insert time, so when we prune
// we will prune entries that are older first
timestamp = v.insertTime
}
sortData[index] = pair[K]{key: k, timeStamp: timestamp}
index++
}
sort.Sort(sortData)
for i, v := range sortData {
if i > entriesToDelete {
break
}
l.updateEntrySize(l.data[v.key], -1)
delete(l.data, v.key)
}
}
}
func newLocalCacheEntry[K comparable, V any](key K, value V, ttl time.Duration) *localCacheEntry[K, V] {
return &localCacheEntry[K, V]{
key: key,
value: value,
ttl: ttl,
insertTime: time.Now(),
}
}
func newLocalCache[K comparable, V any](name string, options ...func(localCache *localCacheOptions)) *localCacheImpl[K, V] {
cache := &localCacheImpl[K, V]{
Name: name,
data: make(map[K]*localCacheEntry[K, V], 0),
options: &localCacheOptions{
TTL: 0,
HighUnits: 0,
HighUnitsMemory: 0,
},
}
// apply any options
for _, f := range options {
f(cache.options)
}
return cache
}
// localCacheOptions defines options for a local cache.
type localCacheOptions struct {
TTL time.Duration
HighUnits int64
HighUnitsMemory int64
InvalidationStrategy InvalidationStrategyType
}
func (o *localCacheOptions) String() string {
return fmt.Sprintf("localCacheOptions{ttl=%v, highUnits=%v, highUnitsMemory=%v, invalidation=%v}",
o.TTL, o.HighUnits, formatMemory(o.HighUnitsMemory), getInvalidationStrategyString(o.InvalidationStrategy))
}
// withLocalCacheExpiry returns a function to set the expiry time for a local cache.
func withLocalCacheExpiry(ttl time.Duration) func(options *localCacheOptions) {
return func(o *localCacheOptions) {
o.TTL = ttl
}
}
// withInvalidationStrategy returns a function to set the invalidation strategy for a local cache.
func withInvalidationStrategy(strategy InvalidationStrategyType) func(options *localCacheOptions) {
return func(o *localCacheOptions) {
o.InvalidationStrategy = strategy
}
}
// withLocalCacheHighUnits returns a function to set the high units for a local cache.
func withLocalCacheHighUnits(highUnits int64) func(options *localCacheOptions) {
return func(o *localCacheOptions) {
o.HighUnits = highUnits
}
}
// withLocalCacheHighUnitsMemory returns a function to set the high units as memory for a local cache.
func withLocalCacheHighUnitsMemory(highUnitsMemory int64) func(options *localCacheOptions) {
return func(o *localCacheOptions) {
o.HighUnitsMemory = highUnitsMemory
}
}
func (l *localCacheImpl[K, V]) registerHit() {
atomic.AddInt64(&l.cacheHits, 1)
}
func (l *localCacheImpl[K, V]) registerMiss() {
atomic.AddInt64(&l.cacheMisses, 1)
}
func (l *localCacheImpl[K, V]) registerPut() {
atomic.AddInt64(&l.cachePuts, 1)
}
func (l *localCacheImpl[K, V]) updateCacheMemory(size int64) {
atomic.AddInt64(&l.cacheMemory, size)
}
func (l *localCacheImpl[K, V]) registerPruneNanos(nanos int64) {
atomic.AddInt64(&l.cachePrunes, 1)
atomic.AddInt64(&l.cachePrunesNannos, nanos)
}
func (l *localCacheImpl[K, V]) registerMissesNanos(nanos int64) {
atomic.AddInt64(&l.cacheMissesNannos, nanos)
}
func (l *localCacheImpl[K, V]) GetCacheHits() int64 {
return l.cacheHits
}
func (l *localCacheImpl[K, V]) GetCacheMisses() int64 {
return l.cacheMisses
}
func (l *localCacheImpl[K, V]) GetCacheMissesDuration() time.Duration {
return time.Duration(l.cacheMissesNannos) * time.Nanosecond
}
func (l *localCacheImpl[K, V]) GetCachePuts() int64 {
return l.cachePuts
}
func (l *localCacheImpl[K, V]) GetCachePrunes() int64 {
return l.cachePrunes
}
func (l *localCacheImpl[K, V]) GetCachePrunesDuration() time.Duration {
return time.Duration(l.cachePrunesNannos) * time.Nanosecond
}
func (l *localCacheImpl[K, V]) GetTotalGets() int64 {
return l.GetCacheHits() + l.GetCacheMisses()
}
func (l *localCacheImpl[K, V]) GetHitRate() float32 {
total := l.cacheHits + l.cacheMisses
if total == 0 {
return 0.0
}
return float32(l.cacheHits) / float32(total)
}
func (l *localCacheImpl[K, V]) ResetStats() {
atomic.StoreInt64(&l.cachePrunesNannos, 0)
atomic.StoreInt64(&l.cacheMissesNannos, 0)
atomic.StoreInt64(&l.cachePrunes, 0)
atomic.StoreInt64(&l.cacheHits, 0)
atomic.StoreInt64(&l.cacheMisses, 0)
atomic.StoreInt64(&l.cachePuts, 0)
}
func (l *localCacheImpl[K, V]) String() string {
return fmt.Sprintf("localCache{name=%s, options=%v, stats=CacheStats{puts=%v, gets=%v, hits=%v, misses=%v, "+
"missesDuration=%v, hitRate=%v, prunes=%v, prunesDuration=%v, size=%v, memoryUsed=%v}}",
l.Name, l.options, l.GetCachePuts(), l.GetTotalGets(), l.GetCacheHits(), l.GetCacheMisses(),
l.GetCacheMissesDuration(), l.GetHitRate()*100, l.GetCachePrunes(), l.GetCachePrunesDuration(), l.Size(), formatMemory(l.cacheMemory))
}
// updateEntrySize updates the cacheMemory size based upon a local entry. The sign indicates to either remove or add.
func (l *localCacheImpl[K, V]) updateEntrySize(entry *localCacheEntry[K, V], sign int) {
l.updateCacheMemory(int64(sign)*(int64(unsafe.Sizeof(entry.key))+int64(unsafe.Sizeof(entry.value))+
(int64(unsafe.Sizeof(entry.ttl)))+(int64(unsafe.Sizeof(entry.insertTime)))) + (int64(unsafe.Sizeof(entry.lastAccess))))
}
func formatMemory(bytesValue int64) string {
var printer = message.NewPrinter(language.English)
if bytesValue < KB {
return printer.Sprintf("%-1dB", bytesValue)
}
if bytesValue < MB {
return printer.Sprintf("%-.1fKB", float64(bytesValue)/1024)
}
if bytesValue < GB {
return printer.Sprintf("%-.1fMB", float64(bytesValue)/1024/1024)
}
return printer.Sprintf("%-.1fGB", float64(bytesValue)/1024/1024/1024)
}