InitPHP\Cache\Handler\Redis stores items in Redis through the
phpredis extension (ext-redis).
| Option | Type | Default | Description |
|---|---|---|---|
prefix |
string |
cache_ |
Prepended to keys. |
host |
string |
127.0.0.1 |
Server host. |
port |
int |
6379 |
Server port. |
timeout |
int|float |
0 |
Connection timeout in seconds (0 = unlimited). |
password |
string|null |
null |
AUTH password; skipped when null. |
database |
int|null |
0 |
Database index to SELECT. |
use InitPHP\Cache\Cache;
use InitPHP\Cache\Handler\Redis;
$cache = Cache::create(Redis::class, [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 1,
'password' => null,
]);
$cache->set('session:abc', $payload, 1800);
$cache->get('session:abc');- Each value is stored as a small serialised envelope, so
null,false, arrays and objects round-trip exactly. - TTLs are delegated to Redis (
SETEX); items with no TTL never expire. clear()runsFLUSHDB, clearing the whole selected database regardless ofprefix. Point the handler at a dedicateddatabaseif you need isolation.
A failed connection, authentication or database selection throws a
CacheException that wraps the underlying RedisException.
This handler requires the C extension. If you cannot install ext-redis, use a
PSR-16 adapter over a pure-PHP client (such as Predis) instead, or fall back to
the PDO or File handler.