From: Michael M Slusarz Date: Mon, 6 Sep 2010 20:17:06 +0000 (-0600) Subject: Have Horde_Memcache implement Serializable X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=6ea8b57e2c33dbd9ba69e262a678b381a2f7e7e4;p=horde.git Have Horde_Memcache implement Serializable --- diff --git a/framework/Memcache/lib/Horde/Memcache.php b/framework/Memcache/lib/Horde/Memcache.php index a2eea7162..93e95434b 100644 --- a/framework/Memcache/lib/Horde/Memcache.php +++ b/framework/Memcache/lib/Horde/Memcache.php @@ -41,9 +41,9 @@ * @category Horde * @author Michael Slusarz * @author Didi Rieder - * @package Horde_Memcache + * @package Memcache */ -class Horde_Memcache +class Horde_Memcache implements Serializable { /** * The max storage size of the memcache server. This should be slightly @@ -53,6 +53,11 @@ class Horde_Memcache const MAX_SIZE = 1000000; /** + * Serializable version. + */ + const VERSION = 1; + + /** * Memcache object. * * @var Memcache @@ -73,13 +78,6 @@ class Horde_Memcache ); /** - * Allow large data items? - * - * @var boolean - */ - protected $_large = true; - - /** * A list of items known not to exist. * * @var array @@ -104,24 +102,60 @@ class Horde_Memcache { $this->_params = array_merge($this->_params, $params); $this->_params['prefix'] = (empty($this->_params['prefix'])) ? 'horde' : $this->_params['prefix']; - $this->_large = !empty($this->_params['large_items']); if (isset($params['logger'])) { $this->_logger = $params['logger']; } - $this->__wakeup(); + $this->_init(); + } + + /** + * Serialize. + * + * @return string Serialized representation of this object. + */ + public function serialize() + { + return serialize(array( + self::VERSION, + $this->_params, + $this->_logger + )); + } + + /** + * Unserialize. + * + * @param string $data Serialized data. + * + * @throws Exception + * @throws Horde_Exception + */ + public function unserialize($data) + { + $data = @unserialize($data); + if (!is_array($data) || + !isset($data[0]) || + ($data[0] != self::VERSION)) { + throw new Exception('Cache version change'); + } + + $this->_params = $data[1]; + $this->_logger = $data[2]; + + $this->_init(); } /** - * Do re-initialization on unserialize(). + * Do initialization. * * @throws Horde_Exception */ - public function __wakeup() + public function _init() { $servers = array(); - $this->_memcache = new Memcache; + $this->_memcache = new Memcache(); for ($i = 0, $n = count($this->_params['hostspec']); $i < $n; ++$i) { if ($this->_memcache->addServer($this->_params['hostspec'][$i], empty($this->_params['port'][$i]) ? 0 : $this->_params['port'][$i], !empty($this->_params['persistent']), !empty($this->_params['weight'][$i]) ? $this->_params['weight'][$i] : 1)) { $servers[] = $this->_params['hostspec'][$i] . (!empty($this->_params['port'][$i]) ? ':' . $this->_params['port'][$i] : ''); @@ -157,7 +191,7 @@ class Horde_Memcache */ public function delete($key, $timeout = 0) { - if ($this->_large) { + if (!empty($this->_params['large_items'])) { /* No need to delete the oversized parts - memcache's LRU * algorithm will eventually cause these pieces to be recycled. */ if (!isset($this->_noexist[$key . '_os'])) { @@ -191,7 +225,7 @@ class Horde_Memcache } $search_keys = $keys; - if ($this->_large) { + if (!empty($this->_params['large_items'])) { foreach ($keys as $val) { $os_keys[$val] = $search_keys[] = $val . '_os'; } @@ -305,7 +339,7 @@ class Horde_Memcache $len = strlen($var); } - if (!$this->_large && ($len > self::MAX_SIZE)) { + if (empty($this->_params['large_items']) && ($len > self::MAX_SIZE)) { return false; } @@ -320,7 +354,7 @@ class Horde_Memcache unset($this->_noexist[$curr_key]); } - if (($res !== false) && $this->_large) { + if (($res !== false) && !empty($this->_params['large_items'])) { $os_key = $this->_key($key . '_os'); if (--$i) { $this->_memcache->set($os_key, $i, 0, $expire); @@ -351,7 +385,7 @@ class Horde_Memcache $len = strlen($var); if ($len > self::MAX_SIZE) { - if ($this->_large) { + if (!empty($this->_params['large_items'])) { $res = $this->_memcache->get(array($this->_key($key), $this->_key($key . '_os'))); if (!empty($res)) { return $this->_set($key, $var, $expire, $len); @@ -361,7 +395,8 @@ class Horde_Memcache } if ($this->_memcache->replace($this->_key($key), $var, empty($this->_params['compression']) ? 0 : MEMCACHE_COMPRESSED, $expire)) { - if ($this->_large && !isset($this->_noexist[$key . '_os'])) { + if (!empty($this->_params['large_items']) && + !isset($this->_noexist[$key . '_os'])) { $this->_memcache->delete($this->_key($key . '_os')); } return true;