API Docs for: 1.0.0
Show:

MRUCache Class

Cache which drops items based on "most recently used." Items are dropped when a user-defined criterion is exceeded, e.g., total size or number of items.

The items are stored in a map of {data,mru_item_ref}. The MRU items are stored in a doubly linked list (which stores the map keys) to allow easy re-ordering and dropping of items. Every cache hit moves the associated MRU item to the front of the list.

Constructor

MRUCache

(
  • config
)

Parameters:

  • config Object
    • metric Function

      Computes the metric for an item. It receives the value as an argument and must return a positive number.

    • limit Number

      Maximum allowed value of the metric. Items are dropped off the end of the MRU list until the metric is less than or equal to the limit.

    • [meta] Function optional

      Attaches meta data to an item when it is added to the cache. It receives the value as an argument.

    • [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

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