A simple and decoupled way to store resource consuming operations results in cache.
You can install the package using composer
$ composer require tequilarapido/result-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()
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.
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
// ...
}
Please see CHANGELOG for more information what has changed recently.
$ composer test
If you discover any security related issues, please email :author_email instead of using the issue tracker.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.