Class yii\redis\predis\PredisConnection
| Inheritance | yii\redis\predis\PredisConnection » yii\base\Component |
|---|---|
| Implements | yii\redis\ConnectionInterface |
| Source Code | https://github.com/yiisoft/yii2-redis/blob/master/src/predis/PredisConnection.php |
Class PredisConnection
See also https://github.com/predis/predis `php
// redis-sentinel
'redis' = [
'class' => PredisConnection::class,
'parameters' => [
'tcp://127.0.0.1:26379?timeout=0.100',
'tcp://127.0.0.1:26380?timeout=0.100',
'tcp://127.0.0.1:26381?timeout=0.100',
],
'options' => [
'replication' => 'sentinel',
'service' => 'mymaster',
'parameters' => [
'password' => 'password',
'database' => 10,
// @see \Predis\Connection\StreamConnection
'persistent' => true, // performs the connection asynchronously
'async_connect' => true, //the connection asynchronously
'read_write_timeout' => 0.1, // timeout of read / write operations
],
],
];
`.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $isActive | boolean | Whether the DB connection is established. | yii\redis\predis\PredisConnection |
| $luaScriptBuilder | yii\redis\LuaScriptBuilder | yii\redis\predis\PredisConnection | |
| $options | mixed | Options to configure some behaviours of the client. | yii\redis\predis\PredisConnection |
| $parameters | mixed | Connection parameters for one or more servers. | yii\redis\predis\PredisConnection |
| $redisCommands | array | List of available redis commands. | yii\redis\predis\PredisConnection |
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $client | \Predis\Client|null | Redis connection | yii\redis\predis\PredisConnection |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __call() | Allows issuing all supported commands via magic methods. | yii\redis\predis\PredisConnection |
| close() | Closes the currently active DB connection. | yii\redis\predis\PredisConnection |
| executeCommand() | yii\redis\predis\PredisConnection | |
| getClient() | Get predis Client | yii\redis\predis\PredisConnection |
| getIsActive() | Returns a value indicating whether the DB connection is established. | yii\redis\predis\PredisConnection |
| getLuaScriptBuilder() | yii\redis\predis\PredisConnection | |
| open() | Establishes a DB connection. | yii\redis\predis\PredisConnection |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| initConnection() | Initializes the DB connection. | yii\redis\predis\PredisConnection |
Events
| Event | Type | Description | Defined By |
|---|---|---|---|
| EVENT_AFTER_OPEN | yii\redis\predis\Event | An event that is triggered after a DB connection is established | yii\redis\predis\PredisConnection |
Property Details
Options to configure some behaviours of the client.
Connection parameters for one or more servers.
List of available redis commands.
See also https://redis.io/commands.
'APPEND',
'AUTH',
'BGREWRITEAOF',
'BGSAVE',
'BITCOUNT',
'BITFIELD',
'BITOP',
'BITPOS',
'BLPOP',
'BRPOP',
'BRPOPLPUSH',
'CLIENT KILL',
'CLIENT LIST',
'CLIENT GETNAME',
'CLIENT PAUSE',
'CLIENT REPLY',
'CLIENT SETNAME',
'CLUSTER ADDSLOTS',
'CLUSTER COUNTKEYSINSLOT',
'CLUSTER DELSLOTS',
'CLUSTER FAILOVER',
'CLUSTER FORGET',
'CLUSTER GETKEYSINSLOT',
'CLUSTER INFO',
'CLUSTER KEYSLOT',
'CLUSTER MEET',
'CLUSTER NODES',
'CLUSTER REPLICATE',
'CLUSTER RESET',
'CLUSTER SAVECONFIG',
'CLUSTER SETSLOT',
'CLUSTER SLAVES',
'CLUSTER SLOTS',
'COMMAND',
'COMMAND COUNT',
'COMMAND GETKEYS',
'COMMAND INFO',
'CONFIG GET',
'CONFIG REWRITE',
'CONFIG SET',
'CONFIG RESETSTAT',
'DBSIZE',
'DEBUG OBJECT',
'DEBUG SEGFAULT',
'DECR',
'DECRBY',
'DEL',
'DISCARD',
'DUMP',
'ECHO',
'EVAL',
'EVALSHA',
'EXEC',
'EXISTS',
'EXPIRE',
'EXPIREAT',
'FLUSHALL',
'FLUSHDB',
'GEOADD',
'GEOHASH',
'GEOPOS',
'GEODIST',
'GEORADIUS',
'GEORADIUSBYMEMBER',
'GET',
'GETBIT',
'GETRANGE',
'GETSET',
'HDEL',
'HEXISTS',
'HGET',
'HGETALL',
'HINCRBY',
'HINCRBYFLOAT',
'HKEYS',
'HLEN',
'HMGET',
'HMSET',
'HSET',
'HSETNX',
'HSTRLEN',
'HVALS',
'INCR',
'INCRBY',
'INCRBYFLOAT',
'INFO',
'KEYS',
'LASTSAVE',
'LINDEX',
'LINSERT',
'LLEN',
'LPOP',
'LPUSH',
'LPUSHX',
'LRANGE',
'LREM',
'LSET',
'LTRIM',
'MGET',
'MIGRATE',
'MONITOR',
'MOVE',
'MSET',
'MSETNX',
'MULTI',
'OBJECT',
'PERSIST',
'PEXPIRE',
'PEXPIREAT',
'PFADD',
'PFCOUNT',
'PFMERGE',
'PING',
'PSETEX',
'PSUBSCRIBE',
'PUBSUB',
'PTTL',
'PUBLISH',
'PUNSUBSCRIBE',
'QUIT',
'RANDOMKEY',
'READONLY',
'READWRITE',
'RENAME',
'RENAMENX',
'RESTORE',
'ROLE',
'RPOP',
'RPOPLPUSH',
'RPUSH',
'RPUSHX',
'SADD',
'SAVE',
'SCARD',
'SCRIPT DEBUG',
'SCRIPT EXISTS',
'SCRIPT FLUSH',
'SCRIPT KILL',
'SCRIPT LOAD',
'SDIFF',
'SDIFFSTORE',
'SELECT',
'SET',
'SETBIT',
'SETEX',
'SETNX',
'SETRANGE',
'SHUTDOWN',
'SINTER',
'SINTERSTORE',
'SISMEMBER',
'SLAVEOF',
'SLOWLOG',
'SMEMBERS',
'SMOVE',
'SORT',
'SPOP',
'SRANDMEMBER',
'SREM',
'STRLEN',
'SUBSCRIBE',
'SUNION',
'SUNIONSTORE',
'SWAPDB',
'SYNC',
'TIME',
'TOUCH',
'TTL',
'TYPE',
'UNSUBSCRIBE',
'UNLINK',
'UNWATCH',
'WAIT',
'WATCH',
'XACK',
'XADD',
'XCLAIM',
'XDEL',
'XGROUP',
'XINFO',
'XLEN',
'XPENDING',
'XRANGE',
'XREAD',
'XREADGROUP',
'XREVRANGE',
'XTRIM',
'ZADD',
'ZCARD',
'ZCOUNT',
'ZINCRBY',
'ZINTERSTORE',
'ZLEXCOUNT',
'ZRANGE',
'ZRANGEBYLEX',
'ZREVRANGEBYLEX',
'ZRANGEBYSCORE',
'ZRANK',
'ZREM',
'ZREMRANGEBYLEX',
'ZREMRANGEBYRANK',
'ZREMRANGEBYSCORE',
'ZREVRANGE',
'ZREVRANGEBYSCORE',
'ZREVRANK',
'ZSCORE',
'ZUNIONSTORE',
'SCAN',
'SSCAN',
'HSCAN',
'ZSCAN',
]
Method Details
Allows issuing all supported commands via magic methods.
$redis->hmset('test_collection', 'key1', 'val1', 'key2', 'val2')
| public mixed __call ( string $name, array $params ) | ||
| $name | string |
Name of the missing method to execute |
| $params | array |
Method call arguments |
| throws | \yii\base\InvalidConfigException | |
|---|---|---|
public function __call($name, $params)
{
$redisCommand = strtoupper(Inflector::camel2words($name, false));
if (in_array($redisCommand, $this->redisCommands, true)) {
return $this->executeCommand($redisCommand, $params);
}
return parent::__call($name, $params);
}
Closes the currently active DB connection.
It does nothing if the connection is already closed.
| public void close ( ) |
public function close(): void
{
if ($this->client === null) {
return;
}
$this->client->disconnect();
}
| public mixed|\Predis\Response\ErrorInterface|\Predis\Response\ResponseInterface executeCommand ( mixed $name, mixed $params = [] ) | ||
| $name | mixed | |
| $params | mixed | |
| throws | \yii\base\InvalidConfigException | |
|---|---|---|
public function executeCommand($name, $params = [])
{
$this->open();
Yii::debug("Executing Redis Command: $name " . implode(' ', $params), __METHOD__);
$command = $this->client->createCommand($name, $params);
$response = $this->client->executeCommand(new CommandDecorator($command));
if ($response instanceof Status) {
// ResponseStatus yii expect as bool
return (string)$response === 'OK' || (string)$response === 'PONG';
}
return $response;
}
Get predis Client
| public \Predis\Client|null getClient ( ) | ||
| throws | \yii\base\InvalidConfigException | |
|---|---|---|
public function getClient(): ?Client
{
$this->open();
return $this->client;
}
Returns a value indicating whether the DB connection is established.
| public boolean getIsActive ( ) | ||
| return | boolean |
Whether the DB connection is established |
|---|---|---|
public function getIsActive(): bool
{
if ($this->client === null) {
return false;
}
return $this->client->isConnected();
}
| public yii\redis\LuaScriptBuilder getLuaScriptBuilder ( ) |
public function getLuaScriptBuilder(): LuaScriptBuilder
{
return new LuaScriptBuilder();
}
Initializes the DB connection.
This method is invoked right after the DB connection is established. The default implementation triggers an EVENT_AFTER_OPEN event.
| protected void initConnection ( ) |
protected function initConnection(): void
{
$this->trigger(self::EVENT_AFTER_OPEN);
}
Establishes a DB connection.
| public void open ( ) | ||
| throws | \yii\base\InvalidConfigException | |
|---|---|---|
public function open(): void
{
if (null !== $this->client) {
return;
}
if (empty($this->parameters)) {
throw new InvalidConfigException('Connection::parameters cannot be empty');
}
Yii::debug('Opening redis DB connection', __METHOD__);
$this->client = new Client($this->parameters, $this->options);
$this->initConnection();
}