


A very simply cache that can be used to store results of computations.
Can save and retrieve arbitrary values using a vector (includnig char vectors) as a key.
Especially useful if a function must perform heavy computation but is often called with
the same inputs (for which it will give the same outputs). Note that the current
implementation does a linear search for the key (a more refined implementation would use
a hash table), so it is not meant for large scale usage.
To use inside a function, make the cache persistent:
persistent cache; if( isempty(cache) ) cache=simplecache('init'); end;
The following line, when placed inside a function, means the cache will stay in memory
until the matlab environment changes. For an example usage see mask_gaussians.
USAGE:
%%% initialize a cache:
cache = simplecache('init');
%%% put something in a cache. Note that key must be a numeric vector.
%%% if cache already contained an object with the same key that obj is overwritten.
cache = simplecache( 'put', cache, key, val );
%%% attempt to get something from cache. found==1 if obj was found, val is the obj.
[found,val] = simplecache( 'get', cache, key );
%%% free key
[cache,found] = simplecache( 'remove', cache, key );
EXAMPLE
cache = simplecache('init');
hellokey=rand(1,3); worldkey=rand(1,11);
cache = simplecache( 'put', cache, hellokey, 'hello' );
cache = simplecache( 'put', cache, worldkey, 'world' );
[f,v]=simplecache( 'get', cache, hellokey ); disp(v);
[f,v]=simplecache( 'get', cache, worldkey ); disp(v);
DATESTAMP
29-Sep-2005 2:00pm
See also PERSISTENT, MASK_GAUSSIANS