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 optionalData store which implements get,put,remove,clear,keys. If not specified, a new instance of
Y.InstanceManager
is created. -
[meta]
Function optionalAttaches 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
FunctionReturns 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 optionalPass 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
Properties
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:
_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:
_fireMessageEvent
-
data
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
ObjectAn object containing the keys type, data, lastEventId, and origin.
Returns:
_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:
_init
()
Void
private
Initializes the EventSource object. Either creates an EventSource instance or an XHR to mimic the functionality.
Returns:
_processDataLine
-
name
-
value
-
secondPass
Responsible for parsing and interpreting a line of data in the event stream source.
Parameters:
-
name
StringThe field name of the line.
-
value
VariantThe 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:
_processIncomingData
-
text
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
StringThe text to parse.
Returns:
_signalOpen
()
Void
private
Updates the readyState property to 1 if it's still set at 0 and fires the open event.
Returns:
_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:
clean
()
Remove all expired values.
clear
()
Remove all values.
close
()
Void
Permanently close the connection with the server.
Returns:
dumpStats
()
Object
This resets all the values.
Returns:
the current stats
get
-
key
Retrieve a value.
Parameters:
-
key
Stringthe key of the object to retrieve
Returns:
the stored object, or undefined if the slot is empty
put
-
key
-
value
Store a value.
Parameters:
-
key
Stringthe key of the value
-
value
Objectthe value to store
Returns:
false if the key has already been used
remove
-
key
Remove an value.
Parameters:
-
key
Stringthe key of the value
Returns:
the value that was removed, or undefined if the slot was empty
replace
-
key
-
value
Store a value.
Parameters:
-
key
Stringthe key of the value
-
value
Objectthe value to store
Returns:
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.