egroupware/phpgwapi/inc/class.egw_cache_files.inc.php

149 lines
3.9 KiB
PHP
Raw Normal View History

Class to manage caching in eGroupware: It allows to cache on 4 levels: a) tree: for all instances/domains runining on a certain source path b) instance: for all sessions on a given instance c) session: for all requests of a session, same as egw_session::appsession() d) request: just for this request (same as using a static variable) There's a get, a set and a unset method for each level: eg. getTree() or setInstance(), as well as a variant allowing to specify the level as first parameter: eg. unsetCache() getXXX($app,$location,$callback=null,array $callback_params,$expiration=0) has three optional parameters allowing to specify: 3. a callback if requested data is not yes stored. In that case the callback is called and it's value is stored in the cache AND retured 4. parameters to pass to the callback as array, see call_user_func_array 5. an expiration time in seconds to specify how long data should be cached, default 0 means infinit (this time is not garantied and not supported for all levels!) Data is stored under an application name and a location, like egw_session::appsession(). In fact data stored at cache level egw_cache::SESSION, is stored in the same way as egw_session::appsession() so both methods can be used with each other. The $app parameter should be either the app or the class name, which both are unique. The tree and instance wide cache uses a certain provider class, to store the data eg. in memcached or if there's nothing else configured in the filesystem (eGW's temp_dir).
2009-04-20 13:50:45 +02:00
<?php
/**
* eGroupWare API: Caching provider storing data to files
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage cache
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2009 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$
*/
/**
* Caching provider storing data in files
*
* The provider creates subdirs under a given path
* for each values in $key
*/
class egw_cache_files implements egw_cache_provider
{
/**
* Extension of file used to store expiration > 0
*/
const EXPIRATION_EXTENSION = '.expiration';
/**
* Path of base-directory for the cache, set via parameter to the constructor or defaults to temp_dir
*
* @var string
*/
protected $base_path;
/**
* Constructor, eg. opens the connection to the backend
*
* @throws Exception if connection to backend could not be established
* @param array $params eg. array(host,port) or array(directory) depending on the provider
*/
function __construct(array $params)
{
if ($params)
{
$this->base_path = $params[0];
}
else
{
if(!isset($GLOBALS['egw_info']['server']['temp_dir']))
{
if (isset($GLOBALS['egw_setup']) && isset($GLOBALS['egw_setup']->db))
{
$GLOBALS['egw_info']['server']['temp_dir'] = $GLOBALS['egw_setup']->db->select(config::TABLE,'config_value',array(
'config_app' => 'phpgwapi',
'config_name' => 'temp_dir',
),__LINE__,__FILE__)->fetchColumn();
}
if (!$GLOBALS['egw_info']['server']['temp_dir'])
{
throw new Exception (__METHOD__."() server/temp_dir is NOT set!");
}
}
$this->base_path = $GLOBALS['egw_info']['server']['temp_dir'].'/egw_cache';
}
if (!isset($this->base_path) || !file_exists($this->base_path) && !mkdir($this->base_path,0700,true))
{
throw new Exception (__METHOD__."() can't create basepath $this->base_path!");
}
}
/**
* Stores some data in the cache
*
* @param array $keys eg. array($level,$app,$location)
* @param mixed $data
* @param int $expiration=0
* @return boolean true on success, false on error
*/
function set(array $keys,$data,$expiration=0)
{
if ($ret = file_put_contents($fname=$this->filename($keys,true),serialize($data),LOCK_EX) > 0)
{
if ((int)$expiration > 0) file_put_contents($fname.self::EXPIRATION_EXTENSION,(string)$expiration);
}
return $ret;
}
/**
* Get some data from the cache
*
* @param array $keys eg. array($level,$app,$location)
* @return mixed data stored or NULL if not found in cache
*/
function get(array $keys)
{
if (!file_exists($fname = $this->filename($keys)))
{
return null;
}
if (file_exists($fname_expiration=$fname.self::EXPIRATION_EXTENSION) &&
($expiration = (int)file_get_contents($fname_expiration)) &&
filemtime($fname) < time()-$expiration)
{
unlink($fname);
unlink($fname_expiration);
return null;
}
return unserialize(file_get_contents($fname));
}
/**
* Delete some data from the cache
*
* @param array $keys eg. array($level,$app,$location)
* @return boolean true on success, false on error (eg. $key not set)
*/
function delete(array $keys)
{
if (!file_exists($fname = $this->filename($keys)))
{
//error_log(__METHOD__.'('.array2string($keys).") file_exists('$fname') == FALSE!");
return false;
}
if (file_exists($$fname_expiration=$fname.self::EXPIRATION_EXTENSION))
{
unlink($fname_expiration);
}
//error_log(__METHOD__.'('.array2string($keys).") calling unlink('$fname')");
return unlink($fname);
}
/**
* Create a path from $keys and $basepath
*
* @param array $keys
* @param boolean $mkdir=false should we create the directory
* @return string
*/
private function filename(array $keys,$mkdir=false)
{
$fname = $this->base_path.'/'.implode('/',$keys);
if ($mkdir && !file_exists($dirname=dirname($fname)))
{
mkdir($dirname,0700,true);
}
return $fname;
}
}