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

RedisSessionHandler
in package
implements SessionHandlerInterface

Redis-backed session handler for ZealPHP.

Reads/writes session data using the same key format as PHP's phpredis session handler (PHPREDIS_SESSION:{session_id}), so sessions created by Apache/mod_php are readable by ZealPHP and vice versa.

Coroutine safety (issue #16)

phpredis (\Redis) is not coroutine-safe: a single connection multiplexed across concurrent coroutines interleaves request/response frames on the same socket, so one coroutine can read another's reply (or garbage). Sharing one handler instance — the common onWorkerStart pattern — therefore corrupted session reads under load, which write_close() then persisted (a 24-key session collapsing to a handful of keys).

This handler keeps one connection per coroutine, stored in the coroutine's context and reaped when the coroutine ends, so concurrent requests never share a socket. Outside a coroutine (CLI, tests) it uses a single fallback connection created at construction. High-throughput deployments that want to avoid per-request connection churn should front this with a connection pool; the per-coroutine model here is correct-by-default.

Every method requires a live Redis connection, so it is verified against a real server rather than unit tests — excluded from coverage measurement (no offline seam without shipping a Redis mock).

Tags
codeCoverageIgnore

Table of Contents

Interfaces

SessionHandlerInterface

Properties

$fallback  : Redis|null
Single connection used outside coroutine context (CLI / tests).
$host  : string
$port  : int
$prefix  : string
$ttl  : int

Methods

__construct()  : mixed
close()  : bool
destroy()  : bool
gc()  : int|false
getRedis()  : Redis
The Redis connection for the current coroutine (or the fallback).
open()  : bool
read()  : string
write()  : bool
connect()  : Redis
redis()  : Redis
Per-coroutine Redis connection. Each coroutine gets its own socket so concurrent session I/O never crosses frames (#16). Outside a coroutine, the construction-time fallback connection is reused.

Properties

$fallback

Single connection used outside coroutine context (CLI / tests).

private Redis|null $fallback = null

Methods

__construct()

public __construct([string $host = '127.0.0.1' ][, int $port = 6379 ][, string $prefix = 'PHPREDIS_SESSION:' ][, int $ttl = 1440 ]) : mixed
Parameters
$host : string = '127.0.0.1'
$port : int = 6379
$prefix : string = 'PHPREDIS_SESSION:'
$ttl : int = 1440

destroy()

public destroy(mixed $sessionId) : bool
Parameters
$sessionId : mixed
Return values
bool

gc()

public gc(mixed $maxlifetime) : int|false
Parameters
$maxlifetime : mixed
Return values
int|false

getRedis()

The Redis connection for the current coroutine (or the fallback).

public getRedis() : Redis
Return values
Redis

open()

public open(mixed $savePath, mixed $sessionName) : bool
Parameters
$savePath : mixed
$sessionName : mixed
Return values
bool

read()

public read(mixed $sessionId) : string
Parameters
$sessionId : mixed
Return values
string

write()

public write(mixed $sessionId, mixed $sessionData) : bool
Parameters
$sessionId : mixed
$sessionData : mixed
Return values
bool

redis()

Per-coroutine Redis connection. Each coroutine gets its own socket so concurrent session I/O never crosses frames (#16). Outside a coroutine, the construction-time fallback connection is reused.

private redis() : Redis
Return values
Redis
On this page