Kind: global class
- InCache
- new InCache([opts])
- instance
- .load([path]) ⇒
Promise
- .save([path]) ⇒
Promise
- .setConfig([opts])
- .getConfig() ⇒
*
- .set(key, value, [opts]) ⇒
record
|*
- .get(key, [onlyValue]) ⇒
record
|*
|null
|undefined
- .info(key) ⇒
recordInfo
|*
|undefined
- .remove(key, [silent])
- .removeFrom(key, where)
- .removeExpired() ⇒
Array
- .addTo(key, value) ⇒
record
|undefined
- .prependTo(key, value) ⇒
record
|undefined
- .updateIn(key, value, where)
- .bulkSet(records, [silent]) ⇒
Object
|undefined
- .bulkRemove(keys, [silent])
- .clean(key)
- .all(asObject) ⇒
*
- .count() ⇒
Number
- .expired(key) ⇒
boolean
- .clear()
- .has(key) ⇒
boolean
- .destroy(...args)
- .stats() ⇒
Object
- .canBeAutoRemove(key) ⇒
boolean
|*
.onRemoved(callback).onCreated(callback).onUpdated(callback)- "beforeGet" (key, record)
- "get" (key, record)
- "beforeSet" (key, value)
- "set" (key, record)
- "create" (key, record)
- "update" (key, record)
- "beforeRemove" (key)
- "remove" (key)
- "beforeBulkSet" (records)
- "bulkSet" (records)
- "beforeBulkRemove" (keys)
- "bulkRemove" (keys)
- "expired" (keys)
- "beforeLoad" (me)
- "load" (err, me)
- "beforeSave" (me)
- "save" (err, me)
- "change" (by)
- "exceed" (diff)
- .load([path]) ⇒
- static
- .isRecord(obj) ⇒
boolean
- .isRecord(obj) ⇒
- inner
- ~record :
Object
- ~recordInfo :
Object
~removedCallback :function
~createdCallback :function
~updatedCallback :function
- ~record :
Create instance
Param | Type | Default | Description |
---|---|---|---|
[opts] | Object | configuration object |
|
[opts.maxAge] | number | 0 | max age in milliseconds. If 0 not expire. (overwritable by |
[opts.expires] | Date | string | a Date for expiration. (overwrites |
|
[opts.silent] | boolean | false | if true no event will be triggered. (overwritable by |
[opts.deleteOnExpires] | boolean | true | if false, the record will not be deleted after expiry. (overwritable by |
[opts.clone] | boolean | false | if true, the object will be cloned before to put it in storage. (overwritable by |
[opts.preserve] | boolean | false | if true, you will no longer be able to update the record once created. (overwritable by |
[opts.maxRecordNumber] | number | 0 | the maximum of record number of the cache, if exceeded some records will be deleted. If 0 is disabled |
[opts.autoLoad] | boolean | true | load cache from disk when instance is created. |
[opts.autoSave] | boolean | false | 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] | number | 5 | period in seconds to check for new changes to save on disk. Works only if |
[opts.filePath] | string | * | ".incache" | cache file path or key. If is a falsy value, |
[opts.storeName] | string | store name |
|
[opts.share] | boolean | false | if true, use global object as storage |
[opts.autoRemovePeriod] | number | 0 | 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] | boolean | false | calling |
[opts.save] | boolean | false | deprecated: if true saves cache in disk when the process is terminated. Use |
[opts.global] | Object | deprecated: global record configuration |
|
[opts.global.silent] | boolean | false | deprecated: if true no event will be triggered, use |
[opts.global.life] | number | 0 | deprecated: max age in seconds. If 0 not expire, use |
Load cache from disk
Kind: instance method of InCache
Emits: beforeLoad
, load
Since: 6.0.0
Param | Type | Default | Description |
---|---|---|---|
[path] | string | "opts.filePath" | file path or key (browser scenario) |
Save cache into disk
Kind: instance method of InCache
Emits: beforeSave
, save
Since: 6.0.0
Param | Type | Default | Description |
---|---|---|---|
[path] | string | "opts.filePath" | file path or key (browser scenario) |
Set configuration
Kind: instance method of InCache
See: constructor for further information
Since: 3.0.0
Param | Type | Description |
---|---|---|
[opts] | Object | configuration object |
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
Param | Type | Default | Description |
---|---|---|---|
key | string | ||
value | * | ||
[opts] | Object | options object |
|
[opts.silent] | boolean | false | if true no event will be triggered. (overwrites global configuration) |
[opts.maxAge] | number | 0 | max age in milliseconds. If 0 not expire. (overwrites global configuration) |
[opts.clone] | boolean | false | if true, the object will be cloned before to put it in storage. (overwrites global configuration) |
[opts.preserve] | boolean | false | 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.deleteOnExpires] | boolean | true | if false, the record will not be deleted after expiry. (overwrites global configuration) |
[opts.life] | number | 0 | 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
Param | Type | Default | Description |
---|---|---|---|
key | string | ||
[onlyValue] | boolean | true | 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
Param | Type |
---|---|
key | string |
Example
inCache.info('my key');
Delete a record
Kind: instance method of InCache
Emits: beforeRemove
, remove
Param | Type | Default | Description |
---|---|---|---|
key | string | ||
[silent] | boolean | false | if true no event will be triggered |
Example
inCache.remove('my key');
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
Param | Type |
---|---|
key | string |
where | * |
Example
inCache.set('myArray', ['hello', 'world']);
inCache.removeFrom('myArray', 'hello'); //-> ['world'];
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
Param | Type |
---|---|
key | string |
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
Param | Type |
---|---|
key | string |
value | * |
Example
inCache.set('myArray', ['hello', 'world']);
inCache.prependTo('myArray', 'ciao'); //-> ['ciao', 'hello', 'world'];
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
Param | Type |
---|---|
key | string |
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}];
Set/update multiple records. This method not trigger any event.
Kind: instance method of InCache
Emits: beforeBulkSet
, bulkSet
Param | Type | Default | Description |
---|---|---|---|
records | Array | e.g. [{key: foo1, value: bar1},{key: foo2, value: bar2}] |
|
[silent] | boolean | false | 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']);
Delete multiple records
Kind: instance method of InCache
Emits: beforeBulkRemove
, bulkRemove
Param | Type | Default | Description |
---|---|---|---|
keys | Array | an array of keys |
|
[silent] | boolean | false | if true no event will be triggered |
Example
inCache.bulkRemove(['key1', 'key2', 'key3']);
Delete multiple records that contain the passed keyword
Kind: instance method of InCache
Param | Type | Description |
---|---|---|
key | string | 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');
Fetch all records
Kind: instance method of InCache
Param | Default |
---|---|
asObject | false |
Returns total of records in storage
Kind: instance method of InCache
Since: 6.0.0
Check if record is expired
Kind: instance method of InCache
Param | Type |
---|---|
key | string |
Remove all records
Kind: instance method of InCache
Check if key exists
Kind: instance method of InCache
Param | Type |
---|---|
key | string |
Example
inCache.has('my key');
Alias of remove
Kind: instance method of InCache
Since: 4.1.1
Param |
---|
...args |
Returns stats of storage
Kind: instance method of InCache
Since: 6.3.0
Check if key can be auto removed
Kind: instance method of InCache
Param |
---|
key |
Deprecated
Triggered when a record has been deleted. Deprecated since 5.0.0: use on('remove', callback)
instead.
Kind: instance method of InCache
Param | Type | Description |
---|---|---|
callback | removedCallback | callback function |
Example
inCache.onRemoved((key)=>{
console.log('removed', key);
});
Deprecated
Triggered when a record has been created. Deprecated since 5.0.0: use on('create', callback)
instead
Kind: instance method of InCache
Param | Type | Description |
---|---|---|
callback | createdCallback | callback function |
Example
inCache.onCreated((key, record)=>{
console.log('created', key, record);
});
Deprecated
Triggered when a record has been updated. Deprecated since 5.0.0: use on('update', callback)
instead
Kind: instance method of InCache
Param | Type | Description |
---|---|---|
callback | updatedCallback | callback function |
Example
inCache.onUpdated((key, record)=>{
console.log('updated', key, record);
});
Triggered before get
Kind: event emitted by InCache
Since: 7.2.0
Param | Type | Description |
---|---|---|
key | string | key |
record | record | record object |
Triggered after get
Kind: event emitted by InCache
Since: 7.2.0
Param | Type | Description |
---|---|---|
key | string | key |
record | record | record object |
Triggered before set
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
key | string | key |
value | string | value |
Triggered after set
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
key | string | key |
record | record | record object |
Triggered after create the record
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
key | string | key of record |
record | record | record object |
Triggered after update the record
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
key | string | key of record |
record | record | record object |
Triggered before remove the record
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
key | string | key of record to be removed |
Triggered after record has been removed
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
key | string | key of record |
Triggered before bulk set
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
records | Array | array of objects |
Triggered after bulk set
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
records | Array | array of objects |
Triggered before remove the records
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
keys | Array | array of keys to be removed |
Triggered after records have been removed
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
keys | Array | array of keys removed |
Triggered when records are expired and opts.autoRemovePeriod
is set
Kind: event emitted by InCache
Since: 5.0.0
Param | Type | Description |
---|---|---|
keys | Array | array of keys expired |
Triggered before load (only if autoLoad
is false)
Kind: event emitted by InCache
Since: 6.4.0
Param | Type |
---|---|
me | InCache |
Triggered after load invocation
Kind: event emitted by InCache
Since: 6.0.0
Param | Type | Description |
---|---|---|
err | null | string | error message, if no errors occurred is null |
me | InCache |
Triggered before save
Kind: event emitted by InCache
Since: 6.4.0
Param | Type |
---|---|
me | InCache |
Triggered after save invocation
Kind: event emitted by InCache
Since: 6.0.0
Param | Type | Description |
---|---|---|
err | null | string | error message, if no errors occurred is null |
me | InCache |
Triggered when data is changed
Kind: event emitted by InCache
Since: 6.1.0
Param | Type | Description |
---|---|---|
by | string | event called by |
Triggered when data exceed max size
Kind: event emitted by InCache
Since: 6.1.0
Param | Type | Description |
---|---|---|
diff | number | exceeded by record number |
Check if object is a InCache~record
Kind: static method of InCache
Param | Type | Description |
---|---|---|
obj | record | InCache record |
InCache record
Kind: inner typedef of InCache
Properties
Name | Type | Description |
---|---|---|
id | string | uuid |
isNew | boolean | indicates if is a new record |
isPreserved | boolean | indicates if record will no longer be editable once created |
toDelete | boolean | indicates if record will be deleted after expiry |
hits | number | how many times it has been used |
lastHit | Date | null | last usage |
createdOn | Date | null | creation date |
updatedOn | Date | null | update date |
expiresOn | Date | null | expiry date |
value | * | record value |
InCache recordInfo
Kind: inner typedef of InCache
Properties
Name | Type | Description |
---|---|---|
id | string | uuid |
isNew | boolean | indicates if is a new record |
isPreserved | boolean | indicates if record will no longer be editable once created |
toDelete | boolean | indicates if record will be deleted after expiry |
hits | number | how many times it has been used |
lastHit | Date | null | last usage |
createdOn | Date | null | creation date |
updatedOn | Date | null | update date |
expiresOn | Date | null | expiry date |
Deprecated
onRemoved callback
Kind: inner typedef of InCache
Param | Type | Description |
---|---|---|
key | string | key of record removed |
Deprecated
onCreated callback
Kind: inner typedef of InCache
Param | Type | Description |
---|---|---|
key | string | key of record created |
record | record | record object |
Deprecated
onUpdated callback
Kind: inner typedef of InCache
Param | Type | Description |
---|---|---|
key | string | key of record updated |
record | record | record object |
Kind: global constant
Kind: static property of SAVE_MODE
Kind: static property of SAVE_MODE