suitmedia/laravel-cacheable

装饰您的仓库并使其可缓存

1.12.0 2024-03-13 12:30 UTC

This package is auto-updated.

Last update: 2024-09-13 13:28:12 UTC


README

Build codecov Total Downloads Latest Stable Version License: MIT

Laravel Cacheable

装饰您的仓库并使其可缓存

摘要

此包将帮助您使仓库可缓存,无需担心如何管理缓存,并提供一种简单的方式来使缓存无效。Laravel Cacheable 包使用动态装饰器类来包装现有的仓库,并在其中添加自动缓存功能。

目录

兼容性

要求

此包要求您使用支持标签的缓存存储,如memcached或redis。如果您使用不支持标签的任何缓存存储,则可能会出现错误。

设置

使用Composer安装包

$ composer require suitmedia/laravel-cacheable

如果您使用Laravel版本5.5及以上,则可以跳过在应用程序中注册服务提供者和包别名。

注册服务提供者

在您的 config/app.php 中添加包服务提供者

'providers' => [
    // ...
    \Suitmedia\Cacheable\ServiceProvider::class,
];

注册包别名

在您的 config/app.php 中添加包别名

'aliases' => [
    // ...
    'Cacheable' => \Suitmedia\Cacheable\Facade::class,
];

配置

使用 php artisan 命令发布配置文件

$ php artisan vendor:publish --provider="Suitmedia\Cacheable\ServiceProvider"

上述命令会将新的配置文件复制到 /config/cacheable.php

return [

    /*
    |--------------------------------------------------------------------------
    | Custom Tags
    |--------------------------------------------------------------------------
    |
    | Define all of Eloquent Models which should add custom cache tags
    | automatically to the cached objects.
    |
    */

    'customTags' => \App\User::class,

    /*
    |--------------------------------------------------------------------------
    | Default Cache Duration
    |--------------------------------------------------------------------------
    |
    | Define the default cache duration here.
    | Setting the cache duration to '0' will make the cache lasts forever.
    |
    */

    'duration' => 0,

    /*
    |--------------------------------------------------------------------------
    | Methods which shouldn't be cached
    |--------------------------------------------------------------------------
    |
    | Define a collection of method names which you don't wish
    | to be cached.
    |
    */

    'except' => [
        'cacheDuration',
        'cacheExcept',
        'cacheKey',
        'cacheTags',
        'create',
        'delete',
        'restore',
        'update',
        'updateOrCreate',
    ],
];

用法

准备您的模型

您希望缓存的每个模型都应该实现 CacheableModel 接口并使用 CacheableTrait。该特性将为您的模型添加额外的功能,并观察模型以发现任何未来的变化。这样,当观察到的模型被更新时,包会立即清除与受影响模型相关的缓存。

namespace App;

use Illuminate\Database\Eloquent\Model;
use Suitmedia\Cacheable\Contracts\CacheableModel;
use Suitmedia\Cacheable\Traits\Model\CacheableTrait;

class Article extends Model implements CacheableModel
{
    use CacheableTrait;
}

准备您的仓库

您需要缓存的每个仓库也必须实现 CacheableRepository 接口。您可以通过使用 CacheableTrait 简单地实现该接口,如下所示

namespace App\Repositories;

use App\Article;
use Suitmedia\Cacheable\Traits\Repository\CacheableTrait;
use Suitmedia\Cacheable\Contracts\CacheableRepository;

class ArticleRepository implements CacheableRepository
{
    use CacheableTrait;

    private $article;

    public function __construct(Article $article)
    {
        $this->article = $article;
    }

    public function model()
    {
        return $this->article;
    }

    /*
    |--------------------------------------------------------------------------
    | Repository's method definition starts from here
    |--------------------------------------------------------------------------
    */

    public function all()
    {
        return $this->model->all();
    }

    public function find($id)
    {
        return $this->model->find($videoId);
    }
}

从仓库检索数据并缓存结果

使用此包,您无需为每个仓库创建新类来装饰。您只需使用 Cacheable 门面来装饰它们,然后仓库方法的全部结果将被自动缓存。

// Lets decorate the repository first
$repo = Cacheable::wrap(ArticleRepository::class);

// The result of these codes will be cached automatically
$repo->all();
$repo->find(1);

缓存失效

此包将帮助您在 CacheableModel 更新时自动使缓存无效。它将根据在模型中定义的缓存标签使缓存无效。

但是,您也可以使用 Cacheable 门面手动使缓存无效。

// Flush everything
Cacheable::flush();

// Flush the cache using a specific tag
Cacheable::flush('Article');

// Flush the cache using a specific tag,
// and only for the cache which belongs to a specific user
Cacheable::flush('LikedArticle:User:7');

许可协议

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