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.
Myna.println("This line is not cached");
var result = new Myna.Cache({
name:"test",
tags:"test,query,output",
refreshInterval:Date.getInterval(Date.MINUTE,3),
code:function(sleepTime){
//an artificially long process
Myna.sleep(Date.getInterval("s",sleepTime));
Myna.print("this line is cached: " + new Date())
}
}).call(10)| Myna. Cache | Creates and manages cached objects |
| Properties | |
| code | The cache function. |
| name | Optional Unique name for this cache object |
| tags | Optional A comma separated list of strings associated with this cache object. |
| refreshInterval | Optional, default 1 hour Cache values older than this interval will be refreshed when accessed. |
| maxIdleInterval | Optional, default 24 hours Interval that a cached object can be idle (not accessed) before being deleted. |
| 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. |
| Functions | |
| Cache | Creates a new Cache Object |
| backgroundRefresh | refresh this cache in a background thread. |
| refresh | refresh this cache and return cached value object; |
| call | Calls the cache function. |
| getCachedValues | returns a Myna.DataSet of all currently cached values |
| clear | clears all cached values for this cache object |
| getByName | Static function that returns the cache object associated with the supplied name, or null if not found |
| getByTags | Static function that returns a Myna.DataSet of cache objects that match all of the supplies tags |
| clearByTags | clears the cached values of all cache objects that match all of the supplies tags |
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.
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
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.
Myna.Cache = function CacheConstructor( options )
Creates a new Cache Object
| options | an object representing cache options. See below. |
| code | 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. |
| name | Optional, default code.toSource() A 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. |
| tags | Optional, 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. |
| 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. |
| ignoreArguments | Optional, 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 |
Myna.Cache.prototype.backgroundRefresh = function CacheBackgroundRefresh( args )
refresh this cache in a background thread.
Whatever parameters required for the cached function
Myna.Thread instance
Myna.Cache.prototype.refresh = function CacheRefresh( args )
refresh this cache and return cached value object;
| value | value returned from cache function |
| content | content generated from the cache function |
Myna.Cache.prototype.call = function CacheCall()
Calls the cache function.
Whatever parameters required by the cache function
cache function’s returned value
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.
Myna.Cache.prototype.getCachedValues = function Cache_getCachedValues()
returns a Myna.DataSet of all currently cached values
Myna.Cache.prototype.clear = function Cache_clear()
clears all cached values for this cache object
var cacheObject = Myna.Cache.getByName("test");
if (cacheObject) cacheObject.clear();
Myna.Cache.getByName=function CacheGetByName( name )
Static function that returns the cache object associated with the supplied name, or null if not found
| name | Name of cache object to retrieve. Case-sensitive. |
//clear all the cached values of the "test" cache
var cacheObject = Myna.Cache.getByName("test")
if (cacheObject) cacheObject.clear()
Myna.Cache.getByTags=function CacheGetByTags( tags )
Static function that returns a Myna.DataSet of cache objects that match all of the supplies tags
| tags | comma separated list of tags to match. Case-insensitive. |
var cacheObjects = Myna.Cache.getByTag("Query,table:orders")Creates a new Cache Object
Myna.Cache = function CacheConstructor( options )
refresh this cache in a background thread.
Myna.Cache.prototype.backgroundRefresh = function CacheBackgroundRefresh( args )
refresh this cache and return cached value object;
Myna.Cache.prototype.refresh = function CacheRefresh( args )
Calls the cache function.
Myna.Cache.prototype.call = function CacheCall()
returns a Myna.DataSet of all currently cached values
Myna.Cache.prototype.getCachedValues = function Cache_getCachedValues()
clears all cached values for this cache object
Myna.Cache.prototype.clear = function Cache_clear()
Static function that returns the cache object associated with the supplied name, or null if not found
Myna.Cache.getByName=function CacheGetByName( name )
Static function that returns a Myna.DataSet of cache objects that match all of the supplies tags
Myna.Cache.getByTags=function CacheGetByTags( tags )
clears the cached values of all cache objects that match all of the supplies tags
Myna.Cache.clearByTags=function Cache_clearByTags( tags )
returns a time interval in milliseconds.
Date.getInterval = function( interval, count )