/**
  * Constructor
  *
  * @param array[] $servers server array
  */
 public function __construct($servers)
 {
     if (!$servers || !is_array($servers) || count($servers) < 1) {
         throw new GitPHP_MessageException('No Memcache servers defined', true, 500);
     }
     if (class_exists('Memcached')) {
         $this->memcacheObj = new Memcached();
         $this->memcacheType = GitPHP_CacheResource_Memcache::Memcached;
         $this->memcacheObj->addServers($servers);
     } else {
         if (class_exists('Memcache')) {
             $this->memcacheObj = new Memcache();
             $this->memcacheType = GitPHP_CacheResource_Memcache::Memcache;
             foreach ($servers as $server) {
                 if (is_array($server)) {
                     $host = $server[0];
                     $port = 11211;
                     if (isset($server[1])) {
                         $port = $server[1];
                     }
                     $weight = 1;
                     if (isset($server[2])) {
                         $weight = $server[2];
                     }
                     $this->memcacheObj->addServer($host, $port, true, $weight);
                 }
             }
         } else {
             throw new GitPHP_MissingMemcacheException();
         }
     }
     $this->servers = $servers;
 }
Example #2
0
 /**
  * Constructor
  *
  * @access public
  *
  * @param array $config config array
  *
  * @result void
  * @throws Exception
  */
 public function __construct(array $config = [])
 {
     parent::__construct($config);
     if (empty($config['type']) || !$this->check()) {
         throw new Exception('Memcache(d) not installed or not select type');
     }
     switch (strtolower($config['type'])) {
         case 'memcached':
             $this->driver = new \Memcached();
             break;
         case 'memcache':
             $this->driver = new \Memcache();
             break;
         default:
             throw new Exception('Selected type not valid in the driver');
     }
     if (!empty($config['servers'])) {
         $this->driver->addServers($config['servers']);
     } elseif ($config['server']) {
         $conf = $config['server'];
         $server = ['hostname' => !empty($conf['hostname']) ? $conf['hostname'] : '127.0.0.1', 'port' => !empty($conf['port']) ? $conf['port'] : 11211, 'weight' => !empty($conf['weight']) ? $conf['weight'] : 1];
         if (get_class($this->driver) === 'Memcached') {
             $this->driver->addServer($server['hostname'], $server['port'], $server['weight']);
         } else {
             $this->driver->addServer($server['hostname'], $server['port'], true, $server['weight']);
         }
     } else {
         throw new Exception('Server(s) not configured');
     }
 }
 /**
  * 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;
 }
Example #4
0
 /**
  * Constructor
  *
  * @param array $options associative array of options
  * @throws App_Cache_Memcached_Exception
  * @return void
  */
 public function __construct(array $options = array())
 {
     if (!extension_loaded('memcached')) {
         throw new App_Cache_Memcached_Exception('The memcache extension must be loaded for using this backend !');
     }
     if ($db = Zend_Db_Table::getDefaultAdapter()) {
         $this->_options['db'] = $db;
     } else {
         throw new App_Cache_Memcached_Exception('Can`t found Zend_Db_Table::getDefaultAdapter()!');
     }
     while (list($name, $value) = each($options)) {
         $this->setOption($name, $value);
     }
     $this->_memcached = new Memcached();
     $this->_memcached->addServers($this->_options['servers']);
 }
 /**
  * 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;
 }
 /**
  * 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;
 }
 /**
  * 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;
 }