Move cache invalidation to Horde_Themes_Cache
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 23 Nov 2010 20:18:39 +0000 (13:18 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 23 Nov 2010 22:47:18 +0000 (15:47 -0700)
framework/Core/lib/Horde/Core/Factory/ThemesCache.php
framework/Core/lib/Horde/Themes/Cache.php

index d00efac..e90f86e 100644 (file)
 class Horde_Core_Factory_ThemesCache
 {
     /**
-     * The list of cache IDs mapped to instance IDs.
-     *
-     * @var array
-     */
-    private $_cacheids = array();
-
-    /**
      * The injector.
      *
      * @var Horde_Injector
@@ -70,44 +63,33 @@ class Horde_Core_Factory_ThemesCache
     {
         $sig = implode('|', array($app, $theme));
 
-        if (isset($this->_instances[$sig])) {
-            return $this->_instances[$sig];
-        }
-
-        if (!empty($GLOBALS['conf']['cachethemes'])) {
-            $cache = $this->_injector->getInstance('Horde_Cache');
-        }
-
-        if (!$cache || ($cache instanceof Horde_Cache_Null)) {
-            $instance = new Horde_Themes_Cache($app, $theme);
-        } else {
-            $id = $sig . '|' . $GLOBALS['registry']->getVersion($app);
-            if ($app != 'horde') {
-                $id .= '|' . $GLOBALS['registry']->getVersion('horde');
-            }
-
-            $cache_sig = hash('md5', $id);
-
-            try {
-                $instance = @unserialize($cache->get($cache_sig, 86400));
-            } catch (Exception $e) {
-                $instance = null;
+        if (!isset($this->_instances[$sig])) {
+            if (!empty($GLOBALS['conf']['cachethemes'])) {
+                $cache = $this->_injector->getInstance('Horde_Cache');
             }
 
-            if (!($instance instanceof Horde_Themes_Cache)) {
+            if (!$cache || ($cache instanceof Horde_Cache_Null)) {
                 $instance = new Horde_Themes_Cache($app, $theme);
-                $instance->build();
+            } else {
+                try {
+                    $instance = @unserialize($cache->get($sig, 86400));
+                } catch (Exception $e) {
+                    $instance = null;
+                }
+
+                if (!($instance instanceof Horde_Themes_Cache)) {
+                    $instance = new Horde_Themes_Cache($app, $theme);
+                    $instance->build();
+                }
 
-                if (empty($this->_cacheids)) {
+                if (empty($this->_instances)) {
                     register_shutdown_function(array($this, 'shutdown'));
                 }
             }
 
-            $this->_cacheids[$sig] = $cache_sig;
+            $this->_instances[$sig] = $instance;
         }
 
-        $this->_instances[$sig] = $instance;
-
         return $this->_instances[$sig];
     }
 
@@ -120,7 +102,7 @@ class Horde_Core_Factory_ThemesCache
 
         foreach ($this->_instances as $key => $val) {
             if ($val->changed) {
-                $cache->set($this->_cacheids[$key], serialize($val), 86400);
+                $cache->set($key, serialize($val), 86400);
             }
         }
     }
index 3294b14..5e732af 100644 (file)
@@ -207,6 +207,18 @@ class Horde_Themes_Cache implements Serializable
         return $out;
     }
 
+    /**
+     */
+    protected function _getExpireId()
+    {
+        $id = array($GLOBALS['registry']->getVersion($this->_app));
+        if ($this->_app != 'horde') {
+            $id[] = $GLOBALS['registry']->getVersion('horde');
+        }
+
+        return 'v:' . implode('|', $id);
+    }
+
     /* Serializable methods. */
 
     /**
@@ -214,6 +226,7 @@ class Horde_Themes_Cache implements Serializable
     public function serialize()
     {
         return serialize(array(
+            $this->_getExpireId(),
             $this->_app,
             $this->_data,
             $this->_theme
@@ -225,10 +238,15 @@ class Horde_Themes_Cache implements Serializable
     public function unserialize($data)
     {
         list(
+            $expire_id,
             $this->_app,
             $this->_data,
             $this->_theme
         ) = unserialize($data);
+
+        if ($expire_id != $this->_getExpireId()) {
+            throw new Exception('Cache invalidated');
+        }
     }
 
 }