getExtra(key)

Returns extra (for given object for given key).

Arguments

  1. key (string): Key to get an extra by.

Lifecycle

  1. preGetExtra

    Event name: preGetExtra Properties passed:

    • cacheInstance reference to cache instance (this)

    • key key passed to getExtra method

    Returns: (object): object containing properties:

    • cacheInstance reference to cache instance (this)

    • key key passed through handlers added for preGetExtra event

  2. postGetExtra

    Event name: postGetExtra Properties passed:

    • cacheInstance reference to cache instance (this) returned by preGetExtra

    • key key returned by preGetExtra

    • extra extra (object) returned by adapter using its getExtra method

    Returns: (object): object containing properties:

    • cacheInstance reference to cache instance (this)

    • key key passed through handlers added for postGetExtra event

    • extra extra passed through handlers added for postGetExtra event

    Eventually getExtra returns extra returned by postGetExtra.

Even though getExtra uses buildKey internally, that build key is not passed in postGetExtra event handler's object's properties. If key is somehow changed (using some plugin) during preBuildKey or postGetKey events, that key is not passed further in getExtra. If you need access to built key, you can still obtain it from returned item to which you have access in postGetExtra.

Make sure that extra exists when writing plugins, always. When extra is passed to event handler for this event, it might not exist anymore, and be undefined. 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 undefined further 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

Last updated