Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

RedisClient
in package

FinalYes

Thin adapter over phpredis (preferred when ext-redis is loaded) or predis (pure-PHP fallback). The ONE place the client lib is referenced by name in ZealPHP — every other class talks to this adapter.

Construct with a redis URL; pass ['prefer' => 'phpredis'|'predis'|'auto'] to force the driver. 'auto' (default) picks phpredis when the ext is loaded, predis when it isn't, throws when neither is available.

Table of Contents

Properties

$driver  : RedisDriver
Active driver — either PhpredisDriver or PredisDriver, selected at construction time.

Methods

__construct()  : mixed
Connect to $url using the best available driver. Pass $opts['prefer'] as 'phpredis', 'predis', or 'auto' (default) to control driver selection explicitly.
close()  : void
Close the underlying driver connection and release resources.
decrby()  : int
Atomically decrement a plain string counter by $by via DECRBY. Returns the new value.
del()  : int
Delete one or more keys. Returns the count of keys that were removed.
driverName()  : string
Return the active driver name — 'phpredis' or 'predis'. Useful for diagnostics and feature guards.
evalScript()  : mixed
exists()  : bool
Return true when $key exists in Redis.
expire()  : bool
Set a TTL of $ttlSeconds on $key via EXPIRE. Returns true on success.
get()  : string|null
Get a plain string key. Returns null on miss.
hdel()  : int
Delete one or more hash fields via HDEL. Returns the number of fields removed.
hgetall()  : array<string, string>
hincrby()  : int
Atomically increment hash field by $by (int) via HINCRBY. Returns the new value.
hincrbyfloat()  : float
Atomically increment hash field by $by (float) via HINCRBYFLOAT. Returns the new value.
hmget()  : array<int, string|null>
hset()  : int
incrby()  : int
Atomically increment a plain string counter by $by via INCRBY. Returns the new value.
mhgetall()  : array<int, array<string, string>>
mhsetWithMembership()  : void
ping()  : bool
Ping the Redis server. Returns true when the server is reachable.
pipeline()  : array<int, mixed>
publish()  : int
Publish $payload to a pub/sub $channel. Returns the number of subscribers that received it.
sadd()  : int
scanCursor()  : array{0: string, 1: list}
scanKeys()  : Generator<int, string>
scard()  : int
set()  : bool
Set a plain string key, optionally with a TTL in seconds. Returns true on success.
srem()  : int
sscan()  : Generator<int, string>
sscanCursor()  : array{0: string, 1: list}
subscribe()  : void
unlink()  : int
Asynchronously delete keys via UNLINK (non-blocking, unlike DEL). Returns the count of keys removed.
xack()  : int
Acknowledge stream entries via XACK, removing them from the consumer group's pending list. Returns the count acknowledged.
xadd()  : string
xautoclaim()  : array{0: string, 1: list}>}
xgroupCreate()  : bool
Create a Redis Stream consumer group. Returns true on success, false when the group already exists (BUSYGROUP — idempotent). Creates the stream when $mkStream is true and the stream doesn't yet exist.
xreadGroup()  : array<string, array<int, array{id: string, payload: array}>>
pickDriver()  : RedisDriver
Select and instantiate the appropriate RedisDriver based on $prefer and the extensions available at runtime. Throws StoreException when $prefer is not one of 'auto', 'phpredis', or 'predis', or when 'auto' is chosen but neither ext-redis nor predis is available.

Properties

$driver

Active driver — either PhpredisDriver or PredisDriver, selected at construction time.

private RedisDriver $driver

Methods

__construct()

Connect to $url using the best available driver. Pass $opts['prefer'] as 'phpredis', 'predis', or 'auto' (default) to control driver selection explicitly.

public __construct(string $url[, array{prefer?: "auto"|"phpredis"|"predis"} $opts = [] ]) : mixed
Parameters
$url : string
$opts : array{prefer?: "auto"|"phpredis"|"predis"} = []

close()

Close the underlying driver connection and release resources.

public close() : void

decrby()

Atomically decrement a plain string counter by $by via DECRBY. Returns the new value.

public decrby(string $key, int $by) : int
Parameters
$key : string
$by : int
Return values
int

del()

Delete one or more keys. Returns the count of keys that were removed.

public del(string ...$keys) : int
Parameters
$keys : string
Return values
int

driverName()

Return the active driver name — 'phpredis' or 'predis'. Useful for diagnostics and feature guards.

public driverName() : string
Return values
string

evalScript()

public evalScript(string $script, array<int, string> $keys, array<int, string> $args) : mixed
Parameters
$script : string
$keys : array<int, string>
$args : array<int, string>

exists()

Return true when $key exists in Redis.

public exists(string $key) : bool
Parameters
$key : string
Return values
bool

expire()

Set a TTL of $ttlSeconds on $key via EXPIRE. Returns true on success.

public expire(string $key, int $ttlSeconds) : bool
Parameters
$key : string
$ttlSeconds : int
Return values
bool

get()

Get a plain string key. Returns null on miss.

public get(string $key) : string|null
Parameters
$key : string
Return values
string|null

hdel()

Delete one or more hash fields via HDEL. Returns the number of fields removed.

public hdel(string $key, string ...$fields) : int
Parameters
$key : string
$fields : string
Return values
int

hgetall()

public hgetall(string $key) : array<string, string>
Parameters
$key : string
Return values
array<string, string>

hincrby()

Atomically increment hash field by $by (int) via HINCRBY. Returns the new value.

public hincrby(string $key, string $field, int $by) : int
Parameters
$key : string
$field : string
$by : int
Return values
int

hincrbyfloat()

Atomically increment hash field by $by (float) via HINCRBYFLOAT. Returns the new value.

public hincrbyfloat(string $key, string $field, float $by) : float
Parameters
$key : string
$field : string
$by : float
Return values
float

hmget()

public hmget(string $key, array<int, string> $fields) : array<int, string|null>
Parameters
$key : string
$fields : array<int, string>
Return values
array<int, string|null>

hset()

public hset(string $key, array<string, string> $fields) : int
Parameters
$key : string
$fields : array<string, string>
Return values
int

incrby()

Atomically increment a plain string counter by $by via INCRBY. Returns the new value.

public incrby(string $key, int $by) : int
Parameters
$key : string
$by : int
Return values
int

mhgetall()

public mhgetall(array<int, string> $keys) : array<int, array<string, string>>
Parameters
$keys : array<int, string>
Return values
array<int, array<string, string>>

mhsetWithMembership()

public mhsetWithMembership(array<int, array{rk: string, fields: array, sk?: string}> $writes[, string|null $setKey = null ][, int|null $ttl = null ]) : void
Parameters
$writes : array<int, array{rk: string, fields: array, sk?: string}>
$setKey : string|null = null
$ttl : int|null = null

ping()

Ping the Redis server. Returns true when the server is reachable.

public ping() : bool
Return values
bool

pipeline()

public pipeline(callable $batch) : array<int, mixed>
Parameters
$batch : callable
Return values
array<int, mixed>

publish()

Publish $payload to a pub/sub $channel. Returns the number of subscribers that received it.

public publish(string $channel, string $payload) : int
Parameters
$channel : string
$payload : string
Return values
int

sadd()

public sadd(string $key, array<int, string> $members) : int
Parameters
$key : string
$members : array<int, string>
Return values
int

scanCursor()

public scanCursor(string $match, string $cursor, int $count) : array{0: string, 1: list}
Parameters
$match : string
$cursor : string
$count : int
Return values
array{0: string, 1: list}

scanKeys()

public scanKeys(string $match[, int $batch = 200 ]) : Generator<int, string>
Parameters
$match : string
$batch : int = 200
Return values
Generator<int, string>

scard()

public scard(string $key) : int
Parameters
$key : string
Return values
int

set()

Set a plain string key, optionally with a TTL in seconds. Returns true on success.

public set(string $key, string $value[, int|null $ttlSeconds = null ]) : bool
Parameters
$key : string
$value : string
$ttlSeconds : int|null = null
Return values
bool

srem()

public srem(string $key, array<int, string> $members) : int
Parameters
$key : string
$members : array<int, string>
Return values
int

sscan()

public sscan(string $key[, int $batch = 100 ]) : Generator<int, string>
Parameters
$key : string
$batch : int = 100
Return values
Generator<int, string>

sscanCursor()

public sscanCursor(string $key, string $cursor, int $count) : array{0: string, 1: list}
Parameters
$key : string
$cursor : string
$count : int
Return values
array{0: string, 1: list}

subscribe()

public subscribe(array<int, string> $exactChannels, array<int, string> $patternChannels, callable(string $payload, string $channel, Array $pattern): void $consumer) : void
Parameters
$exactChannels : array<int, string>
$patternChannels : array<int, string>
$consumer : callable(string $payload, string $channel, Array $pattern): void

Asynchronously delete keys via UNLINK (non-blocking, unlike DEL). Returns the count of keys removed.

public unlink(string ...$keys) : int
Parameters
$keys : string
Return values
int

xack()

Acknowledge stream entries via XACK, removing them from the consumer group's pending list. Returns the count acknowledged.

public xack(string $stream, string $group, string ...$ids) : int
Parameters
$stream : string
$group : string
$ids : string
Return values
int

xadd()

public xadd(string $stream, array<string, string> $fields[, int|null $maxLen = null ]) : string
Parameters
$stream : string
$fields : array<string, string>
$maxLen : int|null = null
Return values
string

xautoclaim()

public xautoclaim(string $stream, string $group, string $consumer, int $minIdleMs[, string $start = '0-0' ][, int $count = 16 ]) : array{0: string, 1: list}>}
Parameters
$stream : string
$group : string
$consumer : string
$minIdleMs : int
$start : string = '0-0'
$count : int = 16
Return values
array{0: string, 1: list}>}

xgroupCreate()

Create a Redis Stream consumer group. Returns true on success, false when the group already exists (BUSYGROUP — idempotent). Creates the stream when $mkStream is true and the stream doesn't yet exist.

public xgroupCreate(string $stream, string $group[, string $id = '$' ][, bool $mkStream = true ]) : bool
Parameters
$stream : string
$group : string
$id : string = '$'
$mkStream : bool = true
Return values
bool

xreadGroup()

public xreadGroup(string $group, string $consumer, array<int, string> $streams, int $count, int $blockMs) : array<string, array<int, array{id: string, payload: array}>>
Parameters
$group : string
$consumer : string
$streams : array<int, string>
$count : int
$blockMs : int
Return values
array<string, array<int, array{id: string, payload: array}>>

pickDriver()

Select and instantiate the appropriate RedisDriver based on $prefer and the extensions available at runtime. Throws StoreException when $prefer is not one of 'auto', 'phpredis', or 'predis', or when 'auto' is chosen but neither ext-redis nor predis is available.

private static pickDriver(string $url, string $prefer) : RedisDriver
Parameters
$url : string
$prefer : string
Return values
RedisDriver
On this page