Add an API to handle caching.
In most cases, the API usages is the following:
$var = PlCache::get{Variant}('key',
'factory',
array('factoryarg1','factoryarg2', ...));
PlCache::invalidate{Variant}, set{Variant} and has{Variant} are also
provided.
The Variant provides hints on the expiration of the data and whether it
can be shared between users/sessions or not. Available variants are:
- Local = no sharing, invalidate data after the execution of the page
- Session = no sharing, invalidate data when the session dies
- Global = sharable, can invalidate data after a timeout
Examples:
function buildDataFromDb($tableName)
{
return do_something_very_expensive($tableName);
}
/* Example using all-in-one 'get' variant' */
$value = PlCache::getSession('blah_' . $tableName',
'buildDataFromDb',
array($tableName),
1 /* ensure the value is not
computed more than once
per second */
);
/* Same example using has, set and get */
if (!PlCache::hasSession('blah_' . $tableName)) {
$value = buildDataFromDb($tableName);
PlCache::setSession('blah_' . $tableName, $value, 1);
} else {
$value = PlCache::getSession('blah_' . $tableName);
}
Note: this second example can in very specific conditions produce invalid
results if hasSession is called before expiration of the data and
getSession after.
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>