fiberphp / cache
⚡ PSR-16 缓存组件 —— Redis store(键前缀隔离、TTL 归一、NX 抢占)与多 store 静态门面。
Requires
- php: >=8.3
- ext-redis: *
- fiberphp/redis: dev-master
- psr/simple-cache: ^3.0
Requires (Dev)
- fiberphp/config: dev-master
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0
Suggests
- fiberphp/config: 安装后 Cache 门面自动读取 Config::get('cache');未安装时用 Cache::configure() 注入配置
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-30 10:39:48 UTC
README
PSR-16 缓存组件:Redis store(键前缀隔离、TTL 归一、NX 抢占)与多 store 静态门面。
环境要求
- PHP >= 8.3
- 扩展:ext-redis(phpredis 5.3+)
- fiberphp/redis(Redis store 的连接池客户端)
安装
composer require fiberphp/cache
快速上手
在 FiberPHP 应用中,配置写入 config/cache.php,门面直接读取,业务代码直接使用 Cache:
use FiberPHP\Cache\Cache;
Cache::set('name', 'fiberphp', 60); // 转发到默认 store
Cache::get('name');
$session = Cache::store('session'); // 命名 store(对应 config/cache.php 的 stores 键)
$session->set('user', ['id' => 1]);
PSR-16 全量接口:get / set / delete / clear / getMultiple / setMultiple / deleteMultiple / has,另有非 PSR 的 add(SET NX 抢占)。
独立使用
本包只强依赖 fiberphp/redis,不强依赖 fiberphp/config 或 fiberphp/framework。门面配置解析顺序:
Cache::configure($config)显式注入- 已安装 fiberphp/config 时读
Config::get('cache')(FiberPHP 应用自动满足) - 两者皆无:抛带指引的
RuntimeException
零 fiber/config 依赖的用法(array 驱动无需任何外部服务;redis 驱动还需 Manager::configure() 提供连接配置):
use FiberPHP\Cache\Cache;
Cache::configure([
'default' => 'default',
'stores' => [
'default' => ['driver' => 'array'],
],
]);
Cache::set('name', 'fiberphp', 60);
也可以直接实例化 PSR-16 store,完全不经过门面:
use FiberPHP\Cache\Driver\ArrayStore;
use FiberPHP\Cache\Driver\RedisStore;
use FiberPHP\Redis\Manager;
$array = new ArrayStore();
$redis = new RedisStore(
prefix: 'cache:',
manager: new Manager(['default' => ['host' => '127.0.0.1', 'port' => 6379]]),
);
安装 fiberphp/config 后也可走配置仓库(Config::load('', 'testing', ['cache' => [...], 'redis' => [...]])),门面会自动识别。
默认配置
包自带 config/cache.php 出厂默认(default store 走 redis 驱动、连接 default、前缀 cache:),在 FiberPHP 应用中由 Manifest 自动收集合并,装包零配置可用。覆盖方式与应用配置一致(深合并,后者覆盖前者):
// config/cache.php:只写要改的键,其余继承包默认
return [
'default' => 'session',
'stores' => [
'session' => [
'driver' => 'redis',
'connection' => 'session',
'prefix' => 'sess:',
'ttl' => 3600,
],
],
];
环境级差异放 config/{env}/cache.php。
配置项
| 字段 | 说明 | 默认值 |
|---|---|---|
default | 默认 store 名 | default |
stores.<name>.driver | 驱动:redis(连接池缓存)/ array(进程内数组,不跨进程共享,测试与单进程用) | redis |
stores.<name>.connection | Redis 连接名(对应 config/redis.php 中的键) | default |
stores.<name>.prefix | 缓存键前缀(PHP 侧拼接) | 'cache:' |
stores.<name>.ttl | 默认有效期秒数;null/0 表示永久 | null |
stores.<name>.flush_db | prefix 为空时 clear() 是否允许 FLUSHDB | false |
flush_db安全约束:prefix 为空时clear()默认抛异常——共享 db 会误删 lock/session/queue 等数据,仅在该 Redis db 由缓存独占时设为true。
测试
测试中用 Cache::swap($store, $name) 注入替身,Cache::reset() 清空全部已构建 store。
composer test # 单元测试(无外部依赖)
docker run -d --name fiberphp-redis-test -p 6379:6379 docker.m.daocloud.io/library/redis:7-alpine
./vendor/bin/phpunit --group integration # 集成测试
docker rm -f fiberphp-redis-test
License
MIT