Exemplo n.º 1
0
 /**
  * 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
  */
 public function init($settings = [])
 {
     if (!class_exists('Memcached')) {
         return false;
     }
     if (!isset($settings['prefix'])) {
         $settings['prefix'] = Inflector::slug(APP_DIR) . '_';
     }
     $settings += ['engine' => 'Memcached', 'servers' => ['127.0.0.1'], 'compress' => false, 'persistent' => true];
     parent::init($settings);
     $this->_keys .= $this->settings['prefix'];
     if (!is_array($this->settings['servers'])) {
         $this->settings['servers'] = [$this->settings['servers']];
     }
     if (!isset($this->_Memcached)) {
         $return = false;
         $this->_Memcached = new Memcached($this->settings['persistent'] ? 'mc' : null);
         $this->_setOptions();
         if (!count($this->_Memcached->getServerList())) {
             $servers = [];
             foreach ($this->settings['servers'] as $server) {
                 $servers[] = $this->_parseServerString($server);
             }
             if ($this->_Memcached->addServers($servers)) {
                 $return = true;
             }
         }
         if (!$this->_Memcached->get($this->_keys)) {
             $this->_Memcached->set($this->_keys, '');
         }
         return $return;
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }
Exemplo n.º 3
0
 /**
  * @param array $serverInfo
  */
 protected function _addServer($serverInfo)
 {
     if (!isset($serverInfo['host']) || !isset($serverInfo['port'])) {
         throw new RuntimeException('illigal server info, host and port is required');
     }
     $serverInfo = array_merge(self::$_baseServerInfo, $serverInfo);
     $servers = $this->_memcache->getServerList();
     if (is_array($servers)) {
         foreach ($servers as $server) {
             if ($server['host'] == $serverInfo['host'] && $server['port'] == $serverInfo['port']) {
                 return true;
             }
         }
     }
     $this->_memcache->addServer($serverInfo['host'], $serverInfo['port']);
 }
Exemplo n.º 4
0
 /**
  * Returns size used by cache
  */
 public function get_stats_size($timeout_time)
 {
     $size = array('bytes' => 0, 'items' => 0, 'timeout_occurred' => false);
     $key_prefix = $this->get_item_key('');
     $error_occurred = false;
     $server_list = $this->_memcache->getServerList();
     $n = 0;
     foreach ($server_list as $server) {
         $loader = new Cache_Memcached_Stats($server['host'], $server['port']);
         $slabs = $loader->slabs();
         if (!is_array($slabs)) {
             $error_occurred = true;
             continue;
         }
         foreach ($slabs as $slab_id) {
             $cdump = $loader->cachedump($slab_id);
             if (!is_array($cdump)) {
                 continue;
             }
             foreach ($cdump as $line) {
                 $key_data = explode(' ', $line);
                 if (!is_array($key_data) || count($key_data) < 3) {
                     continue;
                 }
                 $n++;
                 if ($n % 10 == 0) {
                     $size['timeout_occurred'] = time() > $timeout_time;
                     if ($size['timeout_occurred']) {
                         return $size;
                     }
                 }
                 $key = $key_data[1];
                 $bytes = substr($key_data[2], 1);
                 if (substr($key, 0, strlen($key_prefix)) == $key_prefix) {
                     $size['bytes'] += $bytes;
                     $size['items']++;
                 }
             }
         }
     }
     if ($error_occurred && $size['items'] <= 0) {
         $size['bytes'] = null;
         $size['items'] = null;
     }
     return $size;
 }
Exemplo n.º 5
0
 /**
  * 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;
 }
Exemplo n.º 6
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;
 }