Myna. Cache

Creates and manages cached objects

This class can cache both generated output and generated values.  This works best for time consuming tasks whose output changes infrequently.

Here is an example

Myna.println("This line is not cached");
var result = new Myna.Cache({
name:"test",
tags:"test,query,output",
refreshInterval:Date.getInterval(Date.MINUTE,3),
includeContent:true,
code:function(sleepTime){
//an artificially long process
Myna.sleep(Date.getInterval("s",sleepTime));
Myna.print("this line is cached: " + new Date())
}
}).call(10)
Summary
Myna. CacheCreates and manages cached objects
Properties
codeThe cache function.
nameOptional Unique name for this cache object
tagsOptional A comma separated list of strings associated with this cache object.
includeContentOptional, default false set to “true” to include generated output in the cache
refreshIntervalOptional, default 1 hour Cache values older than this interval will be refreshed when accessed.
maxIdleIntervalOptional, default 24 hours Interval that a cached object can be idle (not accessed) before being deleted.
allowBackgroundRefreshOptional, default true if true, and a cached value is available, then cache refreshes happen in a background thread and the current cached value is returned.
Functions
CacheCreates a new Cache Object
backgroundRefreshrefresh this cache in a background thread.
refreshrefresh this cache and return cached value object;
getretrieves a cached value or generates a new one
setSets a value for this cache object
callCalls the cache function.
getCachedValuesreturns a Myna.DataSet of all currently cached values
clearclears all cached values for this cache object
getByNameStatic function that returns the cache object associated with the supplied name, or null if not found
getByTagsStatic function that returns a Myna.DataSet of cache objects that match all of the supplies tags
clearByTagsclears the cached values of all cache objects that match all of the supplies tags

Properties

code

The cache function.  This is function that will generate cached values and page output

name

Optional Unique name for this cache object

tags

Optional A comma separated list of strings associated with this cache object.

includeContent

Optional, default false set to “true” to include generated output in the cache

refreshInterval

Optional, default 1 hour Cache values older than this interval will be refreshed when accessed.  See Date.getInterval.  A value of -1 will disable refreshing.

maxIdleInterval

Optional, default 24 hours Interval that a cached object can be idle (not accessed) before being deleted.  See Date.getInterval.  A value of -1 means the cache never expires, it is only refreshed.  Note that if maxIdleInterval is less than interval, it will effectively become the refresh interval during periods of infrequent access

allowBackgroundRefresh

Optional, default true if true, and a cached value is available, then cache refreshes happen in a background thread and the current cached value is returned.  Note that if maxIdleInterval is set, background refreshes will only happen if interval has been exceeded but maxIdleInterval has not.

Functions

Cache

Myna.Cache = function CacheConstructor(options)

Creates a new Cache Object

Parameters

optionsan object representing cache options.  See below.

Options Object

nameA name to associate with all the caches for this cache object.  Used to find this cache object from other threads, and to index cached values.
tagsOptional, default null an array or a comma separated list of strings to associate with this cache object.  Used to find multiple cache objects from other threads.  If $application.appname is defined, it is automatically added as a tag.
maxIdleIntervalOptional, default 24 hours Interval that a cached object can be idle (not accessed) before being deleted.  See Date.getInterval.  A value of -1 means the cache never expires, it is only refreshed.  Note that if maxIdleInterval is less than interval, it will effectively become the refresh interval during periods of infrequent access
codeOptional, default null A function to cache.  All parameters used by the function will need to be passed in because code will be executed in a separate thread.  Needed for call
includeContentOptional, default false If true, content created by this function will be captured, cached, and replayed on call
refreshIntervalOptional, default 1 hour Cache values older than this interval will be refreshed when accessed.  See Date.getInterval.  A value of -1 will disable refreshing.
allowBackgroundRefreshOptional, default true if true, and a cached value is available, then cache refreshes happen in a background thread and the current cached value is returned.  Note that if maxIdleInterval is set, background refreshes will only happen if interval has been exceeded but maxIdleInterval has not.
ignoreArgumentsOptional, default undefined Normally a seperate cached value is created for each unique set of arguments.  If this is set to “true” then name is assumed to be unique and only one cached value will ever be created for this name

backgroundRefresh

Myna.Cache.prototype.backgroundRefresh = function CacheBackgroundRefresh(
   args,
   localContext
)

refresh this cache in a background thread.

Parameters

Whatever parameters required for the cached function

Returns

Myna.Thread instance

refresh

Myna.Cache.prototype.refresh = function CacheRefresh(args)

refresh this cache and return cached value object;

The cache value properties

valuevalue returned from cache function
contentcontent generated from the cache function

get

Myna.Cache.prototype.get = function CacheGet(generator)

retrieves a cached value or generates a new one

Parameters

generatorOptional, default null if defined, a function that returns a value for this cache object

Returns

cached value or result of generator or undefined.  If there is no valid cache value, and generator is defined, the it is executed and the result is saved as the cache value, see set , and returned.  Else, if no generator is defined a undefined is returned

set

Myna.Cache.prototype.set = function CacheSet(value,
content)

Sets a value for this cache object

Parameters

valueValue for this cache object
contentOptional, default “” Content to store in this cache object

Returns

value

call

Myna.Cache.prototype.call = function CacheCall()

Calls the cache function.

Parameters

Whatever parameters required by the cache function

Returns

cache function’s returned value

Detail

returns the return value of the cache function and prints any content generated by the cache function.  Separate cached values are generated for each set of parameters passed to this function.

If the cached value is still valid, the cached value is used.  If the cached value is invalid, and allowBackgroundRefresh is true, a background thread is spawned to update the cache, and the existing cache value is used.  If allowBackgroundRefresh is false or if there is not yet a cached value, the current thread is blocked until values can be generated.

getCachedValues

Myna.Cache.prototype.getCachedValues = function Cache_getCachedValues()

returns a Myna.DataSet of all currently cached values

clear

Myna.Cache.prototype.clear = function Cache_clear()

clears all cached values for this cache object

Example

var cacheObject = Myna.Cache.getByName("test");
if (cacheObject) cacheObject.clear();

getByName

Myna.Cache.getByName=function CacheGetByName(name)

Static function that returns the cache object associated with the supplied name, or null if not found

Parameters

nameName of cache object to retrieve.  Case-sensitive.

Example

//clear all the cached values of the "test" cache
var cacheObject = Myna.Cache.getByName("test")
if (cacheObject) cacheObject.clear()

getByTags

Myna.Cache.getByTags=function CacheGetByTags(tags)

Static function that returns a Myna.DataSet of cache objects that match all of the supplies tags

Parameters

tagscomma separated list of tags to match.  Case-insensitive.

Example

var cacheObjects = Myna.Cache.getByTag("Query,table:orders")

clearByTags

Myna.Cache.clearByTags=function Cache_clearByTags(tags)

clears the cached values of all cache objects that match all of the supplies tags

Parameters

tagscomma separated list of tags to match.  Case-insensitive.

Example

Myna.Cache.clearByTags("Query,table:orders")
Myna.Cache = function CacheConstructor(options)
Creates a new Cache Object
Myna.Cache.prototype.backgroundRefresh = function CacheBackgroundRefresh(
   args,
   localContext
)
refresh this cache in a background thread.
Myna.Cache.prototype.refresh = function CacheRefresh(args)
refresh this cache and return cached value object;
Myna.Cache.prototype.get = function CacheGet(generator)
retrieves a cached value or generates a new one
Myna.Cache.prototype.set = function CacheSet(value,
content)
Sets a value for this cache object
Myna.Cache.prototype.call = function CacheCall()
Calls the cache function.
Myna.Cache.prototype.getCachedValues = function Cache_getCachedValues()
returns a Myna.DataSet of all currently cached values
A specialized array for working with tabular data
Myna.Cache.prototype.clear = function Cache_clear()
clears all cached values for this cache object
Myna.Cache.getByName=function CacheGetByName(name)
Static function that returns the cache object associated with the supplied name, or null if not found
Myna.Cache.getByTags=function CacheGetByTags(tags)
Static function that returns a Myna.DataSet of cache objects that match all of the supplies tags
Myna.Cache.clearByTags=function Cache_clearByTags(tags)
clears the cached values of all cache objects that match all of the supplies tags
Date.getInterval = function(interval,
count)
returns a time interval in milliseconds.
Optional, default true if true, and a cached value is available, then cache refreshes happen in a background thread and the current cached value is returned.