veasin/ff-redis
Composer 安装命令:
composer require veasin/ff-redis
包简介
Redis connection pool, queue driver, and cache driver for ff framework
README 文档
README
Redis connection pool, queue driver, and cache driver for ff framework (PHP 8.4+).
composer require veasin/ff-redis
Connection Pool
按配置名管理 Redis 连接生命周期,支持惰性验证与自动重连。由 ff\resource\redis() 函数实现,被 ff\cache\redis() 和 ff\queue\redis() 驱动内部自动调用,也可直接使用。
use function ff\resource\redis; $redis = redis('default'); // get/create default connection $redis = redis('cache'); // get/create named connection redis(null); // clear all connections + failure flags
连接通过 container("#redis.{name}") 配置。默认 $name = 'default',未配置时自动回退:
['host' => '127.0.0.1', 'port' => 6379]
Redis 扩展不存在或连接失败时返回 null,不会抛异常。
惰性验证
通过 #resource.redis.ttl 设置验证间隔(秒),超时后自动执行 ping() 检测连接健康,断连时自动重建。默认 0 不验证。
container('#resource.redis.ttl', 30); // 每 30 秒验证一次
自定义工厂
Swoole 协程版 Redis 或其他自定义实现:
container('#resource/redis:', fn($config) => { $c = new \Swoole\Coroutine\Redis(); $c->connect($config['host'], $config['port'] ?? 6379); return $c; });
配置项
| 容器 key | 类型 | 说明 |
|---|---|---|
#redis.{name} |
array |
Redis 连接配置,支持 host、port、password、database、timeout、prefix |
#resource.redis.ttl |
int |
连接验证间隔秒数,默认 0 不验证 |
#resource/redis: |
callable |
自定义 Redis 工厂,签名 fn(array $config): \Redis |
Queue Driver
基于 Redis list 的队列驱动,生产使用 lPush + serialize,消费使用 brPop 阻塞弹出。
注册
container('^#ext.queue.redis', \ff\queue\redis(...));
ff-redis 的 bootstrap.php 已自动完成注册,安装后即可使用。
使用
// 生产 ff\queue\redis('orders', ['order_id' => 123]); ff\queue\redis('orders', 'plain text'); // 消费 ff\queue\redis('orders', fn($msg) => true); // ack ff\queue\redis('orders', fn($msg) => false); // nack + requeue ff\queue\redis('orders', fn($msg) => null); // nack + discard ff\queue\redis('orders', fn($msg) => 5); // retry after 5 seconds
独立使用
在 ext('queue', ...) 之外也可直接调用底层驱动:
use function ff\queue\redis; redis('orders', ['id' => 1]); // 生产 redis('orders', fn($m) => true); // 消费 redis('orders', fn($m) => true, ['timeout' => 10]); // 指定超时
timeout 为 brPop 阻塞等待秒数,0 表示永久阻塞。
配置
通过 container('#queue/redis') 配置:
container('#queue/redis', [ 'config' => 'default', // 使用的 Redis 连接名(#redis.{name}) 'prefix' => 'q:', // 队列 key 前缀 'timeout' => 10, // brPop 超时秒数,0 永久阻塞 ]);
| 容器 key | 类型 | 说明 |
|---|---|---|
#queue/redis |
array |
队列驱动配置:config、prefix、timeout、ttl |
Cache Driver
签名与 apcu() 完全一致。底层使用 ff\resource\redis() 获取连接,无需单独配置连接。
CRUD
use function ff\cache\redis; redis('key'); // read redis('key', 'value'); // write redis('key', null); // delete redis(null); // flush all redis('key', 'value', 60); // write + TTL redis('key', 'value', ['ttl' => 60, 'config' => 'cfg']); // TTL + 配置名 redis('key', 'value', 'cfg'); // 配置名(string 简写) // Batch redis(['k1', 'k2']); // multi-get redis(['k1' => 'v1']); // multi-set
中间件工厂模式
与 cache() 编排函数配合使用:
cache(redis('my_key', middleware: ['ttl' => 60]), fn($next) => render()); cache(redis('my_key', 'fallback', middleware: 60), fn($next) => null);
配置
container('#cache.redis', [ 'host' => '127.0.0.1', 'port' => 6379, 'password' => 'secret', 'database' => 0, ]); // 命名配置 container('#cache.redis.cfg', [ 'ttl' => 3600, 'prefix' => 'c:', ]);
| 容器 key | 类型 | 说明 |
|---|---|---|
#cache.redis |
array |
默认缓存 Redis 连接配置 |
#cache.redis.{name} |
array |
命名配置,支持 ttl、prefix |
Ext Registration
ff-redis 自动在 bootstrap.php 中注册 ext 驱动(通过 composer.json 的 autoload.files 加载):
container('^#ext.queue.redis', \ff\queue\redis(...)); container('^#ext.cache.redis', \ff\cache\redis(...));
安装后即可通过 ext('queue', 'redis', ...) 和 ext('cache', 'redis', ...) 调用。
也支持直接 use function ff\queue\redis / use function ff\cache\redis 独立使用。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2026-07-08