Cache

Moduleejs
Definition class Cache
InheritanceCache inherit Object
StabilityPrototype.

Cache meta class to manage in-memory storage of key-value data.

The Cache class provides an abstraction over various in-memory and disk-based caching cache backends.


Properties

QualifiersPropertyTypeDescription
get limitsObjectResource limits for the server and for initial resource limits for requests.

Cache Class Methods

(No own class methods defined)

QualifiersMethod

Cache Instance Methods

QualifiersMethod
Cache(adapter: String = null, options: Object = expression)
 Cache constructor.
destroy(): Void
 Destroy the cache.
expire(key: String, when: Date): Boolean
 Set a new expire date for a key.
inc(key: String, amount: Number = 1): Number
 Increment a key's value by a given amount.
read(key: String, options: Object = null): String
 Read a key.
readObj(key: String, options: Object = null): Object
 Read a key and return an object This will read the data for a key and then deserialize.
remove(key: String): Boolean
 Remove the key and associated value from the cache.
setLimits(limits: Object): Void
 Update the cache cache resource limits.
write(key: String, value: String, options: Object = null): Number
 Write the key and associated value to the cache.
writeObj(key: String, value: Object, options: Object = null): Number
 Write the key and associated object value to the cache.

Method Detail

Cache(adapter: String = null, options: Object = expression)
Description
Cache constructor.
Parameters
adapter: String Adapter for the cache cache. E.g. "local". The Local cache is the only currently supported cache backend. [default: null]
options: Object Adapter options. The common options are described below, other options are passed through to the relevant caching backend. [default: expression]
Options
lifespanDefault lifespan for key values in seconds.
resolutionTime in milliseconds to check for expired expired keys.
timeoutTimeout on cache I/O operations.
traceTrace I/O operations for debug.
moduleModule name containing the cache connector class. This is a bare module name without ".mod" or any leading path. If module is not present, a module name is derrived using "ejs.cache" + adapter.
classClass name containing the cache backend. If the class property is not present, the class is derived from the adapter name with "Cache" appended. The first letter of the adapter is converted to uppercase. For example, if the adapter was "mem", the class would be inferred to be "MemCache". H.

destroy(): Void
Description
Destroy the cache.

expire(key: String, when: Date): Boolean
Description
Set a new expire date for a key.
Parameters
key: String Key to modify.
when: Date Date at which to expire the data. Set to null to never expire.
Returns
True if the key's expiry can be updated.

inc(key: String, amount: Number = 1): Number
Description
Increment a key's value by a given amount. This operation is atomic.
Parameters
key: String Key value to read.
amount: Number Amount by which to increment the value. This amount can be negative to achieve a decrement. [default: 1]
Returns
The new key value. If the key does not exist, it is initialized to the amount value.

read(key: String, options: Object = null): String
Description
Read a key.
Parameters
key: String Key value to read.
options: Object Read options. [default: null]
Options
versionIf set to true, the read will return an object hash containing the data and a unique version identifier for the last update to this key. This version identifier can be specified to write to peform an atomic CAS (check and swap) operation.
Returns
Null if the key is not present. Otherwise return key data as a string or if the options parameter specified "version == true", return an object with the properties "data" for the key data and "version" for the CAS version identifier.

readObj(key: String, options: Object = null): Object
Description
Read a key and return an object This will read the data for a key and then deserialize. This assumes that writeObj was used to store the key value.
Parameters
key: String Key value to read.
options: Object Read options. [default: null]
Options
versionIf set to true, the read will return an object hash containing the data and a unique version identifier for the last update to this key. This version identifier can be specified to write to peform an atomic CAS (check and swap) operation.
Returns
Null if the key is not present. Otherwise return key data as an object. P.

remove(key: String): Boolean
Description
Remove the key and associated value from the cache.
Parameters
key: String Key value to remove. If key is null, then all keys are removed.
Returns
True if the key was removed.

setLimits(limits: Object): Void
Description
Update the cache cache resource limits. The supplied limit fields are updated. See the limits property for limit field details.
Parameters
limits: Object Object hash of limit fields and values.
See Also
limits

write(key: String, value: String, options: Object = null): Number
Description
Write the key and associated value to the cache. The value is written according to the optional mode option.
Parameters
key: String Key to modify.
value: String String value to associate with the key.
options: Object Options values. [default: null]
Options
lifespanPreservation time for the key in seconds.
expireWhen to expire the key. Takes precedence over lifetime.
modeMode of writing: "set" is the default and means set a new value and create if required. "add" means set the value only if the key does not already exist. "append" means append to any existing value and create if required. "prepend" means prepend to any existing value and create if required.
versionUnique version identifier to be used for a conditional write. The write will only be performed if the version id for the key has not changed. This implements an atomic compare and swap. See read.
throwThrow an exception rather than returning null if the version id has been updated for the key.
Returns
The number of bytes written, returns null if the write failed due to an updated version identifier for the key.

writeObj(key: String, value: Object, options: Object = null): Number
Description
Write the key and associated object value to the cache. The object value is serialized using JSON notation and written according to the optional mode option.
Parameters
key: String Key to modify.
value: Object Object to associate with the key.
options: Object Options values. [default: null]
Options
lifespanPreservation time for the key in seconds. Set to zero for never expire.
expireWhen to expire the key. Takes precedence over lifetime.
modeMode of writing: "set" is the default and means set a new value and create if required. "add" means set the value only if the key does not already exist. "append" means append to any existing value and create if required. "prepend" means prepend to any existing value and create if required.
versionUnique version identifier to be used for a conditional write. The write will only be performed if the version id for the key has not changed. This implements an atomic compare and swap. See read.
throwThrow an exception rather than returning null if the version id has been updated for the key.
Returns
The number of bytes written, returns null if the write failed due to an updated version identifier for the key. X.