Blog Detail

17

Jun
Manage Cache Items Based on Model Queries in Laravel cover image

arrow_back Manage Cache Items Based on Model Queries in Laravel

LaraCache is an awesome package, using this package, you can cache your heavy and most used queries. All you have to do is to define the CacheEntity objects in the model and specify a valid name and TTL for them. LaraCache will handle the rest of the process automatically. It will create and update cache entities based on the TTL that you’ve defined for each entity.

Manually updating the cache entities of models after dispatching model events (creating, updating, and deleting) isn’t required, LaraCache manages them in the background and ensures the most up-to-date version of each cache entity.

Installation

You can install the package via composer:

composer require mostafaznv/laracache

Publish config file:

php artisan vendor:publish --provider="Mostafaznv\LaraCache\LaraCacheServiceProvider"
Done

Usage

Add LaraCache trait to the model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mostafaznv\LaraCache\Traits\LaraCache;

class Article extends Model
{
    use LaraCache;
    
    /**
     * Define Cache Entities Entities
     *
     * @return CacheEntity[]
     */
    public static function cacheEntities(): array
    {
        return [
            CacheEntity::make('list.forever')
                ->cache(function() {
                    return Article::query()->latest()->get();
                }),

            CacheEntity::make('latest')
                ->validForRestOfDay()
                ->cache(function() {
                    return Article::query()->latest()->first();
                })
        ];
    }
}

Retrieve Cache

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


$cache = Article::cache()->get('latest');
// or
$cache = LaraCache::retrieve(Article::class, 'latest');

Disable/Enable Cache

If you want to disable/enable cache, you can do it in the following two ways:

Disable

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->disable();
// or 
LaraCache::disable(Article::class);

Enable

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;


Article::cache()->enable();
// or 
LaraCache::enable(Article::class);

Update Cache Manually

Sometimes you want to update cache entities manually.

Update an Entity

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;

Article::cache()->update('latest');
// or 
LaraCache::update(Article::class, 'latest');

Delete Cache Manually

Sometimes you want to delete cache entities manually. using these methods, you can do it.

Delete an Entity

Using this feature, you can delete cache entities temporarily. after spending TTL, the cache entity will be generated again.

use App\Models\Article;
use Mostafaznv\LaraCache\Facades\LaraCache;

Article::cache()->delete('latest');
// or 
LaraCache::delete(Article::class, 'latest');

Other Available Methods

  • Update all Entities
  • Update all LaraCache Entities
  • Delete an Entity Forever
  • Delete all Model Entities
  • Delete all Model Entities Forever
  • Delete all LaraCache Entities

This package has a lot more features and other methods with code examples, For more details, you can visit its complete documentation and source code on Github

Published at : 17-06-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project