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.

Methods

_fireErrorEvent

() Void private

Fires the error event. When this happens, the readyState must first be set to 2 so that error handlers querying this value receive the correct value.

Returns:

Void:

_fireMessageEvent

() Void private

Fires the message event with appropriate data, but only if there is actual data to share. This uses the stored event name and data value to fire the appropriate event.

Returns:

Void:

_fireMessageEvent

(
  • data
)
Void private

Fires a message event. This might be either an event of type message or a custom event as received from the event stream.

Parameters:

  • data Object

    An object containing the keys type, data, lastEventId, and origin.

Returns:

Void:

_fireOpenEvent

() Void private

Fires the open event. When this happens, the readyState must first be set to 1 so that event listeners querying this value receive the correct value.

Returns:

Void:

_init

() Void private

Initializes the EventSource object. Either creates an EventSource instance or an XHR to mimic the functionality.

Returns:

Void:

_processDataLine

(
  • name
  • value
  • secondPass
)
Void private

Responsible for parsing and interpreting a line of data in the event stream source.

Parameters:

  • name String

    The field name of the line.

  • value Variant

    The field value of the line.

  • secondPass Boolean

    (Optional) Indicates that this is the second time the function was called for this line. Needed to prevent infinite recursion.

Returns:

Void:

_processIncomingData

(
  • text
)
Void private

Processes the data stream as server-sent events. Goes line by line looking for event information and fires the message event where appropriate.

Parameters:

  • text String

    The text to parse.

Returns:

Void:

_signalOpen

() Void private

Updates the readyState property to 1 if it's still set at 0 and fires the open event.

Returns:

Void:

_validateResponse

() Void private

Called when XHR readyState 4 occurs. Processes the response, then reopens the connection to the server unless close() has been called.

Returns:

Void:

clean

()

Remove all expired values.

clear

()

Remove all values.

close

() Void

Permanently close the connection with the server.

Returns:

Void:

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

Properties

_data

String private

Tracks the last piece of data from the messages stream.

_eventName

String private

Tracks the last event name in the message stream.

_lastEventId

Variant private

Keeps track of the last event ID received from the server. Only used when native EventSource is not available.

_lastIndex

Int private

Keeps track of where in the response buffer to start evaluating new data. Only used when native EventSource is not available.

_transport

XMLHttpRequest | EventSource private

Object used to communicate with the server. May be an XHR or an EventSource.

readyState

Int

The current state of the object. Possible values are 0 for connecting, 1 for connected, and 2 for disconnected.

url

String

The URL or the server-sent events.