Laravel Eloquent Query Cache is a package that provides caching for Eloquent queries.
To install this package, run the following command:
composer require renoki-co/laravel-eloquent-query-cache
To use this package, you can simply add the Cacheable
trait to your Eloquent models:
use RenokiCo\EloquentQueryCache\Cacheable;
class User extends Model {
use Cacheable;
}
This will cache all queries performed on the User
model for 60 seconds by default. You can customize the cache duration by defining a $cacheLifetime
property on your model:
use RenokiCo\EloquentQueryCache\Cacheable;
class User extends Model {
use Cacheable;
protected $cacheLifetime = 300; // Cache queries for 5 minutes.
}
You can also cache specific queries by using the remember
method:
$users = User::remember(120)->get();
This will cache the get()
query for 120 seconds.
To publish the configuration file, run the following command:
php artisan vendor:publish --provider="RenokiCo\EloquentQueryCache\CacheServiceProvider" --tag="config"
This will create a eloquent-query-cache.php
file in your config
directory. You can use this file to customize the package configuration.