/**
  * Initialize the Cache Engine
  *
  * Called automatically by the cache frontend
  * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  *
  * @param array $settings array of setting for the engine
  * @return bool True if the engine has been successfully initialized, false if not
  * @throws CacheException when you try use authentication without Memcached compiled with SASL support
  */
 public function init($settings = array())
 {
     if (!class_exists('Memcached')) {
         return false;
     }
     if (!isset($settings['prefix'])) {
         $settings['prefix'] = Inflector::slug(APP_DIR) . '_';
     }
     if (defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK) {
         $this->_serializers['msgpack'] = Memcached::SERIALIZER_MSGPACK;
     }
     $settings += array('engine' => 'Memcached', 'servers' => array('127.0.0.1'), 'compress' => false, 'persistent' => false, 'login' => null, 'password' => null, 'serialize' => 'php', 'options' => array());
     parent::init($settings);
     if (!is_array($this->settings['servers'])) {
         $this->settings['servers'] = array($this->settings['servers']);
     }
     if (isset($this->_Memcached)) {
         return true;
     }
     if (!$this->settings['persistent']) {
         $this->_Memcached = new Memcached();
     } else {
         $this->_Memcached = new Memcached((string) $this->settings['persistent']);
     }
     $this->_setOptions();
     if (count($this->_Memcached->getServerList())) {
         return true;
     }
     $servers = array();
     foreach ($this->settings['servers'] as $server) {
         $servers[] = $this->_parseServerString($server);
     }
     if (!$this->_Memcached->addServers($servers)) {
         return false;
     }
     if ($this->settings['login'] !== null && $this->settings['password'] !== null) {
         if (!method_exists($this->_Memcached, 'setSaslAuthData')) {
             throw new CacheException(__d('cake_dev', 'Memcached extension is not build with SASL support'));
         }
         $this->_Memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
         $this->_Memcached->setSaslAuthData($this->settings['login'], $this->settings['password']);
     }
     if (is_array($this->settings['options'])) {
         foreach ($this->settings['options'] as $opt => $value) {
             $this->_Memcached->setOption($opt, $value);
         }
     }
     return true;
 }
 /**
  * Initializes
  */
 private function initialize($config)
 {
     if (empty($config['servers'])) {
         return false;
     }
     if (defined('\\Memcached::OPT_REMOVE_FAILED_SERVERS')) {
         $this->_memcache->setOption(\Memcached::OPT_REMOVE_FAILED_SERVERS, true);
     }
     if (isset($config['aws_autodiscovery']) && $config['aws_autodiscovery'] && defined('\\Memcached::OPT_CLIENT_MODE') && defined('\\Memcached::DYNAMIC_CLIENT_MODE')) {
         $this->_memcache->setOption(\Memcached::OPT_CLIENT_MODE, \Memcached::DYNAMIC_CLIENT_MODE);
     }
     foreach ((array) $config['servers'] as $server) {
         if (substr($server, 0, 5) == 'unix:') {
             $this->_memcache->addServer(trim($server), 0);
         } else {
             list($ip, $port) = explode(':', $server);
             $this->_memcache->addServer(trim($ip), (int) trim($port));
         }
     }
     if (isset($config['username']) && !empty($config['username']) && method_exists($this->_memcache, 'setSaslAuthData') && ini_get('memcached.use_sasl')) {
         $this->_memcache->setSaslAuthData($config['username'], $config['password']);
     }
     return true;
 }
 /**
  * Initialize the Cache Engine
  *
  * Called automatically by the cache frontend
  *
  * @param array $config array of setting for the engine
  * @return bool True if the engine has been successfully initialized, false if not
  * @throws \Cake\Error\Exception when you try use authentication without Memcached compiled with SASL support
  */
 public function init(array $config = [])
 {
     if (!class_exists('Memcached')) {
         return false;
     }
     if (!isset($config['prefix'])) {
         $config['prefix'] = Inflector::slug(APP_DIR) . '_';
     }
     if (defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK) {
         $this->_serializers['msgpack'] = Memcached::SERIALIZER_MSGPACK;
     }
     parent::init($config);
     if (isset($config['servers'])) {
         $this->config('servers', $config['servers'], false);
     }
     if (!is_array($this->_config['servers'])) {
         $this->_config['servers'] = [$this->_config['servers']];
     }
     if (isset($this->_Memcached)) {
         return true;
     }
     $this->_Memcached = new \Memcached($this->_config['persistent'] ? (string) $this->_config['persistent'] : null);
     $this->_setOptions();
     if (count($this->_Memcached->getServerList())) {
         return true;
     }
     $servers = [];
     foreach ($this->_config['servers'] as $server) {
         $servers[] = $this->_parseServerString($server);
     }
     if (!$this->_Memcached->addServers($servers)) {
         return false;
     }
     if ($this->_config['login'] !== null && $this->_config['password'] !== null) {
         if (!method_exists($this->_Memcached, 'setSaslAuthData')) {
             throw new Error\Exception('Memcached extension is not build with SASL support');
         }
         $this->_Memcached->setSaslAuthData($this->_config['login'], $this->_config['password']);
     }
     return true;
 }
Beispiel #4
0
 /**
  * 获得Memcache连接
  * 
  * @return Memcache Memcache连接
  */
 public static function get_memcached()
 {
     if (Cache::$memcached === null) {
         Cache::$memcached = new Memcached('ocs');
         if (count(Cache::$memcached->getServerList()) == 0) {
             /*建立连接前,先判断*/
             /*所有option都要放在判断里面,因为有的option会导致重连,让长连接变短连接!*/
             Cache::$memcached->setOption(Memcached::OPT_COMPRESSION, false);
             Cache::$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
             /* addServer 代码必须在判断里面,否则相当于重复建立’ocs’这个连接池,可能会导致客户端php程序异常*/
             foreach (Conf::$memcached as $k => $memconf) {
                 Cache::$memcached->addServer($memconf[0], $memconf[1]);
             }
             if (Conf::$memcached_un) {
                 Cache::$memcached->setSaslAuthData(Conf::$memcached_un, Conf::$memcached_pwd);
             }
         }
         return Cache::$memcached;
     } else {
         return Cache::$memcached;
     }
 }
 /**
  * Initialize the Cache Engine
  *
  * Called automatically by the cache frontend
  * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  *
  * @param array $settings array of setting for the engine
  * @return boolean True if the engine has been successfully initialized, false if not
  * @throws CacheException when you try use authentication without Memcached compiled with SASL support
  */
 public function init($settings = array())
 {
     if (!class_exists('Memcached')) {
         return false;
     }
     if (!isset($settings['prefix'])) {
         $settings['prefix'] = Inflector::slug(APP_DIR) . '_';
     }
     $settings += array('engine' => 'Memcached', 'servers' => array('127.0.0.1'), 'compress' => false, 'persistent' => false, 'login' => null, 'password' => null, 'serialize' => 'php');
     parent::init($settings);
     if (!is_array($this->settings['servers'])) {
         $this->settings['servers'] = array($this->settings['servers']);
     }
     if (isset($this->_Memcached)) {
         return true;
     }
     $this->_Memcached = new Memcached($this->settings['persistent'] ? (string) $this->settings['persistent'] : null);
     $this->_setOptions();
     if (count($this->_Memcached->getServerList())) {
         return true;
     }
     $servers = array();
     foreach ($this->settings['servers'] as $server) {
         $servers[] = $this->_parseServerString($server);
     }
     if (!$this->_Memcached->addServers($servers)) {
         return false;
     }
     if ($this->settings['login'] !== null && $this->settings['password'] !== null) {
         if (!method_exists($this->_Memcached, 'setSaslAuthData')) {
             throw new CacheException(__d('cake_dev', 'Memcached extension is not build with SASL support'));
         }
         $this->_Memcached->setSaslAuthData($this->settings['login'], $this->settings['password']);
     }
     return true;
 }