\Framework\UtilsRedisFactory

Redis 客户端工厂(带自动重连 + 故障切换 + 自动续期分布式锁).

Summary

Methods
Properties
Constants
createRedisClient()
client()
set()
get()
del()
exists()
incr()
decr()
hSet()
hGet()
hGetAll()
expire()
lock()
unlock()
transaction()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
connect()
exec()
$configList
$redis
$maxRetries
$retryDelay
$lockTimers
N/A

Properties

$configList

$configList : array

Type

array — Redis 节点配置

$redis

$redis : null|\Redis

Type

Redis — 当前连接

$maxRetries

$maxRetries : int

Type

int — 最大重试次数

$retryDelay

$retryDelay : float

Type

float — 重试间隔(秒)

$lockTimers

$lockTimers : array

Type

array — array<string, array{timer_id: int, ttl: int}> 锁续期定时器映射

Methods

createRedisClient()

createRedisClient(array  $configList) : \Redis

Parameters

array $configList

Returns

\Redis —

client()

client() : \Redis

Returns

\Redis —

set()

set(string  $key, mixed  $value, int  $ttl, bool  $nx = false) : bool

Parameters

string $key
mixed $value
int $ttl
bool $nx

Returns

bool —

get()

get(string  $key) : mixed

Parameters

string $key

Returns

mixed —

del()

del(array|string  $key) : int

Parameters

array|string $key

Returns

int —

exists()

exists(string  $key) : bool

Parameters

string $key

Returns

bool —

incr()

incr(string  $key, int  $by = 1) : int

Parameters

string $key
int $by

Returns

int —

decr()

decr(string  $key, int  $by = 1) : int

Parameters

string $key
int $by

Returns

int —

hSet()

hSet(string  $hash, string  $field, mixed  $value) : int

Parameters

string $hash
string $field
mixed $value

Returns

int —

hGet()

hGet(string  $hash, string  $field) : mixed

Parameters

string $hash
string $field

Returns

mixed —

hGetAll()

hGetAll(string  $hash) : array

Parameters

string $hash

Returns

array —

expire()

expire(string  $key, int  $ttl) : bool

Parameters

string $key
int $ttl

Returns

bool —

lock()

lock(string  $key, int  $ttl = 10, int  $renewTtl = null) : false|string

获取分布式锁(带自动续期).

Parameters

string $key

锁名称

int $ttl

初始过期时间(秒)

int $renewTtl

续期时间(秒),建议为 ttl 的 1/3

Returns

false|string —

成功返回唯一 token,失败返回 false

unlock()

unlock(string  $key, string  $token) : void

释放锁(安全删除 + 清理续期任务).

Parameters

string $key

锁名称

string $token

加锁时返回的 token

transaction()

transaction(callable  $callback) : array

事务封装.

Parameters

callable $callback

Returns

array —

connect()

connect() : void

exec()

exec(callable  $fn) : mixed

通用执行器(带重试与故障转移).

Parameters

callable $fn

Returns

mixed —