natsumework/laravel-cache-manager
Composer 安装命令:
composer require natsumework/laravel-cache-manager
包简介
Allows you to easily manage the cache with config file. And provide a simple solution to solve the cache penetration and hotspot invalid.
README 文档
README
Allows you to easily manage the cache with config file. And provide a simple solution to solve the cache penetration and hotspot invalid.
Contents
Installation
Install this package via composer:
composer require natsumework/laravel-cache-manager
Publish the configuration file to config/cache-manager.php:
php artisan vendor:publish --provider="Natsumework\CacheManager\CacheManagerServiceProvider"
Configuration
config/cache-manager.php
General Configuration
return [
// ...
/*
|--------------------------------------------------------------------------
| Cache Store
|--------------------------------------------------------------------------
|
| This option controls the cache connection that gets used while
| using this caching library.
|
| Before use the "store", you need to define your `Cache Stores` in config/cache.php
|
| @see https://laravel.com/docs/master/cache#configuration
*/
'store' => 'file',
/*
|--------------------------------------------------------------------------
| Storage time
|--------------------------------------------------------------------------
|
| Specifies that items will expire in a few seconds.
| If set to null, items will be stored indefinitely.
}
*/
'ttl' => 600,
// ...
];
Hotspot Invalid Protection Configuration
return [
// ...
/*
|--------------------------------------------------------------------------
| Hotspot Invalid Protection
|--------------------------------------------------------------------------
|
| When we set the cache, we usually set an expiration time for the cache.
| After this time, the cache will be invalid.
| For some hotspot data, when the cache fails, there will be a large number
| of requests coming over, and then hit the database, which will cause the
| database enormous pressure.
|
| Set "hotspot_protection" to `true` will use the "Mutex lock" to avoid
| the problem of database corruption caused by the failure of hotspot data.
|
| You must confirm that the store you are using supports atomic-locks.
| @see https://laravel.com/docs/master/cache#atomic-locks
*/
'hotspot_protection' => false,
/*
|--------------------------------------------------------------------------
| Hotspot Invalid Protection Lock Timeout
|--------------------------------------------------------------------------
|
| Specifies that "Mutex lock" will be automatically released in a few seconds.
|
*/
'hotspot_protection_ttl' => 15,
/*
|--------------------------------------------------------------------------
| Hotspot Invalid Protection Suffix
|--------------------------------------------------------------------------
|
| When Hotspot Invalid Protection is enabled, `{type}:{index}:{hotspot_protection_suffix}`
| will be used as the key of "Mutex lock".
| You must specify a unique value and avoid using this value as the key for storing data,
| otherwise the data may be overwritten due to duplicate keys.
|
*/
'hotspot_protection_suffix' => 'hotspot_protection',
// ...
];
Penetrate Protection Configuration
return [
// ...
/*
|--------------------------------------------------------------------------
| Penetrate Protection
|--------------------------------------------------------------------------
|
| When the business system initiates a query, the query will first go to
| the cache, if the cache does not exist, it will go to the database for query.
| If no data is found in the database, the data will not be stored in the cache,
| which will cause the query hit the database every time.
|
| When Penetrate protection is enabled, `null` data will be converted to `false`
| and stored in the cache.
*/
'penetrate_protection' => false,
// ...
];
Types Configuration
This configuration allows you to easily manage all caches here.
Notes
- Before you put item into the cache storage, you have to define the type first.
return [
// ...
/*
|--------------------------------------------------------------------------
| Cache Types
|--------------------------------------------------------------------------
| Before you put item into the cache storage, you have to define the type first.
|
| You can also define `ttl`, `hotspot_protection`, `penetrate_protection`,
| `hotspot_protection_ttl` for each type. It takes precedence over a globally defined one.
|
*/
'types' => [
'user' => [
'ttl' => 300, // seconds
'hotspot_protection' => true,
'penetrate_protection' => true,
'hotspot_protection_ttl' => 10, // seconds
],
'active_users' => [
'ttl' => 60 * 60,
'hotspot_protection' => false,
'penetrate_protection' => false,
],
'post' => [],
// ...
],
// ...
];
Usage
Retrieve & Store item
If the item does not exist in the cache, the Closure passed to the remember method will be executed and its result will be placed in the cache.
use Natsumework\CacheManager\Facades\CacheManager;
// CacheManager::remember($type, $index = null, \Closure $callback = null)
// you must define type `user` in config/cache-manager.php
$user = CacheManager::remember('user', 1, function () {
return User::find(1);
});
$data = CacheManager::remember('type', 999, function () {
return null;
});
// If `penetration_protection` is enabled, $data === false
// If `penetration_protection` is not enabled, $data === null
After the item is updated, use the Cache-Aside Pattern to invalid the item
The updated method will remove item.
// CacheManager::updated($type, $index = null)
$user = User::find(1);
$user->name = 'New name';
$user->save();
// user updated
CacheManager::updated('user', 1);
You can also use the forget method to remove item.
CacheManager::forget('user', 1);
Retrieve item
use Natsumework\CacheManager\Facades\CacheManager;
// CacheManager::get($type, $index = null, $default = null)
// you must define type `user` in config/cache-manager.php
$user = CacheManager::get('user', 1, 'user not found');
Storing Items In The Cache
use Natsumework\CacheManager\Facades\CacheManager;
// CacheManager::put($type, $index = null, $value = null)
// you must define type `user` in config/cache-manager.php
CacheManager::put('user', 1, User::find(1));
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Security
If you discover any security related issues, please email natsumework0902@gmail.com instead of using the issue tracker.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
natsumework/laravel-cache-manager 适用场景与选型建议
natsumework/laravel-cache-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 479 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「cache」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 natsumework/laravel-cache-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 natsumework/laravel-cache-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 natsumework/laravel-cache-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Microservice RPC through message queues.
The CodeIgniter Redis package
贝嘟分布式缓存扩展
Laravel 5 - Repositories to the database layer
Redis distributed locks for laravel
统计信息
- 总下载量: 479
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-24