Skip to content

Commit

Permalink
Make MemcachedEngine more friendly without php extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Kyselov committed Jul 12, 2014
1 parent 6488669 commit 8f8b15d
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Cache/Engine/MemcachedEngine.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Cake\Cache\CacheEngine; use Cake\Cache\CacheEngine;
use Cake\Error; use Cake\Error;
use Cake\Utility\Inflector; use Cake\Utility\Inflector;
use \Memcached;


/** /**
* Memcached storage engine for cache. Memcached has some limitations in the amount of * Memcached storage engine for cache. Memcached has some limitations in the amount of
Expand Down Expand Up @@ -60,7 +59,7 @@ class MemcachedEngine extends CacheEngine {
* - `servers` String or array of memcached servers. If an array MemcacheEngine will use * - `servers` String or array of memcached servers. If an array MemcacheEngine will use
* them as a pool. * them as a pool.
* - `options` - Additional options for the memcached client. Should be an array of option => value. * - `options` - Additional options for the memcached client. Should be an array of option => value.
* Use the Memcached::OPT_* constants as keys. * Use the \Memcached::OPT_* constants as keys.
* *
* @var array * @var array
*/ */
Expand All @@ -85,11 +84,7 @@ class MemcachedEngine extends CacheEngine {
* *
* @var array * @var array
*/ */
protected $_serializers = [ protected $_serializers = [];
'igbinary' => Memcached::SERIALIZER_IGBINARY,
'json' => Memcached::SERIALIZER_JSON,
'php' => Memcached::SERIALIZER_PHP
];


/** /**
* Initialize the Cache Engine * Initialize the Cache Engine
Expand All @@ -101,16 +96,21 @@ class MemcachedEngine extends CacheEngine {
* @throws \Cake\Error\Exception when you try use authentication without Memcached compiled with SASL support * @throws \Cake\Error\Exception when you try use authentication without Memcached compiled with SASL support
*/ */
public function init(array $config = []) { public function init(array $config = []) {
if (!class_exists('Memcached')) { if (!class_exists('\\Memcached')) {
return false; return false;
} }


if (!isset($config['prefix'])) { if (!isset($config['prefix'])) {
$config['prefix'] = Inflector::slug(APP_DIR) . '_'; $config['prefix'] = Inflector::slug(APP_DIR) . '_';
} }


if (defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK) { $this->_serializers = [
$this->_serializers['msgpack'] = Memcached::SERIALIZER_MSGPACK; 'igbinary' => \Memcached::SERIALIZER_IGBINARY,
'json' => \Memcached::SERIALIZER_JSON,
'php' => \Memcached::SERIALIZER_PHP
];
if (defined('\\Memcached::HAVE_MSGPACK') && \Memcached::HAVE_MSGPACK) {
$this->_serializers['msgpack'] = \Memcached::SERIALIZER_MSGPACK;
} }


parent::init($config); parent::init($config);
Expand Down Expand Up @@ -177,16 +177,16 @@ protected function _setOptions() {
); );
} }


if ($serializer !== 'php' && !constant('Memcached::HAVE_' . strtoupper($serializer))) { if ($serializer !== 'php' && !constant('\\Memcached::HAVE_' . strtoupper($serializer))) {
throw new Error\Exception( throw new Error\Exception(
sprintf('Memcached extension is not compiled with %s support', $serializer) sprintf('Memcached extension is not compiled with %s support', $serializer)
); );
} }


$this->_Memcached->setOption(Memcached::OPT_SERIALIZER, $this->_serializers[$serializer]); $this->_Memcached->setOption(\Memcached::OPT_SERIALIZER, $this->_serializers[$serializer]);


// Check for Amazon ElastiCache instance // Check for Amazon ElastiCache instance
if (defined('Memcached::OPT_CLIENT_MODE') && defined('Memcached::DYNAMIC_CLIENT_MODE')) { if (defined('\\Memcached::OPT_CLIENT_MODE') && defined('\\Memcached::DYNAMIC_CLIENT_MODE')) {
$this->_Memcached->setOption(\Memcached::OPT_CLIENT_MODE, \Memcached::DYNAMIC_CLIENT_MODE); $this->_Memcached->setOption(\Memcached::OPT_CLIENT_MODE, \Memcached::DYNAMIC_CLIENT_MODE);
} }


Expand Down

0 comments on commit 8f8b15d

Please sign in to comment.