egroupware_official/phpgwapi/inc/class.egw_cache_files.inc.php

215 lines
5.4 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
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
*
* @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>
2015-04-27 10:31:49 +02:00
* @copyright (c) 2009-15 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
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
* @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 extends egw_cache_provider_check implements egw_cache_provider
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
{
/**
* 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
{
$this->base_path = egw_cache::get_system_config('temp_dir', false);
if (isset($this->base_path)) $this->base_path .= '/egw_cache';
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
}
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, if it does NOT already exists there
*
* @param array $keys eg. array($level,$app,$location)
* @param mixed $data
* @param int $expiration =0
* @return boolean true on success, false on error, incl. key already exists in cache
*/
function add(array $keys,$data,$expiration=0)
{
// open only if file does NOT exist
if (!($ret = fopen($fname=$this->filename($keys,true), 'x')))
{
// if file exists, check if it is expired
if (file_exists($fname_expiration=$fname.self::EXPIRATION_EXTENSION) &&
($expiration = (int)file_get_contents($fname_expiration)) &&
filemtime($fname) < time()-$expiration)
{
// open and truncate it
$ret = fopen($fname, 'w');
// remove expiration
unlink($fname_expiration);
}
}
if ($ret)
{
flock($ret, LOCK_EX);
$ok = fwrite($ret, serialize($data));
if ((int)$expiration > 0) file_put_contents($fname.self::EXPIRATION_EXTENSION,(string)$expiration);
flock($ret, LOCK_UN);
fclose($ret);
$ret = $ok !== false;
}
return $ret;
}
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
/**
* Stores some data in the cache
*
* @param array $keys eg. array($level,$app,$location)
* @param mixed $data
2015-04-27 10:31:49 +02:00
* @param int $expiration =0
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
* @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))
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
{
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;
}
2011-11-02 15:57:41 +01:00
if (file_exists($fname_expiration=$fname.self::EXPIRATION_EXTENSION))
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
{
unlink($fname_expiration);
}
//error_log(__METHOD__.'('.array2string($keys).") calling unlink('$fname')");
return unlink($fname);
}
/**
* Delete all data under given keys
*
* @param array $keys eg. array($level,$app,$location)
* @return boolean true on success, false on error (eg. $key not set)
*/
function flush(array $keys)
{
$dir = $this->filename($keys, false);
return file_exists($dir) ? self::rm_recursive($dir) : true;
}
/**
* Recursive delete a path
*
* @param string $path
* @return boolean true on success, false otherwise
*/
private static function rm_recursive($path)
{
if (!is_dir($path))
{
return unlink($path);
}
foreach(scandir($path) as $file)
{
if ($file == '.' || $file == '..') continue;
$file = $path.'/'.$file;
if (is_dir($file))
{
if (!self::rm_recursive($file)) return false;
}
else
{
if (!unlink($file)) return false;
}
}
return rmdir($path);
}
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
/**
* Create a path from $keys and $basepath
*
* @param array $keys
2015-04-27 10:31:49 +02:00
* @param boolean $mkdir =false should we create the directory
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
* @return string
*/
function filename(array $keys,$mkdir=false)
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
{
$fname = $this->base_path.'/'.str_replace(array(':','*'),'-',implode('/',$keys));
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
if ($mkdir && !file_exists($dirname=dirname($fname)))
{
@mkdir($dirname,0700,true);
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
}
return $fname;
}
}