summaryrefslogtreecommitdiff
path: root/framework/Caching/TMemCache.php
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Caching/TMemCache.php')
-rw-r--r--framework/Caching/TMemCache.php51
1 files changed, 45 insertions, 6 deletions
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 <qiang.xue@gmail.com>
* @package System.Caching
@@ -130,6 +130,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
*/
private $_servers=array();
@@ -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,8 +269,28 @@ 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
*/
public function getThreshold()
@@ -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);
+ }
}
/**