michaeljennings/broker
Composer 安装命令:
composer require michaeljennings/broker
包简介
A laravel package for caching keys against an object
README 文档
README
A laravel package that allows you to cache items against an object. For example you may want to cache a user's navigation items against the user model.
broker()->put($user, 'navigation', $navigationItems, 60);
Installation
To install through composer simply run the following command:
composer require michaeljennings/broker
Or add the following to your composer.json file:
{
"require": {
"michaeljennings/broker": "^1.0"
}
}
And then run composer install.
For Laravel 5.5 and upwards, the service provider and facade will be loaded automatically. For older versions of Laravel, you will need to add the broker service provider into your providers array in config/app.php.
'providers' => [ ... 'Michaeljennings\Broker\BrokerServiceProvider' ... ];
The package also comes with a facade, to use it add it to your aliases array in config/app.php.
'aliases' => [ ... 'Broker' => 'Michaeljennings\Broker\Facades\Broker', ... ];
Cacheable Entities
To make a class cacheable you need to implement the Michaeljennings\Broker\Contracts\Cacheable interface and implement the getCacheKey method. This must return a unique key for this class.
class Dummy implements \Michaeljennings\Broker\Contracts\Cacheable { public function getCacheKey() { return get_class($this); } }
Cacheable Models
If you want to make an eloquent model cacheable you can use the Michaeljennings\Broker\Traits\Cacheable trait. This automatically uses the table name as the cache key.
use Illuminate\Database\Eloquent\Model; class Dummy extends Model implements \Michaeljennings\Broker\Contracts\Cacheable { use \Michaeljennings\Broker\Traits\Cacheable; }
Usage
Broker can be accessed either by its facade, its helper method, or through its IOC binding.
// Facade Michaeljennings\Broker\Facades\Broker::get($cachable, 'key'); // Helper broker()->get($cachable, 'key'); // IOC Binding app(Michaeljennings\Broker\Contracts\Broker)->get($cachable, 'key');
Retrieving Items From Cache
The get method is used to retrieve an item for the cacheable entity. If the item does not exist null will be returned.
broker()->get($cacheable, 'key');
Checking If An Item Exists
The has method will check if the key has been set for the cacheable entity.
broker()->has($cacheable, 'key');
Retrieve/Store
Occasionally you may want to retrieve an item, but also set the value if it is not set. You can do this using the remember method.
broker()->remember($cacheable, 'key', function() { return DB::table('users')->get(); });
Storing Items In The Cache
The put method will add an item to the cache for the cacheable entity. By default items will be stored for 60 minutes, but you can specify the amount minutes.
broker()->put($cacheable, 'key', 'value'); broker()->put($cacheable, 'key', 'value', $minutes);
Storing Items Forever
The forever method will store items in the cache indefinitely. These items will need to be removed manually with the forget method.
broker()->forever($cacheable, 'key');
Removing Items From The Cache
The forget method will remove a specific item from the cache.
broker()->forget($cacheable, 'key');
Or you may remove all of the items for a cacheable entity with the flush method.
broker()->flush($cacheable);
Occasionally changes in your application may require you to flush the cache for every entity of a cacheable type, for example you need to clear the cache for every user, but want the rest of the cache to remain.
To this you can use the flushAll method and pass it the class you want to flush.
broker()->flushAll(App\User::class);
michaeljennings/broker 适用场景与选型建议
michaeljennings/broker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.22k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 04 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 michaeljennings/broker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 michaeljennings/broker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-26