From cc9252cc2ac9f6972c039dd74cbe47c379727d71 Mon Sep 17 00:00:00 2001 From: Jean-Luc Gyger Date: Thu, 11 Feb 2016 10:41:14 +0100 Subject: Add support for memcached --- framework/Caching/TMemCache.php | 51 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 6 deletions(-) (limited to 'framework') diff --git a/framework/Caching/TMemCache.php b/framework/Caching/TMemCache.php index 2b200156..8d091194 100644 --- a/framework/Caching/TMemCache.php +++ b/framework/Caching/TMemCache.php @@ -73,7 +73,7 @@ * of TMemCache. * * Automatic compression of values may be used (using zlib extension) by setting {@link getThreshold Threshold} and {@link getMinSavings MinSavings} properties. - * NB : MemCache server(s) must be restarted to apply settings. Require (PECL memcache >= 2.0.0). + * NB : MemCache server(s) must be restarted to apply settings. Require (PECL memcache >= 2.0.0) or memcached if {@link useMemcached} is true. * * @author Qiang Xue * @package System.Caching @@ -129,6 +129,14 @@ class TMemCache extends TCache private $_failureCallback = null; + /** + * @var boolean whether to use memcached or memcache as the underlying caching extension. + * If true {@link http://pecl.php.net/package/memcached memcached} will be used. + * If false {@link http://pecl.php.net/package/memcache memcache}. will be used. + * Defaults to false. + */ + private $_useMemcached=false; + /** * @var array list of servers available */ @@ -140,7 +148,7 @@ class TMemCache extends TCache */ public function __destruct() { - if($this->_cache!==null) + if($this->_cache!==null && !$this->_useMemcached) $this->_cache->close(); } @@ -155,9 +163,12 @@ class TMemCache extends TCache */ public function init($config) { - if(!extension_loaded('memcache')) + if(!extension_loaded('memcache') && !$this->_useMemcached) throw new TConfigurationException('memcache_extension_required'); - $this->_cache=new Memcache; + if(!extension_loaded('memcached') && $this->_useMemcached) + throw new TConfigurationException('memcached_extension_required'); + + $this->_cache = $this->_useMemcached ? new Memcached : new Memcache; $this->loadConfig($config); if(count($this->_servers)) { @@ -258,7 +269,27 @@ class TMemCache extends TCache else $this->_port=TPropertyValue::ensureInteger($value); } + + /** + * @return boolean if memcached instead memcache + */ + public function getUseMemcached() + { + return $this->_useMemcached; + } + /** + * @param string if memcached instead memcache + * @throws TInvalidOperationException if the module is already initialized + */ + public function setUseMemcached($value) + { + if($this->_initialized) + throw new TInvalidOperationException('memcache_host_unchangeable'); + else + $this->_useMemcached=$value; + } + /** * @return integer minimum value length before attempting to compress */ @@ -321,7 +352,11 @@ class TMemCache extends TCache */ protected function setValue($key,$value,$expire) { - return $this->_cache->set($key,$value,0,$expire); + if($this->_useMemcached) { + return $this->_cache->set($key,$value,$expire); + } else { + return $this->_cache->set($key,$value,0,$expire); + } } /** @@ -335,7 +370,11 @@ class TMemCache extends TCache */ protected function addValue($key,$value,$expire) { - return $this->_cache->add($key,$value,0,$expire); + if($this->_useMemcached) { + $this->_cache->add($key,$value,$expire); + } else { + return $this->_cache->add($key,$value,0,$expire); + } } /** -- cgit v1.2.3