Add an API to handle caching.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 16 Oct 2010 20:44:06 +0000 (22:44 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 17 Oct 2010 19:31:55 +0000 (21:31 +0200)
commite3c131627aa03f7107709e6d44c7840c09847acd
tree055180092ef6e5ba227049483141d292c250b089
parent6305462f32087ac6f212e563946e8c5f6d3f204e
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>
classes/plcache.php [new file with mode: 0644]
classes/plglobals.php