How to Cache Query using Laravel Cache?

By Hardik Savani April 16, 2024 Category : Laravel

Hey Developer,

Here, I will show you how to work how to cache query using laravel cache. It's a simple example of laravel cache query example. you'll learn laravel cache remember forever. We will use laravel cache query results.

In this article, i will show you how to cache query result in laravel. laravel provides Cache facade to cache data in laravel. we can use put(), get(), remember(), rememberForever(), forget() and flush() method of Cache facade to cache data. i will give you following examples that will help you how to cache query using laravel cache.

1. Laravel Cache Query Result using put() and get()

2. Laravel Cache Query Result using remember()

3. Laravel Cache Query Result using rememberForever()

4. Laravel Cache Query Result using rememberForever() with dynamic way

5. Laravel Cache Clear using forget() and flush()

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

before goes to all examples, you can set cache drivers on .env file. by default it will be file as cache driver. you can change and set redis, file, database etc.

.env

CACHE_DRIVER=file

1. Laravel Cache Query Result using put() and get()

here, we will simply store data into cache variable using put() method and fetch data using get method as like the following example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

use Illuminate\Support\Facades\Cache;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$seconds = 60;

$posts = Post::get();

Cache::put('posts', $posts, $seconds);

return view('posts', compact('posts'));

}

/**

* Write code on Method

*

* @return response()

*/

public function getPosts($id)

{

$posts = Cache::get('posts');

dd($posts);

}

}

2. Laravel Cache Query Result using remember()

In Laravel, the remember() method is used to cache the result of an expensive operation or query for a specified amount of time. This can be very useful to reduce the load on your server and speed up the response time for frequently accessed data.

Here's how you can use the remember() method in Laravel:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

use Illuminate\Support\Facades\Cache;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$seconds = 60;

$posts = Cache::remember('posts', $seconds, function () {

return Post::get();

});

return view('posts', compact('posts'));

}

}

3. Laravel Cache Query Result using rememberForever()

In Laravel, the rememberForever() method is used to cache the result of an operation indefinitely, meaning the cached data will not expire unless you manually remove it from the cache or clear the entire cache.

Here's how you can use the rememberForever() method in Laravel:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

use Illuminate\Support\Facades\Cache;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Cache::rememberForever('posts', function () {

return Post::get();

});

return view('posts', compact('posts'));

}

}

4. Laravel Cache Query Result using rememberForever() with dynamic way

Here, i will show you how to use dynamic way cache query using rememberForever() function. we will cache each post using id as key. so, let's see the example

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

use Illuminate\Support\Facades\Cache;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function show($id)

{

$post = Cache::remember('post-'.$id, function ($id) {

return Post::find($id);

});

return view('show', compact('post'));

}

}

5. Laravel Cache Clear using forget() and flush()

In Laravel, you can clear cached data using the forget() method to remove specific items from the cache and the flush() method to clear all cached data.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

use Illuminate\Support\Facades\Cache;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function remove()

{

/* Remove a specific item from the cache */

Cache::forget('cache-key');

/* Clear all cached data */

Cache::flush();

}

}

I hope it can help you...

Shares