From 0fefb6277b9d7677f98a35844545422bf346cb77 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Fri, 25 Sep 2009 16:07:52 -0400 Subject: [PATCH] First attempt at optional caching of Horde_Routes generated mappings (Request #8563). --- framework/Routes/lib/Horde/Routes/Mapper.php | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/framework/Routes/lib/Horde/Routes/Mapper.php b/framework/Routes/lib/Horde/Routes/Mapper.php index 10dbfd5c7..8a0ca66c8 100644 --- a/framework/Routes/lib/Horde/Routes/Mapper.php +++ b/framework/Routes/lib/Horde/Routes/Mapper.php @@ -140,6 +140,18 @@ class Horde_Routes_Mapper public $utils; /** + * Cache + * @var Horde_Cache_Base + */ + public $cache; + + /** + * Cache lifetime for the same value of $this->matchList + * @var integer + */ + public $cacheLifetime = 86400; + + /** * Have regular expressions been created for all connected routes? * @var boolean */ @@ -302,12 +314,33 @@ class Horde_Routes_Mapper } /** + * Set an optional Horde_Cache_Base object for the created rules. + * + * @param Horde_Cache_Base $cache Cache object + */ + public function setCache(Horde_Cache_Base $cache) + { + $this->cache = $cache; + } + + /** * Create the generation hashes (arrays) for route lookups * * @return void */ protected function _createGens() { + // Checked for a cached generator dictionary for $this->matchList + if ($this->cache) { + $cacheKey = 'horde.routes.' . sha1(serialize($this->matchList)); + $cachedDict = $cache->get($cacheKey, $this->cacheLifetime); + if ($gendict = @unserialize($cachedDict)) { + $this->_gendict = $gendict; + $this->_createdGens = true; + return; + } + } + // Use keys temporarily to assemble the list to avoid excessive // list iteration testing with foreach. We include the '*' in the // case that a generate contains a controller/action that has no @@ -367,6 +400,12 @@ class Horde_Routes_Mapper if (!isset($gendict['*'])) { $gendict['*'] = array(); } + + // Write to the cache + if ($this->cache) { + $this->cache->set($cacheKey, serialize($gendict), $this->cacheLifetime); + } + $this->_gendict = $gendict; $this->_createdGens = true; } -- 2.11.0