neurony / laravel-query-cache
Requires
- php: ^7.2.5
- illuminate/contracts: ^7.0
- illuminate/database: ^7.0
- illuminate/support: ^7.0
Requires (Dev)
- ext-redis: *
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^8.5
README
遗憾的是,此包现已停止维护。
请查看 Varbox(Laravel 管理面板)以获取此功能及其他更多功能。
- 购买: https://varbox.io/buy
- 文档: https://varbox.io/docs
- 演示: https://demo.varbox.test/admin
- 仓库 https://github.com/VarboxInternational/varbox
谢谢!
缓存所有 "select" 查询或仅缓存特定 Eloquent 模型的重复查询
概述
此包允许您缓存所有类型为 select
的查询,或者仅缓存 Eloquent 模型的重复查询。
请注意,由于使用了缓存标签,"文件"或"数据库"缓存驱动程序与此包不兼容。
兼容的缓存存储:数组、Redis、APC、Memcached
已测试的缓存存储:数组、Redis
安装
通过 Composer 安装此包
composer require neurony/laravel-query-cache
使用以下命令发布配置文件
php artisan vendor:publish --provider="Neurony\QueryCache\ServiceProvider" --tag="config"
请阅读
config/query-cache.php
配置文件的注释,因为它包含额外信息
使用
步骤 1
您的 Eloquent 模型应该使用 Neurony\QueryCache\Traits\IsCacheable
特性。
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Neurony\QueryCache\Traits\IsCacheable; class YourModel extends Model { use IsCacheable; }
步骤 2
在您的 .env
文件中添加必要的环境变量
# The driver used for storing the cache keys that this package generates.
# This driver can differ from your main Laravel's CACHE_DRIVER.
QUERY_CACHE_DRIVER=redis
# Wether to cache absolutely all queries for the current request.
CACHE_ALL_QUERIES=true
# Wether to cache only the duplicated queries for the current request.
CACHE_DUPLICATE_QUERIES=true
根据您如何设置环境变量,下一次您在该 Eloquent 模型上执行 select
查询时,在第一次运行之后,查询将被缓存。
额外内容
使用 QueryCacheService
类
请注意,Neurony\QueryCache\Services\QueryCacheService
类是 Neurony\QueryCache\Contracts\QueryCacheServiceContract
接口的实际实现。
Neurony\QueryCache\Services\QueryCacheService
类绑定到 Laravel 的 IoC 作为单例,并别名为 cache.query
。
有此说法,直接使用 Neurony\QueryCache\Services\QueryCacheService
的推荐方式是
app('cache.query'); // or app(QueryCacheServiceContract::class);
启用/禁用查询缓存
您可以通过调用 Neurony\QueryCache\Services\QueryCacheService
类上的 enableQueryCache
或 disableQueryCache
方法来启用或禁用当前请求的所有查询缓存。
// from her on, no queries will be cached app('cache.query')->disableQueryCache(); /* make your queries the queries up until now won't be cached */ // from her on, apply query caching app('cache.query')->enableQueryCache(); /* make your queries this queries will be cached */
鸣谢
安全
如果您发现任何与安全相关的问题,请通过电子邮件andrei.badea@neurony.ro联系,而不是使用问题跟踪器。
许可
MIT 许可证 (MIT)。请参阅LICENSE以获取更多信息。
更新日志
请参阅CHANGELOG以获取最近更改的详细信息。
贡献
请参阅CONTRIBUTING以获取详细信息。