From 74e214448a7c9f3f73f1490494b508ef7a918a6a Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Sun, 13 Jun 2010 13:12:50 -0400 Subject: [PATCH] Consistenly allow setting prefix for cache entries. For drivers where this makes sense, always allow setting a cache entry prefix for preventing entry collisions from multiple horde instances on the same physical box and for forcing the use of a certain prefix, regardless of the servername we are hitting from. (This last point fixes an issue where a cache entry would not expire when the entry was cached while accessing horde's data via the api from a different virtual server). --- framework/Cache/lib/Horde/Cache/Apc.php | 5 + framework/Cache/lib/Horde/Cache/Base.php | 5 + framework/Cache/lib/Horde/Cache/Eaccelerator.php | 5 + framework/Cache/lib/Horde/Cache/File.php | 32 ++---- framework/Cache/lib/Horde/Cache/Memcache.php | 7 ++ framework/Cache/lib/Horde/Cache/Xcache.php | 22 +--- framework/Cache/lib/Horde/Cache/Zps4.php | 4 + horde/config/conf.php.mine | 133 +++++++++++++++++++++++ horde/config/conf.xml | 32 +++++- 9 files changed, 197 insertions(+), 48 deletions(-) create mode 100644 horde/config/conf.php.mine diff --git a/framework/Cache/lib/Horde/Cache/Apc.php b/framework/Cache/lib/Horde/Cache/Apc.php index 1416cc1b7..83c177188 100644 --- a/framework/Cache/lib/Horde/Cache/Apc.php +++ b/framework/Cache/lib/Horde/Cache/Apc.php @@ -25,6 +25,7 @@ class Horde_Cache_Apc extends Horde_Cache_Base */ public function get($key, $lifetime = 1) { + $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); return apc_fetch($key); } @@ -40,6 +41,7 @@ class Horde_Cache_Apc extends Horde_Cache_Base */ public function set($key, $data, $lifetime = null) { + $key = $this->_params['prefix'] . $key; $lifetime = $this->_getLifetime($lifetime); apc_store($key . '_expire', time(), $lifetime); return apc_store($key, $data, $lifetime); @@ -56,6 +58,7 @@ class Horde_Cache_Apc extends Horde_Cache_Base */ public function exists($key, $lifetime = 1) { + $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); return (apc_fetch($key) === false) ? false : true; } @@ -69,6 +72,7 @@ class Horde_Cache_Apc extends Horde_Cache_Base */ public function expire($key) { + $key = $this->_params['prefix'] . $key; apc_delete($key . '_expire'); return apc_delete($key); } @@ -81,6 +85,7 @@ class Horde_Cache_Apc extends Horde_Cache_Base */ protected function _setExpire($key, $lifetime) { + $key = $this->_params['prefix'] . $key; if ($lifetime == 0) { // Don't expire. return; diff --git a/framework/Cache/lib/Horde/Cache/Base.php b/framework/Cache/lib/Horde/Cache/Base.php index 9b3188c60..2b5a17cf2 100644 --- a/framework/Cache/lib/Horde/Cache/Base.php +++ b/framework/Cache/lib/Horde/Cache/Base.php @@ -50,6 +50,11 @@ abstract class Horde_Cache_Base } $this->_params = array_merge($this->_params, $params); + if (empty($this->_params['prefix'])) { + $this->_params['prefix'] = $_SERVER['SERVER_NAME'] + ? $_SERVER['SERVER_NAME'] + : $_SERVER['HOSTNAME']; + } } /** diff --git a/framework/Cache/lib/Horde/Cache/Eaccelerator.php b/framework/Cache/lib/Horde/Cache/Eaccelerator.php index 2f65f06e5..a0919edf4 100644 --- a/framework/Cache/lib/Horde/Cache/Eaccelerator.php +++ b/framework/Cache/lib/Horde/Cache/Eaccelerator.php @@ -39,6 +39,7 @@ class Horde_Cache_Eaccelerator extends Horde_Cache_Base */ public function get($key, $lifetime = 1) { + $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); return eaccelerator_get($key); } @@ -54,6 +55,7 @@ class Horde_Cache_Eaccelerator extends Horde_Cache_Base */ public function set($key, $data, $lifetime = null) { + $key = $this->_params['prefix'] . $key; $lifetime = $this->_getLifetime($lifetime); eaccelerator_put($key . '_expire', time(), $lifetime); return eaccelerator_put($key, $data, $lifetime); @@ -70,6 +72,7 @@ class Horde_Cache_Eaccelerator extends Horde_Cache_Base */ public function exists($key, $lifetime = 1) { + $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); return (eaccelerator_get($key) === false) ? false : true; } @@ -83,6 +86,7 @@ class Horde_Cache_Eaccelerator extends Horde_Cache_Base */ public function expire($key) { + $key = $this->_params['prefix'] . $key; eaccelerator_rm($key . '_expire'); return eaccelerator_rm($key); } @@ -101,6 +105,7 @@ class Horde_Cache_Eaccelerator extends Horde_Cache_Base return; } + $key = $this->_params['prefix'] . $key; $expire = eaccelerator_get($key . '_expire'); // Set prune period. diff --git a/framework/Cache/lib/Horde/Cache/File.php b/framework/Cache/lib/Horde/Cache/File.php index 80b03a14b..43b113f8a 100644 --- a/framework/Cache/lib/Horde/Cache/File.php +++ b/framework/Cache/lib/Horde/Cache/File.php @@ -23,20 +23,6 @@ class Horde_Cache_File extends Horde_Cache_Base protected $_dir; /** - * The filename prefix for cache files. - * - * @var string - */ - protected $_prefix = 'cache_'; - - /** - * The subdirectory level the cache files should live at. - * - * @var integer - */ - protected $_sub = 0; - - /** * List of key to filename mappings. * * @var array @@ -63,12 +49,12 @@ class Horde_Cache_File extends Horde_Cache_Base ? $params['dir'] : Horde_Util::getTempDir(); - if (isset($params['prefix'])) { - $this->_prefix = $params['prefix']; + if (!isset($params['prefix'])) { + $params['prefix'] = 'cache_'; } - if (isset($params['sub'])) { - $this->_sub = intval($params['sub']); + if (!isset($params['sub'])) { + $params['sub'] = 0; } parent::__construct($params); @@ -257,8 +243,8 @@ class Horde_Cache_File extends Horde_Cache_Base $dir = $this->_dir . '/'; $sub = ''; $md5 = md5($key); - if (!empty($this->_sub)) { - $max = min($this->_sub, strlen($md5)); + if (!empty($this->_params['sub'])) { + $max = min($this->_params['sub'], strlen($md5)); for ($i = 0; $i < $max; $i++) { $sub .= $md5[$i]; if ($create && !is_dir($dir . $sub)) { @@ -270,7 +256,7 @@ class Horde_Cache_File extends Horde_Cache_Base $sub .= '/'; } } - $this->_file[$key] = $dir . $sub . $this->_prefix . $md5; + $this->_file[$key] = $dir . $sub . $this->_params['prefix'] . $md5; } return $this->_file[$key]; @@ -296,14 +282,14 @@ class Horde_Cache_File extends Horde_Cache_Base continue; } - if (strpos($entry, $this->_prefix) === 0) { + if (strpos($entry, $this->_params['prefix']) === 0) { $d_time = isset($excepts[$path]) ? $excepts[$path] : $this->_params['lifetime']; if (!empty($d_time) && (($c_time - $d_time) > filemtime($path))) { @unlink($path); unset($excepts[$path]); } - } elseif (!empty($this->_sub) && is_dir($path)) { + } elseif (!empty($this->_params['sub']) && is_dir($path)) { $this->_gcDir($path, $excepts); } } diff --git a/framework/Cache/lib/Horde/Cache/Memcache.php b/framework/Cache/lib/Horde/Cache/Memcache.php index 36b05d5ba..364c8c160 100644 --- a/framework/Cache/lib/Horde/Cache/Memcache.php +++ b/framework/Cache/lib/Horde/Cache/Memcache.php @@ -70,6 +70,7 @@ class Horde_Cache_Memcache extends Horde_Cache_Base */ public function get($key, $lifetime = 1) { + $key = $this->_params['prefix'] . $key; if (isset($this->_expirecache[$key])) { return $this->_expirecache[$key]; } @@ -108,7 +109,9 @@ class Horde_Cache_Memcache extends Horde_Cache_Base */ public function set($key, $data, $lifetime = null) { + $key = $this->_params['prefix'] . $key; $lifetime = $this->_getLifetime($lifetime); + return ($this->_memcache->set($key . '_e', time(), $lifetime) === false) ? false : $this->_memcache->set($key, $data, $lifetime); @@ -124,6 +127,8 @@ class Horde_Cache_Memcache extends Horde_Cache_Base */ public function exists($key, $lifetime = 1) { + $key = $this->_params['prefix'] . $key; + return ($this->get($key, $lifetime) !== false); } @@ -136,8 +141,10 @@ class Horde_Cache_Memcache extends Horde_Cache_Base */ public function expire($key) { + $key = $this->_params['prefix'] . $key; unset($this->_expirecache[$key]); $this->_memcache->delete($key . '_e'); + return $this->_memcache->delete($key); } diff --git a/framework/Cache/lib/Horde/Cache/Xcache.php b/framework/Cache/lib/Horde/Cache/Xcache.php index 4fc8b0248..1574845a5 100644 --- a/framework/Cache/lib/Horde/Cache/Xcache.php +++ b/framework/Cache/lib/Horde/Cache/Xcache.php @@ -15,25 +15,6 @@ class Horde_Cache_Xcache extends Horde_Cache_Base { /** - * Construct a new Horde_Cache_Xcache object. - * - * @param array $params Configuration parameters: - *
-     * 'prefix' - (string) Key prefix.
-     * 
- */ - public function __construct($params = array()) - { - parent::__construct($params); - - if (empty($this->_params['prefix'])) { - $this->_params['prefix'] = $_SERVER['SERVER_NAME'] - ? $_SERVER['SERVER_NAME'] - : $_SERVER['HOSTNAME']; - } - } - - /** * Attempts to retrieve a piece of cached data and return it to the caller. * * @param string $key Cache key to fetch. @@ -107,12 +88,11 @@ class Horde_Cache_Xcache extends Horde_Cache_Base */ protected function _setExpire($key, $lifetime) { - $key = $this->_params['prefix'] . $key; if ($lifetime == 0) { // don't expire return; } - + $key = $this->_params['prefix'] . $key; $expire = xcache_get($key . '_expire'); // set prune period diff --git a/framework/Cache/lib/Horde/Cache/Zps4.php b/framework/Cache/lib/Horde/Cache/Zps4.php index 2e27d4bf1..f455be74a 100644 --- a/framework/Cache/lib/Horde/Cache/Zps4.php +++ b/framework/Cache/lib/Horde/Cache/Zps4.php @@ -25,6 +25,7 @@ class Horde_Cache_Zps4 extends Horde_Cache_Base */ public function get($key, $lifetime = 1) { + $key = $this->_params['prefix'] . $key; return output_cache_get($key, $lifetime); } @@ -39,6 +40,7 @@ class Horde_Cache_Zps4 extends Horde_Cache_Base */ public function set($key, $data, $lifetime = null) { + $key = $this->_params['prefix'] . $key; output_cache_put($key, $data); return true; } @@ -54,6 +56,7 @@ class Horde_Cache_Zps4 extends Horde_Cache_Base */ public function exists($key, $lifetime = 1) { + $key = $this->_params['prefix'] . $key; $exists = output_cache_exists($key, $lifetime); output_cache_stop(); return $exists; @@ -68,6 +71,7 @@ class Horde_Cache_Zps4 extends Horde_Cache_Base */ public function expire($key) { + $key = $this->_params['prefix'] . $key; return output_cache_remove_key($key); } diff --git a/horde/config/conf.php.mine b/horde/config/conf.php.mine new file mode 100644 index 000000000..fbec49197 --- /dev/null +++ b/horde/config/conf.php.mine @@ -0,0 +1,133 @@ +file - + + + + + @@ -1033,6 +1038,10 @@ you want to use the eAccelerator cache driver, you need version 0.9.5 or lower. + + + @@ -1048,7 +1057,12 @@ use for the cache files."/> - + + + + + @@ -1057,8 +1071,18 @@ - - + + + + + + + + + +