executeCommand() публичный Метод

For a list of available commands and their parameters see http://redis.io/commands.
public executeCommand ( string $name, array $params = [] ) : array | boolean | null | string
$name string the name of the command
$params array list of parameters for the command
Результат array | boolean | null | string Dependent on the executed command this method will return different data types: - `true` for commands that return "status reply" with the message `'OK'` or `'PONG'`. - `string` for commands that return "status reply" that does not have the message `OK` (since version 2.0.1). - `string` for commands that return "integer reply" as the value is in the range of a signed 64 bit integer. - `string` or `null` for commands that return "bulk reply". - `array` for commands that return "Multi-bulk replies". See [redis protocol description](http://redis.io/topics/protocol) for details on the mentioned reply types.
Пример #1
0
 /**
  * @return mixed
  * @throws InvalidConfigException
  */
 public function configGetDatabases()
 {
     try {
         if ($this->_connect instanceof PhpredisConnection) {
             return $this->_connect->executeCommand('CONFIG', ['GET', 'databases'])['databases'];
         } else {
             return $this->_connect->executeCommand('CONFIG', ['GET', 'databases'])[1];
         }
     } catch (\Exception $e) {
         throw new InvalidConfigException('Can`t connect to redis database, please check module and redis-connection settings!!!!');
     }
 }
Пример #2
0
 protected function tearDown()
 {
     $this->redis->executeCommand('FLUSHDB');
     parent::tearDown();
 }
Пример #3
0
 /**
  * Fetch by pk if possible as this is much faster
  * @param Connection $db the database connection used to execute the query.
  * If this parameter is not given, the `db` application component will be used.
  * @param string $type the type of the script to generate
  * @param string $columnName
  * @return array|bool|null|string
  * @throws \yii\base\InvalidParamException
  * @throws \yii\base\NotSupportedException
  */
 private function findByPk($db, $type, $columnName = null)
 {
     if (count($this->where) == 1) {
         $pks = (array) reset($this->where);
     } else {
         foreach ($this->where as $values) {
             if (is_array($values)) {
                 // TODO support composite IN for composite PK
                 throw new NotSupportedException('Find by composite PK is not supported by redis ActiveRecord.');
             }
         }
         $pks = [$this->where];
     }
     /* @var $modelClass ActiveRecord */
     $modelClass = $this->modelClass;
     if ($type == 'Count') {
         $start = 0;
         $limit = null;
     } else {
         $start = $this->offset === null ? 0 : $this->offset;
         $limit = $this->limit;
     }
     $i = 0;
     $data = [];
     foreach ($pks as $pk) {
         if (++$i > $start && ($limit === null || $i <= $start + $limit)) {
             $key = $modelClass::keyPrefix() . ':a:' . $modelClass::buildKey($pk);
             $result = $db->executeCommand('HGETALL', [$key]);
             if (!empty($result)) {
                 $data[] = $result;
                 if ($type === 'One' && $this->orderBy === null) {
                     break;
                 }
             }
         }
     }
     // TODO support orderBy
     switch ($type) {
         case 'All':
             return $data;
         case 'One':
             return reset($data);
         case 'Count':
             return count($data);
         case 'Column':
             $column = [];
             foreach ($data as $dataRow) {
                 $row = [];
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     $row[$dataRow[$i++]] = $dataRow[$i++];
                 }
                 $column[] = $row[$columnName];
             }
             return $column;
         case 'Sum':
             $sum = 0;
             foreach ($data as $dataRow) {
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     if ($dataRow[$i++] == $columnName) {
                         $sum += $dataRow[$i];
                         break;
                     }
                 }
             }
             return $sum;
         case 'Average':
             $sum = 0;
             $count = 0;
             foreach ($data as $dataRow) {
                 $count++;
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     if ($dataRow[$i++] == $columnName) {
                         $sum += $dataRow[$i];
                         break;
                     }
                 }
             }
             return $sum / $count;
         case 'Min':
             $min = null;
             foreach ($data as $dataRow) {
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     if ($dataRow[$i++] == $columnName && ($min == null || $dataRow[$i] < $min)) {
                         $min = $dataRow[$i];
                         break;
                     }
                 }
             }
             return $min;
         case 'Max':
             $max = null;
             foreach ($data as $dataRow) {
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     if ($dataRow[$i++] == $columnName && ($max == null || $dataRow[$i] > $max)) {
                         $max = $dataRow[$i];
                         break;
                     }
                 }
             }
             return $max;
     }
     throw new InvalidParamException('Unknown fetch type: ' . $type);
 }