Today I have released my new Laravel package Laravel Database Cache
https://github.com/eusonlito/laravel-database-cache
This package allow to cache database queries from Models without pain.
You can configure the cache to use tags (on redis
) and flush all database caches at once: https://github.com/eusonlito/laravel-database-cache#flush-caches
You can install the package via composer:
composer require eusonlito/laravel-database-cache
Publish the default configuration in config/database-cache.php
:
php artisan vendor:publish --tag=eusonlito-database-cache
Add this lines to .env
file:
DATABASE_CACHE_ENABLED=true
DATABASE_CACHE_DRIVER=redis
DATABASE_CACHE_TTL=3600
DATABASE_CACHE_TAG=database
DATABASE_CACHE_PREFIX=database|
And configure your models to use the cache
method with:
<?php declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Eusonlito\DatabaseCache\CacheBuilderTrait;
class User extends Model
{
use CacheBuilderTrait;
Now, the simple way to use is:
$articles = Article::latest('published_at')->take(10)->cache()->get();
The results will be cached every time you will call this query.
Also you can set a custom cache time for this query:
$articles = Article::latest('published_at')->take(10)->cache(60)->get();
Or a custom cache name:
$articles = Article::latest('published_at')->take(10)->cache(60, 'latest_articles')->get();
You have more info at package https://github.com/eusonlito/laravel-database-cache
Enjoy!
Top comments (0)