-
Notifications
You must be signed in to change notification settings - Fork 140
/
node_cache.coffee
716 lines (635 loc) · 15.5 KB
/
node_cache.coffee
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
clone = require( "clone" )
EventEmitter = require('events').EventEmitter
# generate superclass
module.exports = class NodeCache extends EventEmitter
constructor: ( @options = {} )->
super()
@_initErrors()
# container for cached data
@data = {}
# module options
@options = Object.assign(
# convert all elements to string
forceString: false
# used standard size for calculating value size
objectValueSize: 80
promiseValueSize: 80
arrayValueSize: 40
# standard time to live in seconds. 0 = infinity;
stdTTL: 0
# time in seconds to check all data and delete expired keys
checkperiod: 600
# en/disable cloning of variables. If `true` you'll get a copy of the cached variable. If `false` you'll save and get just the reference
useClones: true
# whether values should be deleted automatically at expiration
deleteOnExpire: true
# enable legacy callbacks
enableLegacyCallbacks: false
# max amount of keys that are being stored
maxKeys: -1
, @options )
# generate functions with callbacks (legacy)
if (@options.enableLegacyCallbacks)
console.warn("WARNING! node-cache legacy callback support will drop in v6.x")
[
"get",
"mget",
"set",
"del",
"ttl",
"getTtl",
"keys",
"has"
].forEach((methodKey) =>
# reference real function
oldMethod = @[methodKey]
@[methodKey] = (args..., cb) ->
# return a callback if cb is defined and a function
if (typeof cb is "function")
try
res = oldMethod(args...)
cb(null, res)
catch err
cb(err)
else
return oldMethod(args..., cb)
return
return
)
# statistics container
@stats =
hits: 0
misses: 0
keys: 0
ksize: 0
vsize: 0
# pre allocate valid keytypes array
@validKeyTypes = ["string", "number"]
# initalize checking period
@_checkData()
return
# ## get
#
# get a cached key and change the stats
#
# **Parameters:**
#
# * `key` ( String | Number ): cache key
#
# **Example:**
#
# myCache.get "myKey", ( err, val )
#
get: ( key )=>
# handle invalid key types
if (err = @_isInvalidKey( key ))?
throw err
# get data and increment stats
if @data[ key ]? and @_check( key, @data[ key ] )
@stats.hits++
_ret = @_unwrap( @data[ key ] )
# return data
return _ret
else
# if not found return undefined
@stats.misses++
return undefined
# ## mget
#
# get multiple cached keys at once and change the stats
#
# **Parameters:**
#
# * `keys` ( String|Number[] ): an array of keys
#
# **Example:**
#
# myCache.mget [ "foo", "bar" ]
#
mget: ( keys )=>
# convert a string to an array of one key
if not Array.isArray( keys )
_err = @_error( "EKEYSTYPE" )
throw _err
# define return
oRet = {}
for key in keys
# handle invalid key types
if (err = @_isInvalidKey( key ))?
throw err
# get data and increment stats
if @data[ key ]? and @_check( key, @data[ key ] )
@stats.hits++
oRet[ key ] = @_unwrap( @data[ key ] )
else
# if not found return a error
@stats.misses++
# return all found keys
return oRet
# ## set
#
# set a cached key and change the stats
#
# **Parameters:**
#
# * `key` ( String | Number ): cache key
# * `value` ( Any ): An element to cache. If the option `option.forceString` is `true` the module trys to translate it to a serialized JSON
# * `[ ttl ]` ( Number | String ): ( optional ) The time to live in seconds.
#
# **Example:**
#
# myCache.set "myKey", "my_String Value"
#
# myCache.set "myKey", "my_String Value", 10
#
set: ( key, value, ttl )=>
# check if cache is overflowing
if (@options.maxKeys > -1 && @stats.keys >= @options.maxKeys)
_err = @_error( "ECACHEFULL" )
throw _err
# force the data to string
if @options.forceString and typeof value isnt "string"
value = JSON.stringify( value )
# set default ttl if not passed
unless ttl?
ttl = @options.stdTTL
# handle invalid key types
if (err = @_isInvalidKey( key ))?
throw err
# internal helper variables
existent = false
# remove existing data from stats
if @data[ key ]
existent = true
@stats.vsize -= @_getValLength( @_unwrap( @data[ key ], false ) )
# set the value
@data[ key ] = @_wrap( value, ttl )
@stats.vsize += @_getValLength( value )
# only add the keys and key-size if the key is new
if not existent
@stats.ksize += @_getKeyLength( key )
@stats.keys++
@emit( "set", key, value )
# return true
return true
# ## fetch
#
# in the event of a cache miss (no value is assinged to given cache key), value will be written to cache and returned. In case of cache hit, cached value will be returned without executing given value. If the given value is type of `Function`, it will be executed and returned result will be fetched
#
# **Parameters:**
#
# * `key` ( String | Number ): cache key
# * `[ ttl ]` ( Number | String ): ( optional ) The time to live in seconds.
# * `value` ( Any ): if `Function` type is given, it will be executed and returned value will be fetched, otherwise the value itself is fetched
#
# **Example:**
#
# myCache.fetch "myKey", 10, () => "my_String value"
#
# myCache.fetch "myKey", "my_String value"
#
fetch: ( key, ttl, value )=>
# check if cache is hit
if @has( key )
return @get( key )
if typeof value == 'undefined'
value = ttl
ttl = undefined
_ret = if typeof value == 'function' then value() else value
@set( key, _ret, ttl )
return _ret
# ## mset
#
# set multiple keys at once
#
# **Parameters:**
#
# * `keyValueSet` ( Object[] ): an array of objects which include key, value, and ttl
#
# **Example:**
#
# myCache.mset(
# [
# {
# key: "myKey",
# val: "myValue",
# ttl: [ttl in seconds]
# }
# ])
#
#
mset: ( keyValueSet ) =>
# check if cache is overflowing
if (@options.maxKeys > -1 && @stats.keys + keyValueSet.length >= @options.maxKeys)
_err = @_error( "ECACHEFULL" )
throw _err
# loop over keyValueSet to validate key and ttl
for keyValuePair in keyValueSet
{ key, val, ttl } = keyValuePair
# check if there is ttl and it's a number
if ttl and typeof ttl isnt "number"
_err = @_error( "ETTLTYPE" )
throw _err
# handle invalid key types
if (err = @_isInvalidKey( key ))?
throw err
for keyValuePair in keyValueSet
{ key, val, ttl } = keyValuePair
@set(key, val, ttl)
return true
# ## del
#
# remove keys
#
# **Parameters:**
#
# * `keys` ( String | Number | String|Number[] ): cache key to delete or an array of cache keys
#
# **Return**
#
# ( Number ): Number of deleted keys
#
# **Example:**
#
# myCache.del( "myKey" )
#
del: ( keys )=>
# convert keys to an array of itself
if not Array.isArray( keys )
keys = [ keys ]
delCount = 0
for key in keys
# handle invalid key types
if (err = @_isInvalidKey( key ))?
throw err
# only delete if existent
if @data[ key ]?
# calc the stats
@stats.vsize -= @_getValLength( @_unwrap( @data[ key ], false ) )
@stats.ksize -= @_getKeyLength( key )
@stats.keys--
delCount++
# delete the value
oldVal = @data[ key ]
delete @data[ key ]
# return true
@emit( "del", key, oldVal.v )
return delCount
# ## take
#
# get the cached value and remove the key from the cache.
# Equivalent to calling `get(key)` + `del(key)`.
# Useful for implementing `single use` mechanism such as OTP, where once a value is read it will become obsolete.
#
# **Parameters:**
#
# * `key` ( String | Number ): cache key
#
# **Example:**
#
# myCache.take "myKey", ( err, val )
#
take: ( key )=>
_ret = @get(key)
if (_ret?)
@del(key)
return _ret
# ## ttl
#
# reset or redefine the ttl of a key. `ttl` = 0 means infinite lifetime.
# If `ttl` is not passed the default ttl is used.
# If `ttl` < 0 the key will be deleted.
#
# **Parameters:**
#
# * `key` ( String | Number ): cache key to reset the ttl value
# * `ttl` ( Number ): ( optional -> options.stdTTL || 0 ) The time to live in seconds
#
# **Return**
#
# ( Boolen ): key found and ttl set
#
# **Example:**
#
# myCache.ttl( "myKey" ) // will set ttl to default ttl
#
# myCache.ttl( "myKey", 1000 )
#
ttl: (key, ttl) =>
ttl or= @options.stdTTL
if not key
return false
# handle invalid key types
if (err = @_isInvalidKey( key ))?
throw err
# check for existent data and update the ttl value
if @data[ key ]? and @_check( key, @data[ key ] )
# if ttl < 0 delete the key. otherwise reset the value
if ttl >= 0
@data[ key ] = @_wrap( @data[ key ].v, ttl, false )
else
@del( key )
return true
else
# return false if key has not been found
return false
return
# ## getTtl
#
# receive the ttl of a key.
#
# **Parameters:**
#
# * `key` ( String | Number ): cache key to check the ttl value of
#
# **Return**
#
# ( Number|undefined ): The timestamp in ms when the key will expire, 0 if it will never expire or undefined if it not exists
#
# **Example:**
#
# myCache.getTtl( "myKey" )
#
getTtl: ( key )=>
if not key
return undefined
# handle invalid key types
if (err = @_isInvalidKey( key ))?
throw err
# check for existant data and update the ttl value
if @data[ key ]? and @_check( key, @data[ key ] )
_ttl = @data[ key ].t
return _ttl
else
# return undefined if key has not been found
return undefined
return
# ## keys
#
# list all keys within this cache
#
# **Return**
#
# ( Array ): An array of all keys
#
# **Example:**
#
# _keys = myCache.keys()
#
# # [ "foo", "bar", "fizz", "buzz", "anotherKeys" ]
#
keys: ( )=>
_keys = Object.keys( @data )
return _keys
# ## has
#
# Check if a key is cached
#
# **Parameters:**
#
# * `key` ( String | Number ): cache key to check the ttl value
#
# **Return**
#
# ( Boolean ): A boolean that indicates if the key is cached
#
# **Example:**
#
# _exists = myCache.has('myKey')
#
# # true
#
has: ( key )=>
_exists = @data[ key ]? and @_check( key, @data[ key ] )
return _exists
# ## getStats
#
# get the stats
#
# **Parameters:**
#
# -
#
# **Return**
#
# ( Object ): Stats data
#
# **Example:**
#
# myCache.getStats()
# # {
# # hits: 0,
# # misses: 0,
# # keys: 0,
# # ksize: 0,
# # vsize: 0
# # }
#
getStats: =>
@stats
# ## flushAll
#
# flush the whole data and reset the stats
#
# **Example:**
#
# myCache.flushAll()
#
# myCache.getStats()
# # {
# # hits: 0,
# # misses: 0,
# # keys: 0,
# # ksize: 0,
# # vsize: 0
# # }
#
flushAll: ( _startPeriod = true )=>
# parameter just for testing
# set data empty
@data = {}
# reset stats
@stats =
hits: 0
misses: 0
keys: 0
ksize: 0
vsize: 0
# reset check period
@_killCheckPeriod()
@_checkData( _startPeriod )
@emit( "flush" )
return
# ## flushStats
#
# flush the stats and reset all counters to 0
#
# **Example:**
#
# myCache.flushStats()
#
# myCache.getStats()
# # {
# # hits: 0,
# # misses: 0,
# # keys: 0,
# # ksize: 0,
# # vsize: 0
# # }
#
flushStats: ()=>
# reset stats
@stats =
hits: 0
misses: 0
keys: 0
ksize: 0
vsize: 0
@emit( "flush_stats" )
return
# ## close
#
# This will clear the interval timeout which is set on checkperiod option.
#
# **Example:**
#
# myCache.close()
#
close: =>
@_killCheckPeriod()
return
# ## _checkData
#
# internal housekeeping method.
# Check all the cached data and delete the invalid values
_checkData: ( startPeriod = true )=>
# run the housekeeping method
for key, value of @data
@_check( key, value )
if startPeriod and @options.checkperiod > 0
@checkTimeout = setTimeout( @_checkData, ( @options.checkperiod * 1000 ), startPeriod )
@checkTimeout.unref() if @checkTimeout? && @checkTimeout.unref?
return
# ## _killCheckPeriod
#
# stop the checkdata period. Only needed to abort the script in testing mode.
_killCheckPeriod: ->
clearTimeout( @checkTimeout ) if @checkTimeout?
# ## _check
#
# internal method the check the value. If it's not valid any more delete it
_check: ( key, data )=>
_retval = true
# data is invalid if the ttl is too old and is not 0
# console.log data.t < Date.now(), data.t, Date.now()
if data.t isnt 0 and data.t < Date.now()
if @options.deleteOnExpire
_retval = false
@del( key )
@emit( "expired", key, @_unwrap(data) )
return _retval
# ## _isInvalidKey
#
# internal method to check if the type of a key is either `number` or `string`
_isInvalidKey: ( key )=>
unless typeof key in @validKeyTypes
return @_error( "EKEYTYPE", { type: typeof key })
return
# ## _wrap
#
# internal method to wrap a value in an object with some metadata
_wrap: ( value, ttl, asClone = true )=>
if not @options.useClones
asClone = false
# define the time to live
now = Date.now()
livetime = 0
ttlMultiplicator = 1000
# use given ttl
if ttl is 0
livetime = 0
else if ttl
livetime = now + ( ttl * ttlMultiplicator )
else
# use standard ttl
if @options.stdTTL is 0
livetime = @options.stdTTL
else
livetime = now + ( @options.stdTTL * ttlMultiplicator )
# return the wrapped value
oReturn =
t: livetime
v: if asClone then clone( value ) else value
# ## _unwrap
#
# internal method to extract get the value out of the wrapped value
_unwrap: ( value, asClone = true )->
if not @options.useClones
asClone = false
if value.v?
if asClone
return clone( value.v )
else
return value.v
return null
# ## _getKeyLength
#
# internal method the calculate the key length
_getKeyLength: ( key )->
key.toString().length
# ## _getValLength
#
# internal method to calculate the value length
_getValLength: ( value )=>
if typeof value is "string"
# if the value is a String get the real length
value.length
else if @options.forceString
# force string if it's defined and not passed
JSON.stringify( value ).length
else if Array.isArray( value )
# if the data is an Array multiply each element with a defined default length
@options.arrayValueSize * value.length
else if typeof value is "number"
8
else if typeof value?.then is "function"
# if the data is a Promise, use defined default
# (can't calculate actual/resolved value size synchronously)
@options.promiseValueSize
else if Buffer?.isBuffer(value)
value.length
else if value? and typeof value is "object"
# if the data is an Object multiply each element with a defined default length
@options.objectValueSize * Object.keys( value ).length
else if typeof value is "boolean"
8
else
# default fallback
0
# ## _error
#
# internal method to handle an error message
_error: ( type, data = {} )=>
# generate the error object
error = new Error()
error.name = type
error.errorcode = type
error.message = if @ERRORS[ type ]? then @ERRORS[ type ]( data ) else "-"
error.data = data
# return the error object
return error
# ## _initErrors
#
# internal method to generate error message templates
_initErrors: =>
@ERRORS = {}
for _errT, _errMsg of @_ERRORS
@ERRORS[ _errT ] = @createErrorMessage( _errMsg )
return
createErrorMessage: (errMsg) -> (args) -> errMsg.replace("__key", args.type)
_ERRORS:
"ENOTFOUND": "Key `__key` not found"
"ECACHEFULL": "Cache max keys amount exceeded"
"EKEYTYPE": "The key argument has to be of type `string` or `number`. Found: `__key`"
"EKEYSTYPE": "The keys argument has to be an array."
"ETTLTYPE": "The ttl argument has to be a number."