Skip to content

Redis Adapter

Muhammet Şafak edited this page May 29, 2026 · 1 revision

Redis Adapter

InitPHP\Sessions\Adapters\RedisAdapter stores sessions in Redis with a per-key TTL, so expiry is handled by Redis itself.

Requirements: ext-redis.

Options

Option Type Default Notes
redis \Redis Reuse an existing client. When set, the connection options below are ignored.
host string 127.0.0.1
port int 6379
timeout float 0 Connect timeout in seconds.
password string|null null AUTH password.
database int 0 Database index to SELECT.
ttl int 86400 Key expiry in seconds. 0 disables expiry.
prefix string '' Key prefix.

Usage — connect inline

use InitPHP\Sessions\Session;
use InitPHP\Sessions\Adapters\RedisAdapter;

$adapter = new RedisAdapter([
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'timeout'  => 0,
    'password' => null,
    'database' => 0,
    'ttl'      => 86400,
    'prefix'   => 'sess_',
]);

Session::createImmutable($adapter)->start();

Usage — reuse an existing client

If your app already has a configured \Redis, hand it over and skip the connection options:

$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('secret');

$adapter = new RedisAdapter([
    'redis'  => $redis,
    'ttl'    => 86400,
    'prefix' => 'sess_',
]);

Session::createImmutable($adapter)->start();

Expiry

Each write() stores the payload with SETEX using ttl (when ttl > 0), so keys expire automatically — no garbage collection needed. Set ttl => 0 to store keys without expiry (you then manage cleanup yourself).

The session's effective lifetime is the larger of this ttl and whatever your session cookie lifetime is; align them to avoid surprises.

Errors

use InitPHP\Sessions\Exceptions\SessionAdapterException;

try {
    $adapter = new RedisAdapter(['host' => '10.0.0.9', 'port' => 6379]);
} catch (SessionAdapterException $e) {
    // Redis is unreachable — fall back or fail fast.
}

Migrating from initphp/redis-session-handler

This adapter supersedes the standalone initphp/redis-session-handler package. See the Migration Guide.

See also

Clone this wiki locally