hyperf框架中基于swoole table实现的redis上级缓存
背景:
生产环境中出现 Redis 带宽满载的情况,故尝试使用 SwooleTable 做一层对 Redis 的缓存
hyperf memory cache
基于 Swoole Hyperf 框架,将 SwooleTable 作为 Redis 的上级缓存
安装
使用 composer
composer require aston/memory-cache
发布配置文件
php bin/hyperf.php vendor:publish aston/memory-cache
配置文件说明
[
'default' => [
'driver' => SwooleTableDriver::class,
'packer' => PhpSerializerPacker::class,
'tables' => [
'cache' => [
//内存表最大行数
'table_size' => 1024,
'column_value' => [
'type' => Table::TYPE_STRING,//内存表缓存值的字段类型
'size' => 1024//缓存值最大存储长度 设置后,设置的字符串不能超过 size 指定的最大长度
],
//拦截读取缓存的redis命令
'commands' => [
'get', 'hGet', 'hGetAll', 'hMGet', 'hLen' //目前最多支持这么多,可指定拦截命令,留空代表不拦截,
]
],
],
],
]
实现原理:
通过 AOP 拦截 Redis 的读操作(目前只实现了string、hash类型)
#[Aspect]
class CacheAspect extends AbstractAspect
{
public $classes = [Redis::class];
private const ALLOWED_METHODS = [
'get', 'hget', 'hgetall', 'hmget', 'hlen'
];
...
创建自定义进程订阅 Redis 键事件,通过事件维护Swoole Table中的数据过期与删除保证数据一致性
class RedisEventProcess extends AbstractProcess
{
public function handle(): void
{
$pool = $this->container->get(ConfigInterface::class)->get('redis.pool') ?? 'default';
$db = $this->container->get(ConfigInterface::class)->get("redis.$pool.db") ?? 0;
$this->redis = $this->container->get(RedisFactory::class)->get($pool);
$this->redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
$this->redis->config('SET', 'notify-keyspace-events', 'KEA'); // 设置参数
$this->redis->psubscribe([
"__keyevent@{$db}__:del",
"__keyevent@{$db}__:expired",
"__keyevent@{$db}__:hdel",
"__keyevent@{$db}__:hset",
"__keyevent@{$db}__:set",
"__keyevent@{$db}__:rename_from",
"__keyevent@{$db}__:rename_to",
], [$this, 'onPublish']);
}
public function onPublish($redis, string $pattern, string $event, string $key)
{
$arr = explode(':', $event);
$method = end($arr);
$this->logger->debug("触发redis键事件, event: $event, , method: $method, key: $key");
switch (Str::lower($method)) {
case 'set':
$latest = $this->redis->rawCommand('get', $key);
if (!$latest) {
return;
}
$this->logger->debug("刷新string内存缓存 key: $key");
$this->driver->set($key, $latest);
break;
case 'hset':
case 'hdel':
$latest = $this->redis->rawCommand('hGetAll', $key);
if (!$latest) {
return;
}
$hash = array();
for ($i = 0; $i < count($latest); $i += 2) {
$hash[$latest[$i]] = $latest[$i + 1];
}
$this->logger->debug("刷新hash内存缓存 key: $key");
$this->driver->set($key, $this->driver->packer()->pack($hash));
break;
default:
$this->logger->debug("删除内存缓存 key: $key");
$this->driver->del($key);
break;
}
}
}
使用
在 Hyperf 中正常调用 Redis 即可,开发中无需关心数据是从内存表中获取还是从 Redis 获取到的。
如果在 config.php 开启了 DEBUG 等级的日志,开发时会打印对应日志。
AstonChenDev/hyperf-memory-cache: 基于 Swoole Hyperf 框架,将 SwooleTable 作为 Redis 的上级缓存 (github.com)
本作品采用《CC 协议》,转载必须注明作者和本文链接
redis设置set的时候,也在swoole的table里面set一份,读取的时候先读取swoole,没有在读取redis?这样不是双倍内存吗