getItem(key)

Returns an item.

Arguments

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

Lifecycle

  1. preGetItem

    Event name: preGetItem Properties passed:

    • cacheInstance reference to cache instance (this)

    • key key passed to getItem method

    Returns: (object): object containing properties:

    • cacheInstance reference to cache instance (this)

    • key key passed through handlers added for preGetItem event

  2. postGetItem

    Event name: postGetItem Properties passed:

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

    • key key returned by preGetItem

    • item item returned by adapter using its getItem method

    Returns: (object): object containing properties:

    • cacheInstance reference to cache instance (this)

    • key key passed through handlers added for postGetItem event

    • item item passed through handlers added for postGetItem event

    Eventually getItem returns item returned by postGetItem.

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

Make sure that item exists when writing plugins, always. When item is passed to event handler for this event, it might not be an actual item, but undefined. 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 undefined further down, every next plugin will operate on that undefined value. So do not try to access value or 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

Last updated