mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-02 14:25:38 +08:00
Signal handling fixes: - SignalListener: add SIGHUP/SIGTERM handling for both Swoole and Workerman drivers in master and worker processes - Prevent 100% CPU when IDE terminal is closed by ensuring graceful shutdown on terminal hangup PHP version support: - Widen PHP constraint to 8.3, 8.4, 8.5 - Bump doctrine/dbal from ^2.13.1 to ^4.4 - Bump php-cs-fixer to ^3.64, phpstan to ^1.12 - Bump swoole/ide-helper to ^5.0 - Drop phpunit ^8.5 (EOL), keep ^9.0 CI updates: - actions/checkout@v3 → @v4 (Node.js 20 deprecated) - Bump static analysis/code style PHP from 8.1 to 8.3
114 lines
3.4 KiB
PHP
114 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace ZM\Store\KV\Redis;
|
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
use ZM\Store\KV\KVInterface;
|
|
|
|
class KVRedis implements KVInterface
|
|
{
|
|
private string $pool_name;
|
|
|
|
public function __construct(private string $name = '')
|
|
{
|
|
$this->pool_name = config('global.kv.redis_config', 'default');
|
|
}
|
|
|
|
public static function open(string $name = ''): CacheInterface
|
|
{
|
|
return new KVRedis($name);
|
|
}
|
|
|
|
public function get(string $key, mixed $default = null): mixed
|
|
{
|
|
/** @var ZMRedis $redis */
|
|
$redis = RedisPool::pool($this->pool_name)->get();
|
|
$ret = $redis->get($this->name . ':' . $key);
|
|
if ($ret === false) {
|
|
$ret = $default;
|
|
} else {
|
|
$ret = unserialize($ret);
|
|
}
|
|
RedisPool::pool($this->pool_name)->put($redis);
|
|
return $ret;
|
|
}
|
|
|
|
public function set(string $key, mixed $value, \DateInterval|int|null $ttl = null): bool
|
|
{
|
|
/** @var ZMRedis $redis */
|
|
$redis = RedisPool::pool($this->pool_name)->get();
|
|
$ret = $redis->set($this->name . ':' . $key, serialize($value), $ttl);
|
|
RedisPool::pool($this->pool_name)->put($redis);
|
|
return (bool) $ret;
|
|
}
|
|
|
|
public function delete(string $key): bool
|
|
{
|
|
/** @var ZMRedis $redis */
|
|
$redis = RedisPool::pool($this->pool_name)->get();
|
|
$ret = $redis->del($this->name . ':' . $key);
|
|
RedisPool::pool($this->pool_name)->put($redis);
|
|
return (bool) $ret;
|
|
}
|
|
|
|
public function clear(): bool
|
|
{
|
|
/** @var ZMRedis $redis */
|
|
$redis = RedisPool::pool($this->pool_name)->get();
|
|
$ret = $redis->del($this->name . ':*');
|
|
RedisPool::pool($this->pool_name)->put($redis);
|
|
return (bool) $ret;
|
|
}
|
|
|
|
public function getMultiple(iterable $keys, mixed $default = null): iterable
|
|
{
|
|
/** @var ZMRedis $redis */
|
|
$redis = RedisPool::pool($this->pool_name)->get();
|
|
foreach ($keys as $key) {
|
|
$value = $redis->get($this->name . ':' . $key);
|
|
if ($value === false) {
|
|
$value = $default;
|
|
} else {
|
|
$value = unserialize($value);
|
|
}
|
|
yield $key => $value;
|
|
}
|
|
RedisPool::pool($this->pool_name)->put($redis);
|
|
}
|
|
|
|
public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool
|
|
{
|
|
/** @var ZMRedis $redis */
|
|
$redis = RedisPool::pool($this->pool_name)->get();
|
|
$ret = true;
|
|
foreach ($values as $key => $value) {
|
|
$ret = $ret && $redis->set($this->name . ':' . $key, serialize($value), $ttl);
|
|
}
|
|
RedisPool::pool($this->pool_name)->put($redis);
|
|
return $ret;
|
|
}
|
|
|
|
public function deleteMultiple(iterable $keys): bool
|
|
{
|
|
/** @var ZMRedis $redis */
|
|
$redis = RedisPool::pool($this->pool_name)->get();
|
|
$ret = true;
|
|
foreach ($keys as $key) {
|
|
$ret = $ret && $redis->del($this->name . ':' . $key);
|
|
}
|
|
RedisPool::pool($this->pool_name)->put($redis);
|
|
return $ret;
|
|
}
|
|
|
|
public function has(string $key): bool
|
|
{
|
|
/** @var ZMRedis $redis */
|
|
$redis = RedisPool::pool($this->pool_name)->get();
|
|
$ret = $redis->exists($this->name . ':' . $key);
|
|
RedisPool::pool($this->pool_name)->put($redis);
|
|
return (bool) $ret;
|
|
}
|
|
}
|