Don't store updated cached registry values until shutdown
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 16 Nov 2010 07:39:10 +0000 (00:39 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 16 Nov 2010 07:39:10 +0000 (00:39 -0700)
framework/Core/lib/Horde/Registry.php

index f0147b0..04b079d 100644 (file)
@@ -29,25 +29,25 @@ class Horde_Registry
     const HOOK_FATAL = 4;
 
     /**
-     * Cached information.
+     * Hash storing information on each registry-aware application.
      *
      * @var array
      */
-    protected $_cache = array();
+    public $applications = array();
 
     /**
-     * NLS cached information.
+     * The application that called appInit().
      *
-     * @var array
+     * @var string
      */
-    protected $_nlscache = array();
+    public $initialApp;
 
     /**
-     * The last modified time of the newest modified registry file.
+     * NLS configuration.
      *
-     * @var integer
+     * @var array
      */
-    protected $_regmtime;
+    public $nlsconfig = array();
 
     /**
      * Stack of in-use applications.
@@ -64,25 +64,32 @@ class Horde_Registry
     protected $_apis = array();
 
     /**
-     * Hash storing information on each registry-aware application.
+     * Cached information.
      *
      * @var array
      */
-    public $applications = array();
+    protected $_cache = array();
 
     /**
-     * The application that called appInit().
+     * NLS cached information.
      *
-     * @var string
+     * @var array
      */
-    public $initialApp;
+    protected $_nlscache = array();
 
     /**
-     * NLS configuration.
+     * The last modified time of the newest modified registry file.
+     *
+     * @var integer
+     */
+    protected $_regmtime;
+
+    /**
+     * The list of cache entries to save on shutdown.
      *
      * @var array
      */
-    public $nlsconfig = array();
+    protected $_savecache = array();
 
     /**
      * Application bootstrap initialization.
@@ -411,6 +418,27 @@ class Horde_Registry
      */
     public function shutdown()
     {
+        /* Store cache entries to persistent storage. */
+        if (!empty($this->_savecache)) {
+            $ob = $GLOBALS['injector']->getInstance('Horde_Cache');
+
+            foreach ($this->_savecache as $key => $val) {
+                if ($val) {
+                    // Entry has been updated.
+                    $data = serialize($this->_cache[$key]);
+                    $md5sum = hash('md5', $data);
+                    $GLOBALS['session']->set('horde', 'registry/' . $key, $md5sum);
+                    $id = $this->_getCacheId($key, false) . '|' . $md5sum;
+                    if ($ob->set($id, $data, 86400)) {
+                        Horde::logMessage('Horde_Registry: stored ' . $key . ' with cache ID ' . $id, 'DEBUG');
+                    }
+                } elseif ($id = $this->_getCacheId($name)) {
+                    // Entry has been deleted.
+                    $ob->expire($id);
+                }
+            }
+        }
+
         /* Register access key logger for translators. */
         if (!empty($GLOBALS['conf']['log_accesskeys'])) {
             Horde::getAccessKey(null, null, true);
@@ -458,8 +486,9 @@ class Horde_Registry
     public function clearCache()
     {
         $GLOBALS['session']->remove('horde', 'registry/');
-        $this->_saveCacheVar('api', true);
-        $this->_saveCacheVar('appcache', true);
+        $this->_cache = array();
+        $this->_savecache['api'] = false;
+        $this->_savecache['appcache'] = false;
     }
 
     /**
@@ -470,7 +499,7 @@ class Horde_Registry
     protected function _loadApplicationsCache($vhost)
     {
         /* First, try to load from cache. */
-        if ($this->_loadCacheVar('appcache')) {
+        if ($this->_loadCache('appcache')) {
             $this->applications = $this->_cache['appcache'][0];
             $this->_cache['interfaces'] = $this->_cache['appcache'][1];
             return;
@@ -549,7 +578,7 @@ class Horde_Registry
             // Index 1
             $this->_cache['interfaces']
         );
-        $this->_saveCacheVar('appcache');
+        $this->_savecache['appcache'] = true;
     }
 
     /**
@@ -560,7 +589,7 @@ class Horde_Registry
     protected function _loadApiCache()
     {
         /* First, try to load from cache. */
-        if ($this->_loadCacheVar('api')) {
+        if ($this->_loadCache('api')) {
             return;
         }
 
@@ -587,7 +616,7 @@ class Horde_Registry
             }
         }
 
-        $this->_saveCacheVar('api');
+        $this->_savecache['api'] = true;
     }
 
     /**
@@ -1291,13 +1320,13 @@ class Horde_Registry
      */
     public function importConfig($app)
     {
-        if (($app != 'horde') && !$this->_loadCacheVar('conf-' . $app)) {
+        if (($app != 'horde') && !$this->_loadCache('conf-' . $app)) {
             $appConfig = Horde::loadConfiguration('conf.php', 'conf', $app);
             if (empty($appConfig)) {
                 $appConfig = array();
             }
             $this->_cache['conf-' . $app] = $appConfig;
-            $this->_saveCacheVar('conf-' . $app);
+            $this->_savecache['conf-' . $app] = true;
         }
 
         $GLOBALS['conf'] = Horde_Array::array_merge_recursive_overwrite($this->_cache['conf-horde'], $this->_cache['conf-' . $app]);
@@ -1492,38 +1521,13 @@ class Horde_Registry
     }
 
     /**
-     * Saves a cache variable.
-     *
-     * @param string $name     Cache variable name.
-     * @param boolean $expire  Expire the entry?
-     */
-    protected function _saveCacheVar($name, $expire = false)
-    {
-        $ob = $GLOBALS['injector']->getInstance('Horde_Cache');
-
-        if ($expire) {
-            if ($id = $this->_getCacheId($name)) {
-                $ob->expire($id);
-            }
-        } else {
-            $data = serialize($this->_cache[$name]);
-            $md5sum = hash('md5', $data);
-            $GLOBALS['session']->set('horde', 'registry/' . $name, $md5sum);
-            $id = $this->_getCacheId($name, false) . '|' . $md5sum;
-            if ($ob->set($id, $data, 86400)) {
-                Horde::logMessage('Horde_Registry: stored ' . $name . ' with cache ID ' . $id, 'DEBUG');
-            }
-        }
-    }
-
-    /**
      * Retrieves a cache variable.
      *
      * @param string $name  Cache variable name.
      *
      * @return boolean  True if value loaded from cache.
      */
-    protected function _loadCacheVar($name)
+    protected function _loadCache($name)
     {
         if (isset($this->_cache[$name])) {
             return true;