new Cache(id, optionsopt)
Instances of this class represent a "cache"—a synchronous key-value store. Each instance holds the settings for the cache, and provides methods for manipulating the cache and its data.
Generally you don't creates instances of Cache
directly, but instead create
instances of Cache
via CacheFactory#createCache.
Parameters:
Name | Type | Attributes | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
id |
string | A unique identifier for the cache. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options |
object |
<optional> |
Configuration options. Properties
|
Example
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const options = {...};
const cache = cacheFactory.createCache('my-cache', options);
cache.put('foo', 'bar');
console.log(cache.get('foo')); // "bar"
Members
(readonly) cacheFlushInterval :number|null
The interval (in milliseconds) on which the cache should remove all of
its items. Setting this to null
disables the interval. The default is
null
.
Type:
- number | null
Example
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
cacheFlushInterval: 15 * 60 * 1000
});
(readonly) capacity :number
The maximum number of items that can be stored in the cache. When the
capacity is exceeded the least recently accessed item will be removed.
The default is Number.MAX_VALUE
.
Type:
- number
Example
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
capacity: 100
});
(readonly) deleteOnExpire :string
Determines the behavior of a cache when an item expires. The default is
"none"
.
Possible values:
"none"
- Cache will do nothing when an item expires."passive"
- Cache will do nothing when an item expires. Expired items will remain in the cache until requested, at which point they are removed, andundefined
is returned."aggressive"
- Cache will remove expired items as soon as they are discovered.
Type:
- string
Examples
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
deleteOnExpire: 'aggressive'
});
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
deleteOnExpire: 'passive'
});
(readonly) enabled :boolean
Marks whether the cache is enabled or not. For a disabled cache,
Cache#put is a no-op. The default is true
.
Type:
- boolean
Example
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
enabled: false
});
// The cache is disabled, this is a no-op
cache.put('foo', 'bar');
console.log(cache.get('foo')); // undefined
(readonly) id :string
Then unique identifier given to this cache when it was created.
Type:
- string
(readonly) maxAge :number
Represents how long an item can be in the cache before expires. The
cache's behavior toward expired items is determined by
Cache#deleteOnExpire. The default value for maxAge
is
Number.MAX_VALUE
.
Type:
- number
Example
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
// Items expire after 15 minutes
maxAge: 15 * 60 * 1000
});
const cache2 = cacheFactory.createCache('my-cache2', {
// Items expire after 15 minutes
maxAge: 15 * 60 * 1000,
// Expired items will only be deleted once they are accessed
deleteOnExpire: 'passive'
});
const cache3 = cacheFactory.createCache('my-cache3', {
// Items expire after 15 minutes
maxAge: 15 * 60 * 1000,
// Items will be deleted from the cache as soon as they expire
deleteOnExpire: 'aggressive'
});
(readonly) onExpire :function
A callback to be executed when expired items are removed from the
cache when the cache is in "passive"
or "aggressive"
mode. The
default is null
. See Cache~onExpireCallback for the signature
of the onExpire
callback.
Type:
- function
- Default Value:
- null
- Source:
- See:
Example
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
// Items expire after 15 minutes
maxAge: 15 * 60 * 1000,
// Expired items will only be deleted once they are accessed
deleteOnExpire: 'passive',
// Try to rehydrate cached items as they expire
onExpire: function (key, value, done) {
// Do something with key and value
// Will received "done" callback if in "passive" mode and passing
// an onExpire option to Cache#get.
if (done) {
done(); // You can pass whatever you want to done
}
}
});
(readonly) recycleFreq :number|null
The frequency (in milliseconds) with which the cache should check for
expired items. The default is 1000
. The value of this interval only
matters if Cache#deleteOnExpire is set to "aggressive"
.
Type:
- number | null
Example
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
// Items expire after 15 minutes
maxAge: 15 * 60 * 1000,
// Items will be deleted from the cache as soon as they expire
deleteOnExpire: 'aggressive',
// Check for expired items every 10 seconds
recycleFreq: 10 * 1000
});
(readonly) storageMode :string
Determines the storage medium used by the cache. The default is
"memory"
.
Possible values:
"memory"
"localStorage"
"sessionStorage"
Type:
- string
Examples
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
storageMode: 'localStorage'
});
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
storageMode: 'localStorage',
storageImpl: {
setItem: function (key, value) {
console.log('setItem', key, value);
localStorage.setItem(key, value);
},
getItem: function (key) {
console.log('getItem', key);
localStorage.getItem(key);
},
removeItem: function (key) {
console.log('removeItem', key);
localStorage.removeItem(key);
}
}
});
(readonly) storagePrefix :string
The prefix used to namespace the keys for items stored in
localStorage
or sessionStorage
. The default is
"cachefactory.caches."
which is conservatively long in order any
possible conflict with other data in storage. Set to a shorter value
to save storage space.
Type:
- string
Example
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
storageMode: 'localStorage',
// Completely remove the prefix to save the most space
storagePrefix: ''
});
cache.put('foo', 'bar');
console.log(localStorage.get('my-cache.data.foo')); // "bar"
(readonly) storeOnReject :boolean
If set to true
, when a promise is inserted into the cache and is then
rejected, then the rejection value will overwrite the promise in the
cache. The default is false
.
Type:
- boolean
(readonly) storeOnResolve :boolean
If set to true
, when a promise is inserted into the cache and is then
resolved, then the resolution value will overwrite the promise in the
cache. The default is false
.
Type:
- boolean
(inner) StorageImpl :object
Provide a custom storage medium, e.g. a polyfill for localStorage
. Default: null
.
Must implement:
setItem
- Same API aslocalStorage.setItem(key, value)
getItem
- Same API aslocalStorage.getItem(key)
removeItem
- Same API aslocalStorage.removeItem(key)
Type:
- object
Properties:
Name | Type | Description |
---|---|---|
setItem |
function | Implementation of |
getItem |
function | Implementation of |
removeItem |
function | Implementation of |
Methods
destroy()
Destroys this cache and all its data and renders it unusable.
Example
cache.destroy();
disable()
Disables this cache. For a disabled cache, Cache#put is a no-op.
Example
cache.disable();
enable()
Enables this cache. For a disabled cache, Cache#put is a no-op.
Example
cache.enable();
get(key, optionsopt) → {*}
Retrieve an item from the cache, it it exists.
Parameters:
Name | Type | Attributes | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
key |
string | Array.<string> | The key of the item to retrieve. |
|||||||||
options |
object |
<optional> |
Configuration options. Properties
|
Returns:
The value for the specified key
, if any.
- Type
- *
Examples
cache.put('foo', 'bar');
cache.get('foo'); // "bar"
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
deleteOnExpire: 'passive',
maxAge: 15 * 60 * 1000
});
cache.get('foo', {
// Called if "foo" is expired
onExpire: function (key, value) {
// Do something with key and value
}
});
import CacheFactory from 'cachefactory';
const cacheFactory = new CacheFactory();
const cache = cacheFactory.createCache('my-cache', {
deleteOnExpire: 'passive',
maxAge: 15 * 60 * 1000
onExpire: function (key, value, done) {
console.log('Expired item:', key);
if (done) {
done('foo', key, value);
}
}
});
cache.get('foo', {
// Called if "foo" is expired
onExpire: function (msg, key, value) {
console.log(msg); // "foo"
// Do something with key and value
}
});
info(keyopt) → {*}
Retrieve information about the whole cache or about a particular item in the cache.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string |
<optional> |
If specified, retrieve info for a particular item in the cache. |
Returns:
The information object.
- Type
- *
Examples
const info = cache.info();
info.id; // "my-cache"
info.capacity; // 100
info.maxAge; // 600000
info.deleteOnExpire; // "aggressive"
info.cacheFlushInterval; // null
info.recycleFreq; // 10000
info.storageMode; // "localStorage"
info.enabled; // false
info.size; // 1234
const info = cache.info('foo');
info.created; // 1234567890
info.accessed; // 1234567990
info.expires; // 1234569999
info.isExpired; // false
keys() → {Array.<string>}
Retrieve a list of the keys of items currently in the cache.
Returns:
The keys of the items in the cache
- Type
- Array.<string>
Example
const keys = cache.keys();
keySet() → {object}
Retrieve an object of the keys of items currently in the cache.
Returns:
The keys of the items in the cache.
- Type
- object
Example
const keySet = cache.keySet();
put(key, value, optionsopt) → {*}
Insert an item into the cache.
Parameters:
Name | Type | Attributes | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
key |
string | The key under which to insert the item. |
|||||||||||||
value |
* | The value to insert. |
|||||||||||||
options |
object |
<optional> |
Configuration options. Properties
|
Returns:
The inserted value.
- Type
- *
Example
const inserted = cache.put('foo', 'bar');
remove(key) → {*}
Remove an item from the cache.
Parameters:
Name | Type | Description |
---|---|---|
key |
string | The key of the item to remove. |
Returns:
The value of the removed item, if any.
- Type
- *
Example
const removed = cache.remove('foo');
removeAll()
Remove all items from the cache.
Example
cache.removeAll();
removeExpired() → {object}
Remove expired items from the cache, if any.
Returns:
The expired items, if any.
- Type
- object
Example
const expiredItems = cache.removeExpired();
setCacheFlushInterval(cacheFlushInterval)
Update the Cache#cacheFlushInterval for the cache. Pass in null
to disable the interval.
Parameters:
Name | Type | Description |
---|---|---|
cacheFlushInterval |
number | null | The new Cache#cacheFlushInterval. |
Example
cache.setCacheFlushInterval(60 * 60 * 1000);
setCapacity(capacity)
Update the Cache#capacity for the cache. Pass in null
to reset
to Number.MAX_VALUE
.
Parameters:
Name | Type | Description |
---|---|---|
capacity |
number | null | The new Cache#capacity. |
Example
cache.setCapacity(1000);
setDeleteOnExpire(deleteOnExpire)
Update the Cache#deleteOnExpire for the cache. Pass in null
to
reset to "none"
.
Parameters:
Name | Type | Description |
---|---|---|
deleteOnExpire |
string | null | The new Cache#deleteOnExpire. |
Example
cache.setDeleteOnExpire('passive');
setMaxAge(maxAge)
Update the Cache#maxAge for the cache. Pass in null
to reset to
to Number.MAX_VALUE
.
Parameters:
Name | Type | Description |
---|---|---|
maxAge |
number | null | The new Cache#maxAge. |
Example
cache.setMaxAge(60 * 60 * 1000);
setOnExpire(onExpire)
Update the Cache#onExpire for the cache. Pass in null
to unset
the global onExpire
callback of the cache.
Parameters:
Name | Type | Description |
---|---|---|
onExpire |
function | null | The new Cache#onExpire. |
Example
cache.setOnExpire(function (key, value, done) {
// Do something
});
setOptions(options, strictopt)
Update multiple cache options at a time.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
object | The options to set. |
|
strict |
boolean |
<optional> |
Reset options not passed to |
Examples
cache.setOptions({
maxAge: 60 * 60 * 1000,
deleteOnExpire: 'aggressive'
});
cache.setOptions({
maxAge: 60 * 60 * 1000,
deleteOnExpire: 'aggressive'
}, true);
setRecycleFreq(recycleFreq)
Update the Cache#recycleFreq for the cache. Pass in null
to
disable the interval.
Parameters:
Name | Type | Description |
---|---|---|
recycleFreq |
number | null | The new Cache#recycleFreq. |
Example
cache.setRecycleFreq(10000);
setStorageMode(storageMode, storageImpl)
Update the Cache#storageMode for the cache.
Parameters:
Name | Type | Description |
---|---|---|
storageMode |
string | The new Cache#storageMode. |
storageImpl |
object | The new Cache~StorageImpl. |
touch(keyopt, optionsopt)
Reset an item's age in the cache, or if key
is unspecified, touch all
items in the cache.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string |
<optional> |
The key of the item to touch. |
options |
object |
<optional> |
Options to pass to Cache#put if necessary. |
Example
cache.touch('foo');
values() → {array}
Retrieve the values of all items in the cache.
Returns:
The values of the items in the cache.
- Type
- array
Example
const values = cache.values();
Type Definitions
onExpireCallback(key, value, doneopt)
The onExpire
callback.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string | The key of the expired item. |
|
value |
* | The value of the expired item. |
|
done |
function |
<optional> |
If in |