PHP code example of phpdot / redis

1. Go to this page and download the library: Download phpdot/redis library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpdot / redis example snippets


use PHPdot\Redis\Config\RedisConfig;
use PHPdot\Redis\RedisConnection;

$connection = new RedisConnection(new RedisConfig(
    host: '127.0.0.1',
    port: 6379,
    password: 'secret',
    database: 0,
));
$connection->connect();

// Reach the underlying \Redis for commands
$connection->getClient()->set('hello', 'world');
$connection->getClient()->get('hello'); // 'world'

$connection->close();

new RedisConfig(
    host: '127.0.0.1',     // TCP host (ignored when path is set)
    port: 6379,            // TCP port (ignored when path is set)
    path: '',              // Unix socket path; supersedes host/port when set
    password: '',          // AUTH password (Redis 5-). Empty skips AUTH.
    username: '',          // ACL username (Redis 6+). Empty = password-only AUTH.
    database: 0,           // SELECT index after connect
    timeout: 0.0,          // connect timeout, seconds (0 = unlimited)
    retryInterval: 0,      // ext-redis reconnect delay, ms
    readTimeout: 0.0,      // read timeout, seconds
    tls: false,            // connect over TLS (prefixes host with tls://)
    ssl: [],               // stream SSL context options (CA, cert, verify…)
    maxRetries: 3,         // connect-attempt retries, exponential backoff
    persistent: false,     // use pconnect (off by default — pool owns lifecycle)
    context: [],           // catch-all passed as ext-redis' $context arg
);

$connection->connect();          // establish + AUTH + SELECT + PING
$connection->isConnected();      // local flag, no server round-trip
$connection->ping();             // round-trip PING → bool
$connection->ensureConnected();  // throws ConnectionException if down
$connection->reconnect();        // close() then connect()
$connection->close();            // idempotent
$connection->getConfig();        // RedisConfig
$connection->getClient();        // \Redis (throws if not connected)

use PHPdot\Pool\Pool;
use PHPdot\Pool\PoolConfig;
use PHPdot\Redis\Config\RedisConfig;
use PHPdot\Redis\RedisConnector;

$pool = new Pool(
    new RedisConnector(new RedisConfig()),
    PoolConfig::default(),
);

// Borrow a connection per coroutine
$connection = $pool->borrow();
try {
    $connection->getClient()->set('key', 'value');
} finally {
    $pool->release($connection);
}

use PHPdot\Redis\Exception\AuthenticationException;
use PHPdot\Redis\Exception\ConnectionException;

try {
    $connection->connect();
} catch (AuthenticationException $e) {
    // bad credentials — do not retry
} catch (ConnectionException $e) {
    // unreachable after retries; $e->getHost() tells you which endpoint
}