API Docs for: 1.0.0
Show:

ExpirationCache Class

Cache which drops items based on a user-defined expiration criterion, e.g., age. By default, expired items are only removed when they are requested. If you want to "stop the world" and clean out the cache, call clean().

Constructor

ExpirationCache

(
  • config
)

Parameters:

  • config Object
    • [store] Object optional

      Data store which implements get,put,remove,clear,keys. If not specified, a new instance of Y.InstanceManager is created.

    • [meta] Function optional

      Attaches meta data to an item when it is added to the cache. It receives the value as an argument. If not specified, the default is to timestamp the item.

    • expire Function

      Returns true if the item has expired. It receives the meta data and the value as arguments. If a number is specified, it is assumed to be a duration in milliseconds.

    • [stats] Boolean optional

      Pass true if you want to collect basic statistics. Pass a function if you want to control what information is stored for each key. The function receives the key, the value, and the stat object.

Item Index

Methods

clean

()

Remove all expired values.

clear

()

Remove all values.

dumpStats

() Object

This resets all the values.

Returns:

Object: the current stats

get

(
  • key
)
Mixed

Retrieve a value.

Parameters:

  • key String

    the key of the object to retrieve

Returns:

Mixed: the stored object, or undefined if the slot is empty

put

(
  • key
  • value
)
Boolean

Store a value.

Parameters:

  • key String

    the key of the value

  • value Object

    the value to store

Returns:

Boolean: false if the key has already been used

remove

(
  • key
)
Mixed

Remove an value.

Parameters:

  • key String

    the key of the value

Returns:

Mixed: the value that was removed, or undefined if the slot was empty

replace

(
  • key
  • value
)
Mixed

Store a value.

Parameters:

  • key String

    the key of the value

  • value Object

    the value to store

Returns:

Mixed: the original value that was in the slot, or undefined if the slot is empty