Пример #1
0
 /**
  * The options array should contain an array of servers,
  *
  * The "server" option expects an array of servers, with each server being represented by an associative array. Each
  * redis config must have either a "socket" or a "server" value, and optional "port" and "ttl" values (with the ttl
  * representing server timeout, not cache expiration).
  *
  * The "database" option lets developers specific which specific database to use.
  *
  * The "password" option is used for clusters which required authentication.
  *
  * @param  array             $options
  * @throws \RuntimeException
  */
 public function setOptions(array $options = array())
 {
     if (!self::isAvailable()) {
         throw new \RuntimeException('Unable to load Redis driver without PhpRedis extension.');
     }
     // Normalize Server Options
     if (isset($options['servers'])) {
         $unprocessedServers = is_array($options['servers']) ? $options['servers'] : array($options['servers']);
         unset($options['servers']);
         $servers = array();
         foreach ($unprocessedServers as $server) {
             $ttl = '.1';
             if (isset($server['ttl'])) {
                 $ttl = $server['ttl'];
             } elseif (isset($server[2])) {
                 $ttl = $server[2];
             }
             if (isset($server['socket'])) {
                 $servers[] = array('socket' => $server['socket'], 'ttl' => $ttl);
             } else {
                 $host = '127.0.0.1';
                 if (isset($server['server'])) {
                     $host = $server['server'];
                 } elseif (isset($server[0])) {
                     $host = $server[0];
                 }
                 $port = '6379';
                 if (isset($server['port'])) {
                     $port = $server['port'];
                 } elseif (isset($server[1])) {
                     $port = $server[1];
                 }
                 $servers[] = array('server' => $host, 'port' => $port, 'ttl' => $ttl);
             }
         }
     } else {
         $servers = array(array('server' => '127.0.0.1', 'port' => '6379', 'ttl' => 0.1));
     }
     // Merge in default values.
     $options = array_merge($this->defaultOptions, $options);
     // this will have to be revisited to support multiple servers, using
     // the RedisArray object. That object acts as a proxy object, meaning
     // most of the class will be the same even after the changes.
     if (count($servers) == 1) {
         $server = $servers[0];
         $redis = new \Redis();
         if (isset($server['socket']) && $server['socket']) {
             $redis->connect($server['socket']);
         } else {
             $port = isset($server['port']) ? $server['port'] : 6379;
             $ttl = isset($server['ttl']) ? $server['ttl'] : 0.1;
             $redis->connect($server['server'], $port, $ttl);
         }
         // auth - just password
         if (isset($options['password'])) {
             $redis->auth($options['password']);
         }
         $this->redis = $redis;
     } else {
         $redisArrayOptions = array();
         foreach ($this->redisArrayOptionNames as $optionName) {
             if (array_key_exists($optionName, $options)) {
                 $redisArrayOptions[$optionName] = $options[$optionName];
             }
         }
         $serverArray = array();
         foreach ($servers as $server) {
             $serverString = $server['server'];
             if (isset($server['port'])) {
                 $serverString .= ':' . $server['port'];
             }
             $serverArray[] = $serverString;
         }
         $redis = new \RedisArray($serverArray, $redisArrayOptions);
     }
     // select database
     if (isset($options['database'])) {
         $redis->select($options['database']);
     }
     $this->redis = $redis;
 }