CacheInstance
- addHook(hook)
- addHooks(hooks)
- getHooks()
- buildKey(key)
- getItem(key)
- getExtra(key)
- addExtra(key, extra)
- setExtra(key, extra)
- setItem(key, value, [extra])
- hasItem(key)
- removeItem(key)
Important: read about lifecycle of methods and plugins first.
Important: as cacheInstance uses adapters to get / set / find / remove items, they (adapters) can throw on the way. Those throws (what and when) are not mentioned here, but are described in detail in adapters API methods.
addHook(hook)
This method adds hook (to hooks list). Hook is added to the end of the list. See getHooks method and plugins.
Arguments
hook(Hook): An object witheventandhandlerproperties. See plugins for more info.
Throws
- If
eventis not a string, it will throw an errorHook's event must be a string. - If
eventdoesn't begin withpreorpost, it will throw an errorHook's event must start with 'pre' or 'post'. - If
handleris not a function, it will throw an errorHook's handler must be a function.
Example
// assuming that you already have cache instance prepared
const hook = {
event: 'preGetItem',
handler: () => { ... } // some function that does something, not relevant here
}
cache.addHook(hook);
addHooks(hooks)
This method adds hooks (to hooks list). Hooks are added to the end of the list. See getHooks method and plugins.
Arguments
hooks(array): Array of hooks.
Throws
- If
eventfor any hook is not a string, it will throw an errorHook's event must be a string. - If
eventfor any hook doesn't begin withpreorpost, it will throw an errorHook's event must start with 'pre' or 'post'. - If
handlerfor any hook is not a function, it will throw an errorHook's handler must be a function.
Example
// assuming that you already have cache instance prepared
const hook1 = {
event: 'preGetItem',
handler: () => { ... } // some function that does something, not relevant here
}
const hook2 = {
event: 'postSetItem',
handler: () => { ... } // some function that does something, not relevant here
}
cache.addHooks([ hook1, hook2 ]);
getHooks()
Returns hooks.
Returns
(object): Object with hooks.
Hooks, when added, are stored in an object. Properties of that object are hook's events and values are arrays of handlers.
Examples
// assuming that you already have cache instance prepared
const hook1 = {
event: 'preGetItem',
handler: () => { ... } // some function that does something, not relevant here
}
const hook2 = {
event: 'postSetItem',
handler: () => { ... } // some function that does something, not relevant here
}
cache.addHooks([ hook1, hook2 ]);
// In cache, hooks will be stored like so:
{
hooks: {
preGetItem: [
() => {} // handler from hook1
],
postSetItem: [
() => {} // handler from hook2
]
}
}
cache.getHooks(); // it will return object shown above
What will happen when I will add more hooks with same events?
// continuing from the example above
const hook3 = {
event: 'preGetItem',
handler: () => {}
}
cache.addHook(hook3);
//right now hooks will look like so:
{
hooks: {
preGetItem: [
() => {} // handler from hook1,
() => {} // handler from hook3, added to the end of the list of hooks for given event
],
postSetItem: [
() => {} // handler from hook2
]
}
}
buildKey(key)
This method builds a key. See also keys (basic usage) and buildKey(key) in adapter API.
Arguments
key(string): String to build a key of.
Lifecycle
preBuildKeyEvent name:
preBuiltKey
Properties passed:cacheInstancereference to cache instance (this)keykey passed tobuildKeymethod
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpreBuildKeyevent
postBuildKeyEvent name:
postBuiltKey
Properties passed:cacheInstancereference to cache instance (this) returned bypreBuildKeykeykey built by adapter using its buildKey(key) method
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpostBuildKeyevent
Eventually
buildKeyreturns key returned bypostBuildKey.
Returns
(string): Built key. Depends on adapter being used.
Example
// assuming that you already have cache instance prepared
const builtKey = cache.buildKey('key');
getItem(key)
Returns an item.
Arguments
key(string): Key to get an item by.
Lifecycle
preGetItemEvent name:
preGetItem
Properties passed:cacheInstancereference to cache instance (this)keykey passed togetItemmethod
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpreGetItemevent
postGetItemEvent name:
postGetItem
Properties passed:cacheInstancereference to cache instance (this) returned bypreGetItemkeykey returned bypreGetItemitemitem returned by adapter using its getItem(key) method
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpostGetItemeventitemitem passed through handlers added forpostGetItemevent
Eventually
getItemreturns item returned bypostGetItem.
Important: even though
getItemusesbuildKeyinternally, that build key is not passed inpostGetItemevent handler's object's properties. If key is somehow changed (with some plugin) duringpreBuildKeyorpostGetKeyevents, that key is not passed further ingetItem. If you need access to built key, you can still obtain it from returned item to which you have access inpostGetItem.Important: make sure that
itemexists when writing plugins, always. Whenitemis passed to event handler for this event, it might not be an actual item, butundefined. Why? Some plugin before one you will use to operate on this item, might have already removed it. How? Here's an example:
- you have a ttl plugin that will automatically remove an item once it's lifetime is over,
- you have another plugin that does something with item's data,
- order of executing plugins matter, therefore when first plugin will remove that item and pass
undefinedfurther down, every next plugin will operate on that undefined value. So do not try to accessvalueor anything from that item, your app will crash.
Returns
(Item): Item, an object.
(undefined): If item is not found, returns undefined.
Example
// assuming that you already have cache instance prepared
cache.getItem('key'); // Item
cache.getItem('keyForItemThatDoesNotExist'); // undefined
getExtra(key)
Returns extra (for given object for given key).
Arguments
key(string): Key to get an extra by.
Lifecycle
preGetExtraEvent name:
preGetExtra
Properties passed:cacheInstancereference to cache instance (this)keykey passed togetExtramethod
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpreGetExtraevent
postGetExtraEvent name:
postGetExtra
Properties passed:cacheInstancereference to cache instance (this) returned bypreGetExtrakeykey returned bypreGetExtraextraextra (object) returned by adapter using its getExtra(key) method
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpostGetExtraeventextraextra passed through handlers added forpostGetExtraevent
Eventually
getExtrareturns extra returned bypostGetExtra.
Important: even though
getExtrausesbuildKeyinternally, that build key is not passed inpostGetExtraevent handler's object's properties. If key is somehow changed (using some plugin) duringpreBuildKeyorpostGetKeyevents, that key is not passed further ingetExtra. If you need access to built key, you can still obtain it from returned item to which you have access inpostGetExtra.Important: make sure that
extraexists when writing plugins, always. Whenextrais passed to event handler for this event, it might not exist anymore, and beundefined. Why? Some plugin before one you will use to operate on this extra, might have already removed item (from which this extra is taken from). How? Here's an example:
- you have a ttl plugin that will automatically remove an item once it's lifetime is over,
- you have another plugin that does something with this item's extra,
- order of executing plugins matter, therefore when first plugin will remove that item and pass
undefinedfurther down, every next plugin will operate on that undefined value. So do not try to access any property from that extra, your app will crash.
Returns
(object): Object, extra from item.
(undefined): If item is not found (thus no extra can be taken from it), returns undefined.
Example
// assuming that you already have cache instance prepared
cache.getExtra('key'); // object, extra for that item
cache.getExtra('keyForItemThatDoesNotExist'); // undefined
addExtra(key, extra)
Adds extra to item's existing extra. New properties are added, existing are overwritten.
Arguments
key(string): Key to store the extra under (represents given item).extra(object): Extra data to store.
Throws
- If
extrais not an object, it will throw'extra' must be an object.
Lifecycle
preAddExtraEvent name:
preAddExtra
Properties passed:cacheInstancereference to cache instance (this)keykey passed toaddExtramethodextraextra passed toaddExtramethod
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpreAddExtraeventextraextra passed through handlers added forpreAddExtraevent
postAddExtraEvent name:
postAddExtra
Properties passed:cacheInstancereference to cache instance (this) returned bypreAddExtrakeykey returned bypreAddExtraextraextra (object) returned by adapter using its addExtra(key, extra) method
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpostAddExtraeventextraextra passed through handlers added forpostAddExtraevent
Eventually
addExtrareturns extra returned bypostAddExtra.
Example
// Existing extra
{
some: 'data'
}
cache.addExtra(key, { foo: 'bar' });
// Extra after adding new one
{
foo: 'bar',
some: 'data'
}
cache.addExtra(key, { foo: 'baz' });
// Extra after another round of adding stuff to it
{
foo: 'baz', // <-- notice that this value's changed
some: 'data'
}
setExtra(key, extra)
Sets (replaces existing) extra in an item.
Important: as existing extra is being overwritten, be careful how / when you use it. Simply, use this method with caution.
Arguments
key(string): Key to store the extra under (represents given item).extra(object): Extra data to replace existing one.
Throws
- If
extrais not an object, it will throw'extra' must be an object.
Lifecycle
preSetExtraEvent name:
preSetExtra
Properties passed:cacheInstancereference to cache instance (this)keykey passed tosetExtramethodextraextra passed tosetExtramethod
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpreSetExtraeventextraextra passed through handlers added forpreSetExtraevent
postSetExtraEvent name:
postSetExtra
Properties passed:cacheInstancereference to cache instance (this) returned bypreSetExtrakeykey returned bypreSetExtraextraextra (object) returned by adapter using its setExtra(key, extra) method
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpostSetExtraeventextraextra passed through handlers added forpostSetExtraevent
Eventually
setExtrareturns extra returned bypostSetExtra.
Example
// Existing extra
{
some: 'data'
}
cache.setExtra(key, { foo: 'bar' });
// Extra after setting new one
{
foo: 'bar'
}
setItem(key, value, [extra])
Sets an item (and returns it, once created).
Arguments
key(string): Key to store the value under.value(any): Any value to store in item. See values.extra(object): Optional, defaults to empty object if not passed. Extra data to store alongside value in item.
Lifecycle
preSetItemEvent name:
preSetItem
Properties passed:cacheInstancereference to cache instance (this)keykey passed tosetItemmethodvaluevalue passed tosetItemmethodextraextra passed tosetItemmethod
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpreSetItemeventvaluevalue passed through handlers added forpreSetItemeventextraextra passed through handlers added forpreSetItemevent
postSetItemEvent name:
postSetItem
Properties passed:cacheInstancereference to cache instance (this) returned bypreSetItemkeykey returned bypreSetItemvaluevalue returned bypreSetItemextraextra returned bypreSetItemitemreturned by adapter using setItem(key, value, extra) method
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpostSetItemeventvaluevalue passed through handlers added forpostSetItemeventextraextra passed through handlers added forpostSetItemeventitemitem passed through handlers added forpostSetItemevent
Eventually
setItemreturns item returned bypostSetItem.
Important: even though
setItemusesbuildKeyinternally, that build key is not passed inpostSetItemevent handler's object's properties. If key is somehow changed (with some plugin) duringpreBuildKeyorpostGetKeyevents, that key is not passed further insetItem. If you need access to built key, you can still obtain it from returned item to which you have access inpostSetItem.
Returns
(Item): Stored (thus created) item.
Example
// assuming that you already have cache instance prepared
cache.setItem('key', 'value'); // Item, the very one set here
cache.setitem('key', 'value', { some: 'extra data' }); // Item, the very one set here
hasItem(key)
Checks if item exists.
Arguments
key(string): Key to check if an item exists by.
Lifecycle
preHasItemEvent name:
preHasItem
Properties passed:cacheInstancereference to cache instance (this)keykey passed tohasItemmethod
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpreHasItemevent
postHasItemEvent name:
postHasItem
Properties passed:cacheInstancereference to cache instance (this) returned bypreHasItemkeykey returned bypreHasItemresultboolean value returned by adapter using its hasItem(key) method
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpostHasItemeventresultboolean value passed through handlers added forpostHasItemevent
Eventually
hasItemreturns result returned bypostHasItem.
Important: even though
hasItemusesbuildKeyinternally, that build key is not passed inpostHasItemevent handler's object's properties. If key is somehow changed (with some plugin) duringpreBuildKeyorpostGetKeyevents, that key is not passed further inhasItem. If you need access to built key, you can still obtain it from returned item to which you have access inpostHasItem.
Returns
(bool): true if item exists, false otherwise.
Example
// assuming that you already have cache instance prepared
cache.hasItem('key'); // true
cache.hasItem('keyForItemThatDoesNotExist'); // false
removeItem(key)
Removes an item.
Arguments
key(string): Key to remove an item by.
Lifecycle
preRemoveItemEvent name:
preRemoveItem
Properties passed:cacheInstancereference to cache instance (this)keykey passed toremoveItemmethod
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpreRemoveItemevent
postRemoveItemEvent name:
postRemoveItem
Properties passed:cacheInstancereference to cache instance (this) returned bypreRemoveItemkeykey returned bypreRemoveItemresultboolean value returned by adapter using its removeItem(key) method
Returns: (object): object containing properties:
cacheInstancereference to cache instance (this)keykey passed through handlers added forpostRemoveItemeventresultboolean value passed through handlers added forpostRemoveItemevent
Eventually
removeItemreturns result returned bypostRemoveItem.
Important: even though
removeItemusesbuildKeyinternally, that build key is not passed inpostRemoveItemevent handler's object's properties. If key is somehow changed (with some plugin) duringpreBuildKeyorpostGetKeyevents, that key is not passed further inremoveItem. If you need access to built key, you can still obtain it from returned item to which you have access inpostRemoveItem.
Returns
(bool): true if item was removed, false otherwise.
Example
// assuming that you already have cache instance prepared
cache.removeItem('key'); // true
cache.removeItem('keyForItemThatDoesNotExist'); // false