Skip to content

Commit

Permalink
Cache server prefix; use FNV1-32 if available
Browse files Browse the repository at this point in the history
Hashing/serialization is expensive, so cache results.
And no reason to use SHA-1 since we simply need uniqueid
  • Loading branch information
slusarz committed Apr 1, 2014
1 parent a10c4ef commit 2673cd5
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions framework/Db/lib/Horde/Db/Adapter/Base.php
Expand Up @@ -79,6 +79,13 @@ abstract class Horde_Db_Adapter_Base implements Horde_Db_Adapter
*/
protected $_cache;

/**
* Cache prefix.
*
* @var string
*/
protected $_cachePrefix;

/**
* Log object.
*
Expand Down Expand Up @@ -362,8 +369,7 @@ public function resetRuntime()
*/
public function cacheWrite($key, $value)
{
$key = get_class($this) . hash('sha1', serialize($this->_config)) . $key;
$this->_cache->set($key, $value);
$this->_cache->set($this->_cacheKey($key), $value);
}

/**
Expand All @@ -380,8 +386,7 @@ public function cacheWrite($key, $value)
*/
public function cacheRead($key)
{
$key = get_class($this) . hash('sha1', serialize($this->_config)) . $key;
return $this->_cache->get($key, 0);
return $this->_cache->get($this->_cacheKey($key), 0);
}


Expand Down Expand Up @@ -781,4 +786,21 @@ protected function _formatLogEntry($message, $sql)
{
return "SQL $message \n\t" . wordwrap(preg_replace("/\s+/", ' ', $sql), 70, "\n\t ", 1);
}

/**
* Returns the prefixed cache key to use.
*
* @param string $key A cache key.
*
* @return string Prefixed cache key.
*/
protected function _cacheKey($key)
{
if (!isset($this->_cachePrefix)) {
$this->_cachePrefix = get_class($this) . hash((PHP_MINOR_VERSION >= 4) ? 'fnv132' : 'sha1', serialize($this->_config));
}

return $this->_cachePrefix . $key;
}

}

0 comments on commit 2673cd5

Please sign in to comment.