tequilarapido / result-cache
A simple and decoupled way to store resource consuming operations results in cache.
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-10-24 05:34:55 UTC
README
A simple and decoupled way to store resource consuming operations results in cache.
Contents
Installation
You can install the package using composer
$ composer require tequilarapido/result-cache
Usage
Cache
- Create a class that extends 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; } }
- Now you can simply call the get method to fetch cache. If the cache is invalid or not yet created, the operations will be executed.
class SomeController { public function books() { return (new BooksCache)->get(); } }
- Clean the cache
The package uses the default cache driver defined in your laravel application.
You can clean the cache using the artisan cache:clear
You can also clean the cache programmatically using :
(new BooksCache)->forget()
Application locale aware cache
Sometimes we need to cache something, but we need multiple versions depending on the application locale.
For this kind of use case we need to extend the LocaleAwareResultCache
, and define the locales that are available in our application.
- Create a class that extends 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'] } }
- Now you can simply call the get method to fetch cache. If the cache is invalid or not yet created, the operations will be executed.
class SomeController { public function books() { return (new BooksCache)->setLocale($locale)->get(); } }
- Clean the cache
The package uses the default cache driver defined in your laravel application.
You can clean the cache using the artisan cache:clear
You can also clean the cache programmatically using :
(new BooksCache)->forget()
this will clean cache for all locales.
Cache expiration
By default, cache is created for one day. You can override the protected $minutes
property on
your cache class to specify how much minutes you want your cache before it gets invalidated.
use Tequilarapido\ResultCache\ResultCache; class BooksCache extends ResultCache { protected $minutes = 60; // One hour // ... }
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Security
If you discover any security related issues, please email :author_email instead of using the issue tracker.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.