From: Jan Schneider Date: Wed, 22 Sep 2010 19:20:34 +0000 (+0200) Subject: Throw exceptions when not passing strings, and if writing the cache shouldn't fail. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=523561bce887d33e305d61cc67efde0ce822df1a;p=horde.git Throw exceptions when not passing strings, and if writing the cache shouldn't fail. --- diff --git a/framework/Cache/lib/Horde/Cache/Apc.php b/framework/Cache/lib/Horde/Cache/Apc.php index 4860d33f0..4c7828d95 100644 --- a/framework/Cache/lib/Horde/Cache/Apc.php +++ b/framework/Cache/lib/Horde/Cache/Apc.php @@ -34,15 +34,21 @@ class Horde_Cache_Apc extends Horde_Cache_Base * Attempts to store an object to the cache. * * @param string $key Cache key (identifier). - * @param mixed $data Data to store in the cache. + * @param string $data Data to store in the cache. * @param integer $lifetime Data lifetime. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } $key = $this->_params['prefix'] . $key; $lifetime = $this->_getLifetime($lifetime); - apc_store($key . '_expire', time(), $lifetime); - apc_store($key, $data, $lifetime); + if (apc_store($key . '_expire', time(), $lifetime)) { + apc_store($key, $data, $lifetime); + } } /** diff --git a/framework/Cache/lib/Horde/Cache/Base.php b/framework/Cache/lib/Horde/Cache/Base.php index 9faa3d5ca..2067a1f48 100644 --- a/framework/Cache/lib/Horde/Cache/Base.php +++ b/framework/Cache/lib/Horde/Cache/Base.php @@ -92,6 +92,8 @@ abstract class Horde_Cache_Base * data becomes available for garbage * collection. If null use the default Horde GC * time. If 0 will not be GC'd. + * + * @throws Horde_Cache_Exception */ abstract public function set($key, $data, $lifetime = null); diff --git a/framework/Cache/lib/Horde/Cache/Eaccelerator.php b/framework/Cache/lib/Horde/Cache/Eaccelerator.php index 93ff1c692..c91965420 100644 --- a/framework/Cache/lib/Horde/Cache/Eaccelerator.php +++ b/framework/Cache/lib/Horde/Cache/Eaccelerator.php @@ -48,15 +48,21 @@ class Horde_Cache_Eaccelerator extends Horde_Cache_Base * Attempts to store an object to the cache. * * @param string $key Cache key (identifier). - * @param mixed $data Data to store in the cache. + * @param string $data Data to store in the cache. * @param integer $lifetime Data lifetime. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } $key = $this->_params['prefix'] . $key; $lifetime = $this->_getLifetime($lifetime); - eaccelerator_put($key . '_expire', time(), $lifetime); - eaccelerator_put($key, $data, $lifetime); + if (eaccelerator_put($key . '_expire', time(), $lifetime)) { + eaccelerator_put($key, $data, $lifetime); + } } /** @@ -72,7 +78,7 @@ class Horde_Cache_Eaccelerator extends Horde_Cache_Base { $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); - return (eaccelerator_get($key) === false) ? false : true; + return eaccelerator_get($key) !== false; } /** diff --git a/framework/Cache/lib/Horde/Cache/File.php b/framework/Cache/lib/Horde/Cache/File.php index cc369982f..d4cf07cea 100644 --- a/framework/Cache/lib/Horde/Cache/File.php +++ b/framework/Cache/lib/Horde/Cache/File.php @@ -134,11 +134,17 @@ class Horde_Cache_File extends Horde_Cache_Base * Attempts to store data to the filesystem. * * @param string $key Cache key. - * @param mixed $data Data to store in the cache. (MUST BE A STRING) + * @param string $data Data to store in the cache. * @param integer $lifetime Data lifetime. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } + $filename = $this->_keyToFile($key, true); $tmp_file = Horde_Util::getTempFile('HordeCache', true, $this->_dir); if (isset($this->_params['umask'])) { @@ -146,7 +152,7 @@ class Horde_Cache_File extends Horde_Cache_Base } if (file_put_contents($tmp_file, $data) === false) { - return; + throw new Horde_Cache_Exception('Cannot write to cache directory ' . $this->_dir); } @rename($tmp_file, $filename); diff --git a/framework/Cache/lib/Horde/Cache/Memcache.php b/framework/Cache/lib/Horde/Cache/Memcache.php index ac199e5b6..e9073ebbe 100644 --- a/framework/Cache/lib/Horde/Cache/Memcache.php +++ b/framework/Cache/lib/Horde/Cache/Memcache.php @@ -104,9 +104,15 @@ class Horde_Cache_Memcache extends Horde_Cache_Base * @param string $key Cache key. * @param mixed $data Data to store in the cache. * @param integer $lifetime Data lifetime. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } + $key = $this->_params['prefix'] . $key; $lifetime = $this->_getLifetime($lifetime); diff --git a/framework/Cache/lib/Horde/Cache/Mock.php b/framework/Cache/lib/Horde/Cache/Mock.php index 48fe8fa8e..872ba375e 100644 --- a/framework/Cache/lib/Horde/Cache/Mock.php +++ b/framework/Cache/lib/Horde/Cache/Mock.php @@ -53,11 +53,16 @@ class Horde_Cache_Mock extends Horde_Cache_Base * Attempts to store an object to the cache. * * @param string $key Cache key (identifier). - * @param mixed $data Data to store in the cache. + * @param string $data Data to store in the cache. * @param integer $lifetime Data lifetime. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } $this->_cache[$key] = $data; } diff --git a/framework/Cache/lib/Horde/Cache/Null.php b/framework/Cache/lib/Horde/Cache/Null.php index c8f7589d7..27267021b 100644 --- a/framework/Cache/lib/Horde/Cache/Null.php +++ b/framework/Cache/lib/Horde/Cache/Null.php @@ -32,11 +32,16 @@ class Horde_Cache_Null extends Horde_Cache_Base * Attempts to store an object to the cache. * * @param string $key Cache key (identifier). - * @param mixed $data Data to store in the cache. + * @param string $data Data to store in the cache. * @param integer $lifetime Data lifetime. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } } /** diff --git a/framework/Cache/lib/Horde/Cache/Session.php b/framework/Cache/lib/Horde/Cache/Session.php index 9dd306662..260b56fc5 100644 --- a/framework/Cache/lib/Horde/Cache/Session.php +++ b/framework/Cache/lib/Horde/Cache/Session.php @@ -67,11 +67,16 @@ class Horde_Cache_Session extends Horde_Cache_Base * Attempts to store an object to the cache. * * @param string $key Cache key (identifier). - * @param mixed $data Data to store in the cache. + * @param string $data Data to store in the cache. * @param integer $lifetime Data lifetime. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } $this->_sess[$key] = array( 'd' => $data, 'l' => $this->_getLifetime($lifetime) diff --git a/framework/Cache/lib/Horde/Cache/Sql.php b/framework/Cache/lib/Horde/Cache/Sql.php index 61c69f503..e5ab16550 100644 --- a/framework/Cache/lib/Horde/Cache/Sql.php +++ b/framework/Cache/lib/Horde/Cache/Sql.php @@ -136,12 +136,18 @@ class Horde_Cache_Sql extends Horde_Cache_Base * Attempts to store data. * * @param string $key Cache key. - * @param mixed $data Data to store in the cache. (MUST BE A STRING) + * @param string $data Data to store in the cache. * @param integer $lifetime Maximum data life span or 0 for a * non-expiring object. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } + $okey = $key; $key = hash('md5', $key); @@ -171,7 +177,9 @@ class Horde_Cache_Sql extends Horde_Cache_Base try { $this->_db->insert($query, $values); - } catch (Horde_Db_Exception $e) {} + } catch (Horde_Db_Exception $e) { + throw new Horde_Cache_Exception($e); + } } /** diff --git a/framework/Cache/lib/Horde/Cache/Stack.php b/framework/Cache/lib/Horde/Cache/Stack.php index 434be713b..d075b9c58 100644 --- a/framework/Cache/lib/Horde/Cache/Stack.php +++ b/framework/Cache/lib/Horde/Cache/Stack.php @@ -77,14 +77,20 @@ class Horde_Cache_Stack extends Horde_Cache_Base * Attempts to store an object in the cache. * * @param string $key Object ID used as the caching key. - * @param mixed $data Data to store in the cache. + * @param string $data Data to store in the cache. * @param integer $lifetime Object lifetime - i.e. the time before the * data becomes available for garbage * collection. If null use the default Horde GC * time. If 0 will not be GC'd. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } + /* Do writes in *reverse* order - it is OK if a write to one of the * non-master backends fails. */ $master = true; diff --git a/framework/Cache/lib/Horde/Cache/Xcache.php b/framework/Cache/lib/Horde/Cache/Xcache.php index 60fdca3b1..bd4306167 100644 --- a/framework/Cache/lib/Horde/Cache/Xcache.php +++ b/framework/Cache/lib/Horde/Cache/Xcache.php @@ -37,15 +37,21 @@ class Horde_Cache_Xcache extends Horde_Cache_Base * Attempts to store an object to the cache. * * @param string $key Cache key (identifier). - * @param mixed $data Data to store in the cache. + * @param string $data Data to store in the cache. * @param integer $lifetime Data lifetime. + * + * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { + if (!is_string($data)) { + throw new Horde_Cache_Exception('Data must be a string.'); + } $key = $this->_params['prefix'] . $key; $lifetime = $this->_getLifetime($lifetime); - xcache_set($key . '_expire', time(), $lifetime); - xcache_set($key, $data, $lifetime); + if (xcache_set($key . '_expire', time(), $lifetime)) { + xcache_set($key, $data, $lifetime); + } } /**