hareland/multi-cache-remember
Composer 安装命令:
composer require hareland/multi-cache-remember
包简介
This package offers the rememberMany macro for the Laravel Cache facade, providing efficient retrieval of multiple cache keys with customizable expiration and callbacks for missing keys.
README 文档
README
What does this package solve?
This package offers the rememberMany macro for the Laravel Cache facade, providing efficient retrieval of multiple cache keys with customizable expiration and callbacks for missing keys.
The rememberMany macro for the Laravel Cache facade provides several advantages over the traditional method of looking up multiple cache keys one by one.
Why?
-
Reduced number of cache lookups: With the traditional method, each cache key is looked up individually, even if some of them are being retrieved in quick succession. With the rememberMany macro, all the requested keys are retrieved in a single call to Cache::many(), which can significantly reduce the number of cache lookups and improve performance.
-
Customizable cache expiration: The rememberMany macro allows you to specify an expiration time for all the cached values at once, which can simplify the cache management process and make it easier to ensure that your cached data is up-to-date.
-
Callbacks for missing keys: The rememberMany macro allows you to specify a callback function for each cache key, which will be called only if the key is not found in the cache. This is similar to the
Cache::remember()method.
Install
composer require hareland/multi-cache-remember
Register ServiceProvider (if you must)
// in config/app.php 'providers' => [ \Hareland\MultiCacheRemember\MultiCacheServiceProvider::class, ]
Usage
To set the default TTL, you can set this in
config/services.phpon the keymulti_remember.default_ttl-> You will have to set it in seconds.
Simple (No custom TTL)
<?php use Illuminate\Support\Facades\Cache; [$user1, $user2, $meta] = Cache::rememberMany([ 'user:1' => fn ()=> \App\Models\User::findOrFail(1), 'user:2' => fn ()=> \App\Models\User::findOrFail(2), 'meta:11'=> fn ()=> \App\Models\Meta::findOrFail(11), ], 5);// 5 seconds is now the TTL for all the items.
Custom TTL on some keys
<?php use Illuminate\Support\Facades\Cache; [$topStats, $orgSales, $overview] = Cache::rememberMany([ 'dashboard.stats.top:user:1' => [fn() => \App\Models\Stats::findFor(request()->user()), 60 * 15], 'dashboard.stats.sales:org:3' => [fn() => \App\Models\StatsForOrf::findFor(request()->user()->currentOrg), 60 * 5], 'dashboard.stats.overview:org:3' => fn() => \App\Models\OverviewStats::findFor(request()->user()->currentOrg), ], 60); // 60 seconds is the default TTL for any keys that does not have a custom one.
hareland/multi-cache-remember 适用场景与选型建议
hareland/multi-cache-remember 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 195 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hareland/multi-cache-remember 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hareland/multi-cache-remember 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 195
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-05