From: Alexander Ebert Date: Sat, 26 Jan 2013 23:30:08 +0000 (+0100) Subject: Added support for "memcached" (dropped "memcache") X-Git-Tag: 2.0.0_Beta_1~537^2~1 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=831c0d19569eb17cfad49f9dc40aa310e23e57b7;p=GitHub%2FWoltLab%2FWCF.git Added support for "memcached" (dropped "memcache") --- diff --git a/com.woltlab.wcf/option.xml b/com.woltlab.wcf/option.xml index 0e1e37861b..9943fb60bc 100644 --- a/com.woltlab.wcf/option.xml +++ b/com.woltlab.wcf/option.xml @@ -69,7 +69,7 @@ general.cache - + general.cache @@ -385,22 +385,22 @@ debug:mail_debug_logfile_path,!mail_use_f_param,!mail_smtp_host,!mail_smtp_port, radioButton - + - - diff --git a/wcfsetup/install/files/lib/acp/page/CacheListPage.class.php b/wcfsetup/install/files/lib/acp/page/CacheListPage.class.php index 9e385dfada..f3750ed014 100755 --- a/wcfsetup/install/files/lib/acp/page/CacheListPage.class.php +++ b/wcfsetup/install/files/lib/acp/page/CacheListPage.class.php @@ -87,7 +87,7 @@ class CacheListPage extends AbstractPage { } break; - case 'wcf\system\cache\source\MemcacheCacheSource': + case 'wcf\system\cache\source\MemcachedCacheSource': // set version $this->cacheData['version'] = WCF_VERSION; diff --git a/wcfsetup/install/files/lib/system/cache/source/MemcacheAdapter.class.php b/wcfsetup/install/files/lib/system/cache/source/MemcacheAdapter.class.php deleted file mode 100644 index 174a6f9b29..0000000000 --- a/wcfsetup/install/files/lib/system/cache/source/MemcacheAdapter.class.php +++ /dev/null @@ -1,65 +0,0 @@ - - * @package com.woltlab.wcf - * @subpackage system.cache.source - * @category Community Framework - */ -class MemcacheAdapter extends SingletonFactory { - /** - * memcache object - * @var Memcache - */ - private $memcache = null; - - /** - * @see wcf\system\SingletonFactory::init() - */ - protected function init() { - if (!class_exists('Memcache')) { - throw new SystemException('memcache support is not enabled.'); - } - - // init memcache - $this->memcache = new \Memcache(); - - // add servers - $servers = explode("\n", StringUtil::unifyNewlines(CACHE_SOURCE_MEMCACHE_HOST)); - foreach ($servers as $server) { - $server = StringUtil::trim($server); - if (!empty($server)) { - $host = $server; - $port = 11211; // default memcache port - // get port - if (strpos($host, ':')) { - $parsedHost = explode(':', $host); - $host = $parsedHost[0]; - $port = $parsedHost[1]; - } - - $this->memcache->addServer($host, $port, CACHE_SOURCE_MEMCACHE_USE_PCONNECT); - } - } - - // test connection - $this->memcache->get('testing'); - } - - /** - * Returns the memcache object. - * - * @return Memcache - */ - public function getMemcache() { - return $this->memcache; - } -} diff --git a/wcfsetup/install/files/lib/system/cache/source/MemcacheCacheSource.class.php b/wcfsetup/install/files/lib/system/cache/source/MemcacheCacheSource.class.php deleted file mode 100644 index 4397ad73b5..0000000000 --- a/wcfsetup/install/files/lib/system/cache/source/MemcacheCacheSource.class.php +++ /dev/null @@ -1,189 +0,0 @@ - - * @package com.woltlab.wcf - * @subpackage system.cache.source - * @category Community Framework - */ -class MemcacheCacheSource implements ICacheSource { - /** - * MemcacheAdapter object - * @var wcf\system\cache\source\MemcacheAdapter - */ - protected $adapter = null; - - /** - * list of cache resources - * @var array - */ - protected $cacheResources = null; - - /** - * list of new cache resources - * @var array - */ - protected $newLogEntries = array(); - - /** - * list of obsolete resources - * @var array - */ - protected $obsoleteLogEntries = array(); - - /** - * Creates a new MemcacheCacheSource object. - */ - public function __construct() { - $this->adapter = MemcacheAdapter::getInstance(); - } - - /** - * Returns the memcache adapter. - * - * @return MemcacheAdapter - */ - public function getAdapter() { - return $this->adapter; - } - - // internal log functions - /** - * Loads the cache log. - */ - protected function loadLog() { - if ($this->cacheResources === null) { - $this->cacheResources = array(); - $sql = "SELECT * - FROM wcf".WCF_N."_cache_resource"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(); - while ($row = $statement->fetchArray()) { - $this->cacheResources[] = $row['cacheResource']; - } - } - } - - /** - * Saves modifications of the cache log. - */ - protected function updateLog() { - if (!empty($this->newLogEntries)) { - $sql = "DELETE FROM wcf".WCF_N."_cache_resource - WHERE cacheResource = ?"; - $statement = WCF::getDB()->prepareStatement($sql); - foreach ($this->newLogEntries as $entry) { - $statement->execute(array($entry)); - } - - $sql = "INSERT INTO wcf".WCF_N."_cache_resource - (cacheResource) - VALUES (?)"; - $statement = WCF::getDB()->prepareStatement($sql); - foreach ($this->newLogEntries as $entry) { - $statement->execute(array($entry)); - } - - } - - if (!empty($this->obsoleteLogEntries)) { - $sql = "DELETE FROM wcf".WCF_N."_cache_resource - WHERE cacheResource = ?"; - $statement = WCF::getDB()->prepareStatement($sql); - foreach ($this->obsoleteLogEntries as $entry) { - $statement->execute(array($entry)); - } - } - } - - /** - * Adds a cache resource to cache log. - * - * @param string $cacheResource - */ - protected function addToLog($cacheResource) { - $this->newLogEntries[] = $cacheResource; - } - - /** - * Removes an obsolete cache resource from cache log. - * - * @param string $cacheResource - */ - protected function removeFromLog($cacheResource) { - $this->obsoleteLogEntries[] = $cacheResource; - } - - // CacheSource implementations - /** - * @see wcf\system\cache\source\ICacheSource::get() - */ - public function get(array $cacheResource) { - $value = $this->getAdapter()->getMemcache()->get($cacheResource['file']); - if ($value === false) return null; - return $value; - } - - /** - * @see wcf\system\cache\source\ICacheSource::set() - */ - public function set(array $cacheResource, $value) { - $this->getAdapter()->getMemcache()->set($cacheResource['file'], $value, MEMCACHE_COMPRESSED, $cacheResource['maxLifetime']); - $this->addToLog($cacheResource['file']); - } - - /** - * @see wcf\system\cache\source\ICacheSource::delete() - */ - public function delete(array $cacheResource) { - $this->getAdapter()->getMemcache()->delete($cacheResource['file']); - $this->removeFromLog($cacheResource['file']); - } - - /** - * @see wcf\system\cache\source\ICacheSource::clear() - */ - public function clear($directory, $filepattern) { - $this->loadLog(); - $pattern = preg_quote(FileUtil::addTrailingSlash($directory), '%').str_replace('*', '.*', str_replace('.', '\.', $filepattern)); - foreach ($this->cacheResources as $cacheResource) { - if (preg_match('%^'.$pattern.'$%i', $cacheResource)) { - $this->getAdapter()->getMemcache()->delete($cacheResource); - $this->removeFromLog($cacheResource); - } - } - } - - /** - * @see wcf\system\cache\source\ICacheSource::flush() - */ - public function flush() { - // clear cache - $this->getAdapter()->getMemcache()->flush(); - - // clear log - $this->newLogEntries = $this->obsoleteLogEntries = array(); - - $sql = "DELETE FROM wcf".WCF_N."_cache_resource"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(); - } - - /** - * @see wcf\system\cache\source\ICacheSource::close() - */ - public function close() { - // update log - $this->updateLog(); - - // close connection - if ($this->getAdapter() !== null && $this->getAdapter()->getMemcache() !== null) $this->getAdapter()->getMemcache()->close(); - } -} diff --git a/wcfsetup/install/files/lib/system/cache/source/MemcachedAdapter.class.php b/wcfsetup/install/files/lib/system/cache/source/MemcachedAdapter.class.php new file mode 100644 index 0000000000..36e235bd31 --- /dev/null +++ b/wcfsetup/install/files/lib/system/cache/source/MemcachedAdapter.class.php @@ -0,0 +1,80 @@ + + * @package com.woltlab.wcf + * @subpackage system.cache.source + * @category Community Framework + */ +class MemcachedAdapter extends SingletonFactory { + /** + * memcached object + * @var \Memcached + */ + private $memcached = null; + + /** + * @see wcf\system\SingletonFactory::init() + */ + protected function init() { + if (!class_exists('Memcached')) { + throw new SystemException('memcached support is not enabled.'); + } + + // init memcached + if (CACHE_SOURCE_MEMCACHED_USE_PCONNECT) { + $this->memcached = new \Memcached('wcf_memcached'); + } + else { + $this->memcached = new \Memcached(); + } + + // add servers + $tmp = explode("\n", StringUtil::unifyNewlines(CACHE_SOURCE_MEMCACHED_HOST)); + $servers = array(); + $defaultWeight = floor(100 / count($tmp)); + foreach ($tmp as $server) { + $server = StringUtil::trim($server); + if (!empty($server)) { + $host = $server; + $port = 11211; // default memcached port + $weight = $defaultWeight; + + // get port + if (strpos($host, ':')) { + $parsedHost = explode(':', $host); + $host = $parsedHost[0]; + $port = $parsedHost[1]; + + if (isset($parsedHost[2])) { + $weight = $parsedHost[2]; + } + } + + $servers[] = array($host, $port, $weight); + } + } + + $this->memcached->addServers($servers); + + // test connection + $this->memcached->get('testing'); + } + + /** + * Returns the memcached object. + * + * @return \Memcached + */ + public function getMemcached() { + return $this->memcached; + } +} diff --git a/wcfsetup/install/files/lib/system/cache/source/MemcachedCacheSource.class.php b/wcfsetup/install/files/lib/system/cache/source/MemcachedCacheSource.class.php new file mode 100644 index 0000000000..16d7fe19fc --- /dev/null +++ b/wcfsetup/install/files/lib/system/cache/source/MemcachedCacheSource.class.php @@ -0,0 +1,189 @@ + + * @package com.woltlab.wcf + * @subpackage system.cache.source + * @category Community Framework + */ +class MemcachedCacheSource implements ICacheSource { + /** + * MemcachedAdapter object + * @var wcf\system\cache\source\MemcachedAdapter + */ + protected $adapter = null; + + /** + * list of cache resources + * @var array + */ + protected $cacheResources = null; + + /** + * list of new cache resources + * @var array + */ + protected $newLogEntries = array(); + + /** + * list of obsolete resources + * @var array + */ + protected $obsoleteLogEntries = array(); + + /** + * Creates a new MemcachedCacheSource object. + */ + public function __construct() { + $this->adapter = MemcachedAdapter::getInstance(); + } + + /** + * Returns the memcached adapter. + * + * @return \MemcachedAdapter + */ + public function getAdapter() { + return $this->adapter; + } + + // internal log functions + /** + * Loads the cache log. + */ + protected function loadLog() { + if ($this->cacheResources === null) { + $this->cacheResources = array(); + $sql = "SELECT * + FROM wcf".WCF_N."_cache_resource"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(); + while ($row = $statement->fetchArray()) { + $this->cacheResources[] = $row['cacheResource']; + } + } + } + + /** + * Saves modifications of the cache log. + */ + protected function updateLog() { + if (!empty($this->newLogEntries)) { + $sql = "DELETE FROM wcf".WCF_N."_cache_resource + WHERE cacheResource = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + foreach ($this->newLogEntries as $entry) { + $statement->execute(array($entry)); + } + + $sql = "INSERT INTO wcf".WCF_N."_cache_resource + (cacheResource) + VALUES (?)"; + $statement = WCF::getDB()->prepareStatement($sql); + foreach ($this->newLogEntries as $entry) { + $statement->execute(array($entry)); + } + + } + + if (!empty($this->obsoleteLogEntries)) { + $sql = "DELETE FROM wcf".WCF_N."_cache_resource + WHERE cacheResource = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + foreach ($this->obsoleteLogEntries as $entry) { + $statement->execute(array($entry)); + } + } + } + + /** + * Adds a cache resource to cache log. + * + * @param string $cacheResource + */ + protected function addToLog($cacheResource) { + $this->newLogEntries[] = $cacheResource; + } + + /** + * Removes an obsolete cache resource from cache log. + * + * @param string $cacheResource + */ + protected function removeFromLog($cacheResource) { + $this->obsoleteLogEntries[] = $cacheResource; + } + + // CacheSource implementations + /** + * @see wcf\system\cache\source\ICacheSource::get() + */ + public function get(array $cacheResource) { + $value = $this->getAdapter()->getMemcached()->get($cacheResource['file']); + if ($value === false) return null; + return $value; + } + + /** + * @see wcf\system\cache\source\ICacheSource::set() + */ + public function set(array $cacheResource, $value) { + $this->getAdapter()->getMemcached()->set($cacheResource['file'], $value, MEMCACHE_COMPRESSED, $cacheResource['maxLifetime']); + $this->addToLog($cacheResource['file']); + } + + /** + * @see wcf\system\cache\source\ICacheSource::delete() + */ + public function delete(array $cacheResource) { + $this->getAdapter()->getMemcached()->delete($cacheResource['file']); + $this->removeFromLog($cacheResource['file']); + } + + /** + * @see wcf\system\cache\source\ICacheSource::clear() + */ + public function clear($directory, $filepattern) { + $this->loadLog(); + $pattern = preg_quote(FileUtil::addTrailingSlash($directory), '%').str_replace('*', '.*', str_replace('.', '\.', $filepattern)); + foreach ($this->cacheResources as $cacheResource) { + if (preg_match('%^'.$pattern.'$%i', $cacheResource)) { + $this->getAdapter()->getMemcached()->delete($cacheResource); + $this->removeFromLog($cacheResource); + } + } + } + + /** + * @see wcf\system\cache\source\ICacheSource::flush() + */ + public function flush() { + // clear cache + $this->getAdapter()->getMemcached()->flush(); + + // clear log + $this->newLogEntries = $this->obsoleteLogEntries = array(); + + $sql = "DELETE FROM wcf".WCF_N."_cache_resource"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(); + } + + /** + * @see wcf\system\cache\source\ICacheSource::close() + */ + public function close() { + // update log + $this->updateLog(); + + // close connection + if ($this->getAdapter() !== null && $this->getAdapter()->getMemcached() !== null) $this->getAdapter()->getMemcached()->close(); + } +} diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index aa3960936a..49dfadc4b9 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -275,21 +275,21 @@ - - - + + + - + - - memcached.org.]]> + + memcached.org.]]>