♥ Made with <love/> And I love <code/>
A super cool package for caching the laravel response dynamically.
You can install the package via composer:
composer require theriddleofenigma/laravel-rache
Add the service provider and alias in the bootstrap/app.php.
// Service provider
$app->register(\Rache\RacheServiceProvider::class);
// Alias
$app->alias('Rache', \Rache\Facades\Rache::class);
For laravel, the service provider and aliases will be loaded automatically by the Package Discovery.
Publish the config file by running the following artisan command. It will publish the rache config file under config/rache.php.
php artisan rache:publish
You can either declare the default middleware, or you extend it by creating your own middleware for customisation.
'rache' => \Rache\Middleware\CacheResponse::class,
Make sure to set the rache driver in the .env file with the corresponding driver name which you want to use.
RACHE_DRIVER=redis // It uses laravel cache system behind the scenes.
Since rache uses Laravel Cache Tags behind the scenes it contains the same limitations as Laravel Cache tags. Cache tags
aren't supported when using the file
, dynamodb
, or database
cache drivers.
Rache tags acts as label for settings up the cache against some data. Auth
, Request
, and Pagination
tags are added
by default. You can find them in the rache.php config file under tags key.
You can create a Rache tag using the following artisan commands.
php artisan make:rache-tag {tag-name}
or
php artisan rache:make-tag {tag-name}
Once you have created the rache tag successfully, then you can configure the dataset in the newly created file
under getTagDetails()
method. The array data returned from the getTagDetails()
method should contain the key-value
pairs of unique dataset, and it will bring the cache dynamic on using with the rache middleware.
Let's add a Rache Tag for search,
Run php artisan make:rache-tag Search
in the project base directory.
Then add the unique constraints for the search tag under getTagDetails()
,
/**
* Get the tag details of this rache tag.
*
* @return array
*/
public function getTagDetails(): array
{
return [
'search' => $this->request->input('search'),
];
}
After that you should define the newly created tag in the rache.php config file as follows,
/*
* You may declare the tags here. All responses will be tagged.
* These tags are very used while forgetting the response cache.
*/
'tags' => [
'auth' => \Rache\Tags\Auth::class, // Added by default
'page' => \Rache\Tags\Pagination::class, // Added by default
'request' => \Rache\Tags\Request::class, // Added by default
// Custom tags
'search' => \App\Rache\Tags\Search::class, // Newly added
],
You can add any number of tags based on the no. of unique constraints they for the route by which the cache will be considered as new.
Use the middleware along with tags. You can enter the ttl as a tag for settings a different lifetime than the lifetime configured in the rache.php config file. You can pass the ttl in any position along with the other tags.
- The lifetime ttl entered are in seconds here.
- You must have declared the route name in order to rache middleware against a route.
- You can use the middleware without tags like
->middleware('rache')
. It will cache the response without considering any tag data. It will be useful if a route response won't vary for anyone.
// Both acts as same.
rache:ttl_10,auth,page,search
rache:auth,ttl_10,search,page
Route::get('/posts', 'PostController@index')
->middleware('rache:ttl_10,auth,page,search')
->name('posts.index');
By calling the flushAll, you can clear all the cached response in the application.
Rache::flushAll();
You can flush the cache by using Rache::flushTag({tag-name}, {options:[route, data]})
. We can find some real-time
examples for flushing the tags.
Let's say you want to clear all the cache based on the auth tag,
Rache::flushTag('auth', [
'route' => 'posts.index',
]);
If the route name is not mentioned, then the cache for all the routes having the auth tag will get cleared.
If you want to clear for the current authenticated user then,
Rache::flushTag('auth', [
'route' => 'posts.index',
'data' => Rache::getTagData('auth'),
]);
The Rache::getTagData()
will render the data as same as it's get rendered for creating the cache.
In case you want to clear the data for other users,
$userId = 2;
Rache::flushTag('auth', [
'route' => 'posts.index',
'data' => Rache::getTagInstance('auth')->getTagDetails($userId),
]);
If the route name is not mentioned, then the cache for all the routes having the auth tag will get cleared. Here, the auth tag for userId 2 will get cleared without touching the cache of other userId's.
You can flush any type of tag along with route name or data based on your need. Ex: On creating new record or delete event or on custom event or an API trigger. You can use it anywhere, whenever a tag has been flushed it will clear all the corresponding cache.
Copyright © Kumaravel
Laravel Rache is open-sourced software licensed under the MIT license.