tequilarapido / result-cache
将消耗资源的操作结果存储在缓存中的简单且解耦的方式。
1.0.0
2018-09-17 10:19 UTC
Requires
- laravel/framework: 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.*|| 5.6.*|| 5.7.*
Requires (Dev)
- mockery/mockery: ^0.9.5
- orchestra/testbench: ^3.2
- phpunit/phpunit: 4.*
This package is auto-updated.
Last update: 2024-09-24 05:17:02 UTC
README
将消耗资源的操作结果存储在缓存中的简单且解耦的方式。
内容
安装
您可以使用composer安装此包
$ composer require tequilarapido/result-cache
使用
缓存
- 创建一个扩展ResultCache的类
use Tequilarapido\ResultCache\ResultCache; class BooksCache extends ResultCache { public function key() { return 'app.books.all'; } public function data() { // Some heavy / resources consuming operations // ... return $books; } }
- 现在您只需简单地调用get方法来获取缓存。如果缓存无效或尚未创建,则将执行操作。
class SomeController { public function books() { return (new BooksCache)->get(); } }
- 清除缓存
该包使用您在Laravel应用程序中定义的默认缓存驱动程序。
您可以使用artisan cache:clear
来清除缓存
您也可以使用以下方式程序化地清除缓存
(new BooksCache)->forget()
应用程序区域感知缓存
有时我们需要缓存一些内容,但需要根据应用程序的区域提供多个版本。对于这类用例,我们需要扩展LocaleAwareResultCache
,并定义应用程序中可用的区域。
- 创建一个扩展LocaleAwareResultCache的类
use Tequilarapido\ResultCache\LocaleAwareResultCache; class BooksCache extends LocaleAwareResultCache { public function key() { return 'app.books.all'; } public function data() { // Some heavy / resources consuming operations // We have access to $this->locale here to customize results according to locale // ... return $books; } public function supportedLocales() { return ['en', 'fr', 'ar'] } }
- 现在您只需简单地调用get方法来获取缓存。如果缓存无效或尚未创建,则将执行操作。
class SomeController { public function books() { return (new BooksCache)->setLocale($locale)->get(); } }
- 清除缓存
该包使用您在Laravel应用程序中定义的默认缓存驱动程序。
您可以使用artisan cache:clear
来清除缓存
您也可以使用以下方式程序化地清除缓存
(new BooksCache)->forget()
这将清除所有区域的缓存。
缓存过期
默认情况下,缓存有效期为一天。您可以在您的缓存类中重写受保护的$minutes
属性来指定缓存失效前的分钟数。
use Tequilarapido\ResultCache\ResultCache; class BooksCache extends ResultCache { protected $minutes = 60; // One hour // ... }
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
测试
$ composer test
安全
如果您发现任何安全问题,请通过:author_email发送电子邮件,而不是使用问题跟踪器。
贡献
有关详细信息,请参阅CONTRIBUTING。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。