webman-tech/laravel-cache
Composer 安装命令:
composer require webman-tech/laravel-cache
包简介
Webman plugin webman-tech/laravel-cache
README 文档
README
Split from webman-tech/laravel-monorepo
适用于 webman 的 Laravel 缓存组件,基于 illuminate/cache 实现。
安装
composer require webman-tech/laravel-cache
简介
该组件将 Laravel 强大的缓存功能引入 webman 框架中,使开发者能够使用 Laravel 的缓存 API。
所有方法和配置与 Laravel 几乎一致,因此使用方式可完全参考 Laravel Cache 文档。
特殊使用说明
1. Facades 使用方式
在 webman 中使用以下 Facades 替代 Laravel 的对应 Facades:
- 使用
WebmanTech\LaravelCache\Facades\Cache替代Illuminate\Support\Facades\Cache - 使用
WebmanTech\LaravelCache\Facades\CacheRateLimiter替代Illuminate\Support\Facades\RateLimiter - 使用
WebmanTech\LaravelCache\Facades\CacheLocker处理锁操作,支持watchdog前缀实现锁自动续期
2. 命令行支持
组件提供以下命令行工具:
# 删除缓存下的某个键 php webman cache:forget key_name # 清空所有缓存(注意:此方法使用 Cache::flush 来清除) php webman cache:clear
3. 扩展支持
在 config/plugin/webman-tech/laravel-cache/cache.php 中配置 extend:
return [ 'extend' => function(\Illuminate\Cache\CacheManager $cache) { $cache->extend('mongo', function () use ($cache) { return $cache->repository(new MongoStore); }); } ];
4. PSR 标准支持
// PSR-16 简单缓存 $psr16 = Cache::psr16(); // PSR-6 缓存池(需要安装 symfony/cache) $psr6 = Cache::psr6();
5. 限流中间件
组件实现了适用于 webman 路由的限流中间件:
use Webman\Route; use WebmanTech\LaravelCache\Middleware\ThrottleRequestsFactory; Route::get('/api/users', [UserController::class, 'index']) ->middleware([ new ThrottleRequestsFactory([ 'limiter_for' => 'api', // 需要在 rate_limiter.php 中配置 ]), ]);
6. Watchdog Lock(锁自动续期)
Laravel 的 Cache Lock 有固定的 TTL,长任务执行期间锁可能过期。Watchdog Lock 在锁持有期间自动续期,防止锁意外丢失。
// 通过 CacheLocker 使用 watchdog 锁(推荐) $lock = CacheLocker::watchdogOrder('order_123', 30); // 30s TTL,每 10s 自动续期 $lock->get(function () { // 长时间任务,锁不会过期 $this->processOrder(); }); // 手动 acquire/release 模式 $lock = CacheLocker::watchdogOrder('order_123', 30); if ($lock->acquire()) { try { $this->processOrder(); } finally { $lock->release(); } }
watchdog 前缀的使用方式与 restore 前缀一致,仅影响行为,不影响锁名:
CacheLocker::order('key', 30); // 普通锁,锁名: order:key CacheLocker::watchdogOrder('key', 30); // watchdog 锁,锁名: order:key(同一把锁)
支持所有 cache store 类型(Redis、Database、Memcached、File、Array),Redis 使用 Lua 脚本原子续期,Database 使用条件更新,其他 store 尽力而为。
注意事项
Cache::flush()会清空存储器下的所有数据,而非仅当前应用的缓存- 可通过配置
app.flush.prevent = true禁止使用 flush 方法 - 缓存的默认过期时间为永久,需要手动设置过期时间
webman-tech/laravel-cache 适用场景与选型建议
webman-tech/laravel-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.05k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 12 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 webman-tech/laravel-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webman-tech/laravel-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-14