mtgofa/laravel-query-cache

Laravel 数据库缓存包和缺失关系事件。

v1.0.3 2022-10-06 09:37 UTC

This package is auto-updated.

Last update: 2024-09-06 14:08:05 UTC


README

本包基于 2 个包

- whthT/perfectly-cache

- chelout/laravel-relationship-events

它是缓存包和关系事件合并的结果,如果关系发生变化则抛出缓存。

特别感谢

PerfectlyCache

Laravel Eloquent 查询缓存包。

它用于缓存和使用你做出的任何查询,无需对数据库、系统或查询进行任何更改。

PerfectlyCache 会自动将相同的查询通过缓存模型的结果重定向到模型。

安装

  • Composer 执行以下命令以获取包的最新版本:
composer require mtgofa/laravel-query-cache
  • 发布配置
php artisan vendor:publish --provider="MTGofa\QueryCache\Providers\QueryCacheServiceProvider"
  • 在模型上使用特质
    在你的模型中添加以下代码 / 或添加你的基模型如下:
<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

use MTGofa\QueryCache\Traits\QueryCachable;

class User extends Model
{
    use QueryCachable;
}

你通过此模型做出的所有数据库查询都将被缓存,并在需要时从缓存中读取,而不是从数据库中读取。

配置

// config('perfectly-cache.(name)')
// Eq: config('perfectly-cache.enabled')
return [

    "enabled" => true, // Is cache enabled? cache for production --> !env('APP_DEBUG', false)

    "minutes" => 1, // Cache minutes.

    /**
     * If this event is triggered on this model,
     * the cache of that table is deleted.
     */
    "clear_events" => [
        "created",
        "updated",
        "deleted",
        "restored"
    ],
];

使用方法

// Basic cache
$results = \App\Category::find($id);

// Basic cache skip
$results = \App\Category::skipCache()->find($id);

// Basic usage with eager load
$results = \App\Category::with("_list_category_tags")->find($id);

// Basic cache skip usage with eager load
$results = \App\Category::with("^_list_category_tags")->find($id);

缓存跳过

  • 通过链式调用
    // ->skipCache();
    $result = Category::select("id", "name")->skipCache()->get();
  • 通过预加载
    /**
    * Thanks to the ^ sign, you can prevent your relationships from being cached.
    */
    $results = Category::select("id", "name")->with([
        "^_list_category_tags:id,category_id,name,slug"
    ])->find($id);
    
    /**
    * It will no longer be hidden in the cache for the tag table of the categories.
    */
   
  • 在模型中跳过

    使用 $isCacheEnable 变量管理你的模型。

<?php
namespace App;

class Category extends BaseModel
{
    /* Cache disabled by this variable */
    protected $isCacheEnable = false;
}

缓存时间调整

你可以在配置文件中设置缓存时间(perfectly-cache.minutes
你可以在全局范围内从模型或直接在查询期间指定,你可以通过编辑设置来应用所有模型。缓存时间可以在查询、模型和设置中进行编辑。

  • 在配置文件中
...
"minutes" => 30,

  • 在模型中 $cacheMinutes
<?php

namespace App;


class Module extends BaseModel
{
    protected $table = "modules";

    protected $cacheMinutes = 20; // Now cache time 20 minutes.
}
  • 在查询中 ->remember(:minutes)
$modules = \App\Module::remember(10)->select("id", "name")->get();

此查询将被缓存 10 分钟。

  • 在预加载中
$modules = \App\Module::with([
    "(10)categories:id,name,module_id"
])->select("id", "name")->get();
// Categories will be cached for 10 minutes.

程序化缓存重新加载

如果你想逻辑上刷新查询,可以使用 ->reloadCache() 如下。

$module = Module::select("id", "name", "need_cache_reload")->first();
if($module->need_cache_reload) { // simple true value
    $module->reloadCache();
}

命令

# Clear all caches.
php artisan perfectly-cache:clear

#Clear all users table caches
php artisan perfectly-cache:clear users

#Clear all users and modules tables caches
php artisan perfectly-cache:clear users modules
# Infinity table names

#Show cache details
php artisan perfectly-cache:list

注意

如果你已经在查询上使用了时间并且此查询将被缓存,如下所示:

$modules2 = Module::select("id", "name")
    ->where("created_time_unix", ">=", time())
    ->get();

你需要在此次查询上添加 ->skipCache() 方法。
因为:每次运行此查询时都会创建不同的缓存。

许可证

本项目采用 MIT 许可证 - 有关详细信息,请参阅 LICENSE 文件