summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorctrlaltca <ctrlaltca@gmail.com>2016-02-11 11:33:43 +0100
committerctrlaltca <ctrlaltca@gmail.com>2016-02-11 11:33:43 +0100
commit52f3d7b084f5f066f10c1691833383ebe924f588 (patch)
tree4d768b2367dd9dfda5e7fbe2ac1d8b151e0cedb8
parent4db58d8d7cdbf56f32c5644f90c7294c7ad0002d (diff)
parentcc9252cc2ac9f6972c039dd74cbe47c379727d71 (diff)
Merge pull request #565 from majuca/master
Issue #559, add support for memcached
-rw-r--r--framework/Caching/TMemCache.php51
-rw-r--r--framework/Data/DataGateway/TDataGatewayCommand.php4
2 files changed, 49 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);
+ }
}
/**
diff --git a/framework/Data/DataGateway/TDataGatewayCommand.php b/framework/Data/DataGateway/TDataGatewayCommand.php
index 246c192b..d314ca05 100644
--- a/framework/Data/DataGateway/TDataGatewayCommand.php
+++ b/framework/Data/DataGateway/TDataGatewayCommand.php
@@ -160,6 +160,8 @@ class TDataGatewayCommand extends TComponent
*/
public function findByPk($keys)
{
+ if($keys===null)
+ return null;
list($where, $parameters) = $this->getPrimaryKeyCondition((array)$keys);
$command = $this->getBuilder()->createFindCommand($where, $parameters);
$this->onCreateCommand($command, new TSqlCriteria($where,$parameters));
@@ -196,6 +198,8 @@ class TDataGatewayCommand extends TComponent
*/
public function deleteByPk($keys)
{
+ if(count($keys)==0)
+ return 0;
$where = $this->getCompositeKeyCondition((array)$keys);
$command = $this->getBuilder()->createDeleteCommand($where);
$this->onCreateCommand($command, new TSqlCriteria($where,$keys));