Skip to content

Latest commit

 

History

History
1357 lines (1182 loc) · 37.2 KB

api.md

File metadata and controls

1357 lines (1182 loc) · 37.2 KB

Classes

InCache

Constants

SAVE_MODE

InCache

Kind: global class

new InCache([opts])

Create instance

ParamTypeDefaultDescription
[opts]Object

configuration object

[opts.maxAge]number0

max age in milliseconds. If 0 not expire. (overwritable by set)

[opts.expires]Date | string

a Date for expiration. (overwrites opts.maxAge, overwritable by set)

[opts.silent]booleanfalse

if true no event will be triggered. (overwritable by set)

[opts.deleteOnExpires]booleantrue

if false, the record will not be deleted after expiry. (overwritable by set)

[opts.clone]booleanfalse

if true, the object will be cloned before to put it in storage. (overwritable by set)

[opts.preserve]booleanfalse

if true, you will no longer be able to update the record once created. (overwritable by set)

[opts.maxRecordNumber]number0

the maximum of record number of the cache, if exceeded some records will be deleted. If 0 is disabled

[opts.autoLoad]booleantrue

load cache from disk when instance is created.

[opts.autoSave]booleanfalse

if true saves cache in disk when the process is terminated.

[opts.autoSaveMode]string"terminate"

there are 2 modes -> "terminate": saves before the process is terminated (server only). "timer": every n seconds checks for new changes and save on disk.

[opts.autoSavePeriod]number5

period in seconds to check for new changes to save on disk. Works only if opts.autoSaveMode is set to 'timer' mode.

[opts.filePath]string | *".incache"

cache file path or key. If is a falsy value, load and save will always be solved

[opts.storeName]string

store name

[opts.share]booleanfalse

if true, use global object as storage

[opts.autoRemovePeriod]number0

period in seconds to remove expired records. When set, the records will be removed only on check, when 0 it won't run

[opts.nullIfNotFound]booleanfalse

calling get if the key is not found returns null. If false returns undefined

[opts.save]booleanfalse

deprecated: if true saves cache in disk when the process is terminated. Use autoSave instead.

[opts.global]Object

deprecated: global record configuration

[opts.global.silent]booleanfalse

deprecated: if true no event will be triggered, use silent instead

[opts.global.life]number0

deprecated: max age in seconds. If 0 not expire, use maxAge instead

inCache.load([path]) ⇒ Promise

Load cache from disk

Kind: instance method of InCache
Emits: beforeLoad, load
Since: 6.0.0

ParamTypeDefaultDescription
[path]string"opts.filePath"

file path or key (browser scenario)

inCache.save([path]) ⇒ Promise

Save cache into disk

Kind: instance method of InCache
Emits: beforeSave, save
Since: 6.0.0

ParamTypeDefaultDescription
[path]string"opts.filePath"

file path or key (browser scenario)

inCache.setConfig([opts])

Set configuration

Kind: instance method of InCache
See: constructor for further information
Since: 3.0.0

ParamTypeDescription
[opts]Object

configuration object

inCache.getConfig() ⇒ *

Get configuration

Kind: instance method of InCache

inCache.set(key, value, [opts]) ⇒ record | *

Set/update record

Kind: instance method of InCache
Emits: beforeSet, create, update, set

ParamTypeDefaultDescription
keystring
value*
[opts]Object

options object

[opts.silent]booleanfalse

if true no event will be triggered. (overwrites global configuration)

[opts.maxAge]number0

max age in milliseconds. If 0 not expire. (overwrites global configuration)

[opts.clone]booleanfalse

if true, the object will be cloned before to put it in storage. (overwrites global configuration)

[opts.preserve]booleanfalse

if true, you will no longer be able to update the record once created. (overwrites global configuration)

[opts.expires]Date | string

a Date for expiration. (overwrites global configuration and opts.maxAge)

[opts.deleteOnExpires]booleantrue

if false, the record will not be deleted after expiry. (overwrites global configuration)

[opts.life]number0

deprecated: max age in seconds. If 0 not expire. (overwrites global configuration)

Example

inCache.set('my key', 'my value');
inCache.set('my object', {a: 1, b: 2});
inCache.set('my boolean', true, {maxAge: 2000}); // Expires after 2 seconds

inCache.get(key, [onlyValue]) ⇒ record | * | null | undefined

Get record by key

Kind: instance method of InCache
Emits: get, beforeGet

ParamTypeDefaultDescription
keystring
[onlyValue]booleantrue

if false get InCache record

Example

inCache.get('my key');

inCache.info(key) ⇒ recordInfo | * | undefined

Get info record by key

Kind: instance method of InCache
Since: 7.1.0

ParamType
keystring

Example

inCache.info('my key');

inCache.remove(key, [silent])

Delete a record

Kind: instance method of InCache
Emits: beforeRemove, remove

ParamTypeDefaultDescription
keystring
[silent]booleanfalse

if true no event will be triggered

Example

inCache.remove('my key');

inCache.removeFrom(key, where)

Given a key that has value like an array removes key(s) if where is satisfied

Kind: instance method of InCache
Since: 3.0.0

ParamType
keystring
where*

Example

inCache.set('myArray', ['hello', 'world']);
inCache.removeFrom('myArray', 'hello'); //-> ['world'];

inCache.removeExpired() ⇒ Array

Remove expired records

Kind: instance method of InCache
Returns: Array - expired keys
Since: 4.1.0
Example

inCache.set('my key 1', 'my value');
inCache.set('my key 2', 'my value', {maxAge: 1000});
inCache.set('my key 3', 'my value', {maxAge: 1500});
setTimeout(()=>{
     inCache.removeExpired();
     inCache.all(); //-> [{key: 'my key 1', value: 'my value'}]
}, 2000)

inCache.addTo(key, value) ⇒ record | undefined

Given a key that has value like an array adds value to end of array

Kind: instance method of InCache
Since: 3.0.0

ParamType
keystring
value*

Example

inCache.set('myArray', ['hello', 'world']);
inCache.addTo('myArray', 'ciao'); //-> ['hello', 'world', 'ciao'];

inCache.prependTo(key, value) ⇒ record | undefined

Given a key that has value like an array adds value to beginning of array

Kind: instance method of InCache
Since: 3.0.0

ParamType
keystring
value*

Example

inCache.set('myArray', ['hello', 'world']);
inCache.prependTo('myArray', 'ciao'); //-> ['ciao', 'hello', 'world'];

inCache.updateIn(key, value, where)

Given a key that has value like an array updates key(s) if where is satisfied

Kind: instance method of InCache
Since: 3.0.0

ParamType
keystring
value*
where*

Example

inCache.set('myArray', ['hello', 'world']);
inCache.updateIn('myArray', 'ciao', 'hello'); //-> ['ciao', 'world'];

inCache.set('myArray', [{a: 1, b: 2, c: 3], {b: 2, c: 3}, {b: 4, e: 5});
inCache.updateIn('myArray', {z: 0, x: 0}, {b: 2, c: 3}); //-> [{z: 0, x: 0}, {z: 0, x: 0}, {b: 4, e: 5}];

inCache.bulkSet(records, [silent]) ⇒ Object | undefined

Set/update multiple records. This method not trigger any event.

Kind: instance method of InCache
Emits: beforeBulkSet, bulkSet

ParamTypeDefaultDescription
recordsArray

e.g. [{key: foo1, value: bar1},{key: foo2, value: bar2}]

[silent]booleanfalse

if true no event will be triggered

Example

inCache.bulkSet([
     {key: 'my key 1', value: 'my value 1'},
     {key: 'my key 2', value: 'my value 2'},
     {key: 'my key 3', value: 'my value 3'},
     {key: 'my key 4', value: 'my value 4'}
]);
// or
inCache.bulkSet(['hello','world']);

inCache.bulkRemove(keys, [silent])

Delete multiple records

Kind: instance method of InCache
Emits: beforeBulkRemove, bulkRemove

ParamTypeDefaultDescription
keysArray

an array of keys

[silent]booleanfalse

if true no event will be triggered

Example

inCache.bulkRemove(['key1', 'key2', 'key3']);

inCache.clean(key)

Delete multiple records that contain the passed keyword

Kind: instance method of InCache

ParamTypeDescription
keystring

a string that is relative to a group of keys

Example

inCache.set('/api/users/foo', 'Mario Rossi');
inCache.set('/api/users/bar', 'Antonio Bianchi');
inCache.clean('/api/users');

inCache.all(asObject) ⇒ *

Fetch all records

Kind: instance method of InCache

ParamDefault
asObjectfalse

inCache.count() ⇒ Number

Returns total of records in storage

Kind: instance method of InCache
Since: 6.0.0

inCache.expired(key) ⇒ boolean

Check if record is expired

Kind: instance method of InCache

ParamType
keystring

inCache.clear()

Remove all records

Kind: instance method of InCache

inCache.has(key) ⇒ boolean

Check if key exists

Kind: instance method of InCache

ParamType
keystring

Example

inCache.has('my key');

inCache.destroy(...args)

Alias of remove

Kind: instance method of InCache
Since: 4.1.1

Param
...args

inCache.stats() ⇒ Object

Returns stats of storage

Kind: instance method of InCache
Since: 6.3.0

inCache.canBeAutoRemove(key) ⇒ boolean | *

Check if key can be auto removed

Kind: instance method of InCache

Param
key

inCache.onRemoved(callback)

Deprecated

Triggered when a record has been deleted. Deprecated since 5.0.0: use on('remove', callback) instead.

Kind: instance method of InCache

ParamTypeDescription
callbackremovedCallback

callback function

Example

inCache.onRemoved((key)=>{
     console.log('removed', key);
});

inCache.onCreated(callback)

Deprecated

Triggered when a record has been created. Deprecated since 5.0.0: use on('create', callback) instead

Kind: instance method of InCache

ParamTypeDescription
callbackcreatedCallback

callback function

Example

inCache.onCreated((key, record)=>{
     console.log('created', key, record);
});

inCache.onUpdated(callback)

Deprecated

Triggered when a record has been updated. Deprecated since 5.0.0: use on('update', callback) instead

Kind: instance method of InCache

ParamTypeDescription
callbackupdatedCallback

callback function

Example

inCache.onUpdated((key, record)=>{
     console.log('updated', key, record);
});

"beforeGet" (key, record)

Triggered before get

Kind: event emitted by InCache
Since: 7.2.0

ParamTypeDescription
keystring

key

recordrecord

record object

"get" (key, record)

Triggered after get

Kind: event emitted by InCache
Since: 7.2.0

ParamTypeDescription
keystring

key

recordrecord

record object

"beforeSet" (key, value)

Triggered before set

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
keystring

key

valuestring

value

"set" (key, record)

Triggered after set

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
keystring

key

recordrecord

record object

"create" (key, record)

Triggered after create the record

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
keystring

key of record

recordrecord

record object

"update" (key, record)

Triggered after update the record

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
keystring

key of record

recordrecord

record object

"beforeRemove" (key)

Triggered before remove the record

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
keystring

key of record to be removed

"remove" (key)

Triggered after record has been removed

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
keystring

key of record

"beforeBulkSet" (records)

Triggered before bulk set

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
recordsArray

array of objects

"bulkSet" (records)

Triggered after bulk set

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
recordsArray

array of objects

"beforeBulkRemove" (keys)

Triggered before remove the records

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
keysArray

array of keys to be removed

"bulkRemove" (keys)

Triggered after records have been removed

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
keysArray

array of keys removed

"expired" (keys)

Triggered when records are expired and opts.autoRemovePeriod is set

Kind: event emitted by InCache
Since: 5.0.0

ParamTypeDescription
keysArray

array of keys expired

"beforeLoad" (me)

Triggered before load (only if autoLoad is false)

Kind: event emitted by InCache
Since: 6.4.0

ParamType
meInCache

"load" (err, me)

Triggered after load invocation

Kind: event emitted by InCache
Since: 6.0.0

ParamTypeDescription
errnull | string

error message, if no errors occurred is null

meInCache

"beforeSave" (me)

Triggered before save

Kind: event emitted by InCache
Since: 6.4.0

ParamType
meInCache

"save" (err, me)

Triggered after save invocation

Kind: event emitted by InCache
Since: 6.0.0

ParamTypeDescription
errnull | string

error message, if no errors occurred is null

meInCache

"change" (by)

Triggered when data is changed

Kind: event emitted by InCache
Since: 6.1.0

ParamTypeDescription
bystring

event called by set,remove,clear or clean

"exceed" (diff)

Triggered when data exceed max size

Kind: event emitted by InCache
Since: 6.1.0

ParamTypeDescription
diffnumber

exceeded by record number

InCache.isRecord(obj) ⇒ boolean

Check if object is a InCache~record

Kind: static method of InCache

ParamTypeDescription
objrecord

InCache record

InCache~record : Object

InCache record

Kind: inner typedef of InCache
Properties

NameTypeDescription
idstring

uuid

isNewboolean

indicates if is a new record

isPreservedboolean

indicates if record will no longer be editable once created

toDeleteboolean

indicates if record will be deleted after expiry

hitsnumber

how many times it has been used

lastHitDate | null

last usage

createdOnDate | null

creation date

updatedOnDate | null

update date

expiresOnDate | null

expiry date

value*

record value

InCache~recordInfo : Object

InCache recordInfo

Kind: inner typedef of InCache
Properties

NameTypeDescription
idstring

uuid

isNewboolean

indicates if is a new record

isPreservedboolean

indicates if record will no longer be editable once created

toDeleteboolean

indicates if record will be deleted after expiry

hitsnumber

how many times it has been used

lastHitDate | null

last usage

createdOnDate | null

creation date

updatedOnDate | null

update date

expiresOnDate | null

expiry date

InCache~removedCallback : function

Deprecated

onRemoved callback

Kind: inner typedef of InCache

ParamTypeDescription
keystring

key of record removed

InCache~createdCallback : function

Deprecated

onCreated callback

Kind: inner typedef of InCache

ParamTypeDescription
keystring

key of record created

recordrecord

record object

InCache~updatedCallback : function

Deprecated

onUpdated callback

Kind: inner typedef of InCache

ParamTypeDescription
keystring

key of record updated

recordrecord

record object

SAVE_MODE

Kind: global constant

SAVE_MODE.TERMINATE

Kind: static property of SAVE_MODE

SAVE_MODE.TIMER

Kind: static property of SAVE_MODE