Files
zhamao-framework/src/ZM/Store/KV/Redis/KVRedis.php

114 lines
3.4 KiB
PHP
Raw Normal View History

2023-01-03 00:50:31 +08:00
<?php
declare(strict_types=1);
namespace ZM\Store\KV\Redis;
use Psr\SimpleCache\CacheInterface;
use ZM\Store\KV\KVInterface;
2023-01-13 14:19:20 +08:00
class KVRedis implements KVInterface
2023-01-03 00:50:31 +08:00
{
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
2023-01-03 00:50:31 +08:00
{
/** @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
2023-01-03 00:50:31 +08:00
{
/** @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;
}
}