示例#1
0
 /**
  * 架构函数
  * @param array $options 缓存参数
  * @access public
  */
 public function __construct($options = array())
 {
     ini_set('default_socket_timeout', -1);
     //不超时
     if (empty($options)) {
         $options = array('host' => C('KVSTORE_HOST'), 'port' => C('KVSTORE_PORT'), 'timeout' => false, 'persistent' => false, 'prefix' => C('DATA_CACHE_PREFIX'), 'user' => C('DATA_CACHE_USER'), 'pwd' => C('DATA_CACHE_PW'), 'expire' => 12000);
     } else {
         $options['host'] = isset($options['host']) ?: C('KVSTORE_HOST');
         $options['port'] = isset($options['port']) ?: C('KVSTORE_PORT');
         $options['timeout'] = isset($options['timeout']) ?: FALSE;
         $options['persistent'] = isset($options['persistent']) ?: FALSE;
         $options['prefix'] = isset($options['prefix']) ?: C('DATA_CACHE_PREFIX');
         $options['user'] = isset($options['user']) ?: C('DATA_CACHE_USER');
         $options['pwd'] = isset($options['pwd']) ?: C('DATA_CACHE_PW');
         $options['expire'] = isset($options['expire']) ?: 12000;
     }
     $this->options = $options;
     $redis = new \Redis();
     for ($i = 0; $i < 2; $i++) {
         if ($redis->pconnect($options['host'], $options['port']) == false) {
             try {
                 $error = $redis->getLastError();
             } catch (\RedisException $ex) {
                 $error = $ex->getMessage();
                 DILog($error);
                 if ($error != 'Redis server went away') {
                     break;
                     //跳出重试循环
                 } else {
                     sleep(0.5);
                     //暂停0.5s以后重试连接
                     DILog('Kvstore: Try Reconnected');
                 }
             }
             //	    E($redis->getLastError());
         }
     }
     $redis->setOption(\Redis::OPT_PREFIX, $options['prefix']);
     /* user:password 拼接成AUTH的密码 */
     if ($redis->auth($options['user'] . ':' . $options['pwd']) == false) {
         $error = $redis->getLastError();
         DILog($error);
         //	    E($redis->getLastError());
     }
     $this->handler = $redis;
 }
示例#2
0
 /**
  * 架构函数
  * @param array $options 缓存参数
  * @access public
  */
 public function __construct($options = array())
 {
     ini_set('default_socket_timeout', -1);
     //不超时
     if (empty($options)) {
         $options = array('host' => C('KVSTORE_HOST'), 'port' => C('KVSTORE_PORT'), 'timeout' => false, 'persistent' => false, 'prefix' => C('DATA_CACHE_PREFIX'), 'user' => C('DATA_CACHE_USER'), 'pwd' => C('DATA_CACHE_PW'), 'expire' => 12000);
     }
     $this->options = $options;
     $redis = new \Redis();
     if ($redis->pconnect($options['host'], $options['port']) == false) {
         E($redis->getLastError());
     }
     $redis->setOption(\Redis::OPT_PREFIX, $options['prefix']);
     /* user:password 拼接成AUTH的密码 */
     if ($redis->auth($options['host'] . ':' . $options['pwd']) == false) {
         E($redis->getLastError());
     }
     $this->handler = $redis;
 }