cache::async(n) | In-memory caches | cache::async(n) |
cache::async - Asynchronous in-memory cache
package require Tcl 8.4
package require cache::async ?0.3?
::cache::async objectName commandprefix ?options...?
objectName get key donecmdprefix
objectName set key value
objectName unset key
objectName exists key
objectName clear ?key?
This package provides objects which cache data in memory, and operate asynchronously with regard to request and responses. The objects are agnostic with regard to cache keys and values, and unknown methods are delegated to the provider of cached data. These two properties make it easy to use caches as a facade for any data provider.
The package exports a class, cache::async, as specified below.
The commandprefix is the action to perform when an user asks for data in the cache and the cache doesn't yet know about the key. When run the commandprefix is given three additional arguments, the string get, the key requested, and the cache object itself, in the form of its object command, in this order. The execution of the action is done in an idle-handler, decoupling it from the original request.
The only supported option is
The object commands created by the class commands above have the form:
Whenever the information is/becomes available the donecmdprefix will be run to transfer the result to the caller. This command prefix is invoked with either 2 or 3 arguments, i.e.
For a cache object configured to be fully asynchronous (default) the donecmdprefix is always run in an idle-handler, decoupling it from the request. Otherwise the callback will be invoked synchronously when the key is known to the cache at the time of the invokation.
Another important part of the cache's behaviour, as it is asynchronous it is possible that multiple get requests are issued for the same key before it can respond. In that case the cache will issue only one data request to the provider, for the first of these, and suspend the others, and then notify all of them when the data becomes available.
It is expected that the data provider (see commandprefix of the constructor) uses them in response to data requests for unknown keys.
Note how this matches the cache's own API towards its caller, calling the donecmd of get-requests issued to itself with either "set key value" or "unset key", versus issuing get-requests to its own provider with itself in the place of the donecmd, expecting to be called with either "set key value" or "unset key".
This also means that these methods invoke the donecmd of all get-requests waiting for information about the modified key.
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category cache of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation.
asynchronous, cache, callback, synchronous
Copyright (c) 2008 Andreas Kupries <andreas_kupries@users.sourceforge.net>
0.3 | cache |