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

RedisCounterBackend
in package
implements CounterBackend

FinalYes

Redis/Valkey-backed CounterBackend.

Each counter is one Redis string at {prefix}:counter:{name}. INCRBY/DECRBY/GET/SET are native one-op atomic. compareAndSet runs a small Lua script for server-side CAS in one round-trip.

Table of Contents

Interfaces

CounterBackend
Behavioural contract for Counter backends.

Constants

LUA_CAS  : mixed = "if redis.call('GET', KEYS[1]) == ARGV[1] then...

Properties

$pool  : RedisConnectionPool
$prefix  : string

Methods

__construct()  : mixed
compareAndSet()  : bool
decr()  : int
expire()  : bool
Set TTL on a counter (N-3). Returns true if the TTL was applied.
get()  : int
incr()  : int
incrBounded()  : int|null
Bounded atomic increment (N-2). Increments by $by only if the resulting value would NOT exceed $maxBound. Returns the new value on success, or NULL when the cap would be exceeded (no-op).
mincr()  : array<string, int>
Batch increment (N-4). Pipelined where the backend supports it.
reset()  : void
set()  : bool
setIfAbsent()  : bool
Initialize a counter ONLY if it doesn't already exist (N-1).
key()  : string

Constants

LUA_CAS

private mixed LUA_CAS = "if redis.call('GET', KEYS[1]) == ARGV[1] then redis.call('SET', KEYS[1], ARGV[2]); return 1; else return 0; end"

Properties

Methods

compareAndSet()

public compareAndSet(string $name, int $expected, int $new) : bool
Parameters
$name : string
$expected : int
$new : int
Return values
bool

decr()

public decr(string $name[, int $by = 1 ]) : int
Parameters
$name : string
$by : int = 1
Return values
int

expire()

Set TTL on a counter (N-3). Returns true if the TTL was applied.

public expire(string $name, int $seconds) : bool

Atomic backend: no-op (returns false — shared memory has no TTL; the counter lives until the master dies). Redis backend: EXPIRE on the underlying key.

Parameters
$name : string
$seconds : int
Return values
bool

incr()

public incr(string $name[, int $by = 1 ]) : int
Parameters
$name : string
$by : int = 1
Return values
int

incrBounded()

Bounded atomic increment (N-2). Increments by $by only if the resulting value would NOT exceed $maxBound. Returns the new value on success, or NULL when the cap would be exceeded (no-op).

public incrBounded(string $name, int $by, int $maxBound) : int|null

Use case: rate-limit slots, pool counters where the cap matters.

Parameters
$name : string
$by : int
$maxBound : int
Return values
int|null

mincr()

Batch increment (N-4). Pipelined where the backend supports it.

public mincr(array<string|int, mixed> $deltas) : array<string, int>
Parameters
$deltas : array<string|int, mixed>

name → delta (defaults to 1 each)

Return values
array<string, int>

name → new value (in input order)

set()

public set(string $name, int $value) : bool
Parameters
$name : string
$value : int
Return values
bool

setIfAbsent()

Initialize a counter ONLY if it doesn't already exist (N-1).

public setIfAbsent(string $name, int $value) : bool

Returns true on first-time set; false if the counter already has a value (existing value is preserved).

Parameters
$name : string
$value : int
Return values
bool

key()

private key(string $name) : string
Parameters
$name : string
Return values
string
On this page