Throw exceptions when not passing strings, and if writing the cache shouldn't fail.
authorJan Schneider <jan@horde.org>
Wed, 22 Sep 2010 19:20:34 +0000 (21:20 +0200)
committerJan Schneider <jan@horde.org>
Wed, 22 Sep 2010 19:20:34 +0000 (21:20 +0200)
framework/Cache/lib/Horde/Cache/Apc.php
framework/Cache/lib/Horde/Cache/Base.php
framework/Cache/lib/Horde/Cache/Eaccelerator.php
framework/Cache/lib/Horde/Cache/File.php
framework/Cache/lib/Horde/Cache/Memcache.php
framework/Cache/lib/Horde/Cache/Mock.php
framework/Cache/lib/Horde/Cache/Null.php
framework/Cache/lib/Horde/Cache/Session.php
framework/Cache/lib/Horde/Cache/Sql.php
framework/Cache/lib/Horde/Cache/Stack.php
framework/Cache/lib/Horde/Cache/Xcache.php

index 4860d33..4c7828d 100644 (file)
@@ -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);
+        }
     }
 
     /**
index 9faa3d5..2067a1f 100644 (file)
@@ -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);
 
index 93ff1c6..c919654 100644 (file)
@@ -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;
     }
 
     /**
index cc36998..d4cf07c 100644 (file)
@@ -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);
index ac199e5..e9073eb 100644 (file)
@@ -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);
 
index 48fe8fa..872ba37 100644 (file)
@@ -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;
     }
 
index c8f7589..2726702 100644 (file)
@@ -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.');
+        }
     }
 
     /**
index 9dd3066..260b56f 100644 (file)
@@ -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)
index 61c69f5..e5ab165 100644 (file)
@@ -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);
+        }
     }
 
     /**
index 434be71..d075b9c 100644 (file)
@@ -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;
index 60fdca3..bd43061 100644 (file)
@@ -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);
+        }
     }
 
     /**