natsumework / laravel-cache-manager

允许您通过配置文件轻松管理缓存。并提供简单的解决方案解决缓存穿透和热点失效问题。

1.1.0 2024-03-23 13:14 UTC

This package is auto-updated.

Last update: 2024-09-23 14:28:46 UTC


README

Latest Version on Packagist Test Status Software License Total Downloads

允许您通过配置文件轻松管理缓存。并提供简单的解决方案解决缓存穿透和热点失效问题。

内容

安装

使用composer安装此包

composer require natsumework/laravel-cache-manager

将配置文件发布到config/cache-manager.php

php artisan vendor:publish --provider="Natsumework\CacheManager\CacheManagerServiceProvider"

配置

config/cache-manager.php

通用配置

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.net.cn/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,

    // ...
];

热点失效保护配置

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.net.cn/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',

    // ...
];

穿透保护配置

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,

    // ...
];

类型配置

此配置允许您轻松管理这里的所有缓存。

注意
  • 在将项目放入缓存存储之前,您必须先定义类型。
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' => [],

        // ...
    ],

    // ...
];

用法

检索和存储项目

如果项目不存在于缓存中,将执行传递给remember方法的闭包,并将其结果放置在缓存中。

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

项目更新后,使用Cache-Aside模式使项目失效

使用updated方法移除项目。

// CacheManager::updated($type, $index = null)

$user = User::find(1);
$user->name = 'New name';
$user->save();

// user updated
CacheManager::updated('user', 1);

您也可以使用forget方法来移除项目。

CacheManager::forget('user', 1);

检索项目

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');

在缓存中存储项目

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

测试

$ composer test

安全

如果您发现任何安全相关的问题,请通过电子邮件natsumework0902@gmail.com报告,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅CONTRIBUTING

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件