farzinft/laravel-model-caching

此包的最新版本(dev-master)没有提供许可证信息。

缓存 Laravel Eloquent 查询结果的简单方法

dev-master 2020-01-07 13:29 UTC

This package is auto-updated.

Last update: 2024-09-12 06:34:37 UTC


README

适用于 Laravel 5.x.x

这是一个用于缓存 Eloquent 查询结果的包。

安装

在 composer.json 中要求此包,并运行 composer update(或直接运行 composer require farzinft/laravel-model-caching:dev-master

"farzin/laravel-model-caching": "dev-master"

您需要发布此包的配置文件。这将添加 config/model-caching.php 文件,您可以在此处配置此包。

$ php artisan vendor:publish --provider="Fthi\ModelCaching\ServiceProvider"

使用方法

您只需将模型添加到模型缓存构造函数中,并以正常方式编写 Eloquent 查询。此包使用 Redis 进行结果缓存;

示例

假设您有一个客户模型,您编写了如下查询

未缓存

 "$customers = Customer::with('books')->where(['status' => 1])->get()"

您想缓存结果 50 秒,请在 model-caching.php 中更改 cache_expire_time(以秒为单位),或在您的模型中定义一个名为 $cacheExpireTime 的公共变量,然后

已缓存

"$customers = (new ModelCaching(new Customer))->with('books')->where(['status' => 1])->get()"

当缓存时间过期时,结果将从数据库中返回。

仅在 laravel 5.x 上进行了测试,可能适用于其他版本,请进行测试 :)