Click Below to subscribe

How To Use Laravel Cache for Query Caching 2020.

Laravel provides an expressive, unified API for various caching backends. We can easily configure caching in Laravel.

Laravel provides two drivers for caching.

a). Database

b). File

In this tutorial, we are using file cache driver. file cache driver is enabled by default. you can check the CACHE_DRIVER in your .env file.

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

use Cache

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Cache;

# Store Data

1. If we want to cache our query data for a limited time then we can use Cache::put() method.

$users = User::get();
Cache::put('users', $users, $seconds);

2. if you want to store your data forever then you can use Cache::forever() or Cache::put() method without time.

$users = User::get();
Cache::forever('users', $users);

Cache::put('users', $users);

# Retrieve Data

1. we can retrieve the data from the cache using Cache::get() method.

$result = Cache::get('users');

2. we can also pass a default value if the users key is not present in the cache.

$result = Cache::get('users', $dummyUsers);

# Other Way (Recommended)

In the above examples, there is a chance maybe the cache key is missing on the cache. in that case, you have no data in the result variable. so in that case we can use remember methods that have a fallback if the key that is we are trying to retrieve is null.

1. If you want to store your data for a limited time in the cache then you can use remember() method.

$result = Cache::remember('users', $seconds, function () { 
    return DB::table('users')->get(); // By Using DB
});


$result = Cache::remember('users', $seconds, function () {
    return User::get();   // By Using Eloquent
});

As in the above example, you can see the three arguments taken by the remember() method.

If the key does not exist in the cache, the closure passed to the remember method will be executed and its result will be placed in the cache.

2. If you want to store your data forever in the cache you can use rememberForever() method.

$result = Cache::rememberForever('users', function () {
    return DB::table('users')->get();
});

$result = Cache::rememberForever('users', function () {
    return User::get();
});

As you can see above method doesn't take time argument.

3.  If you want to make dynamic key names or you need to pass the argument in closure.

suppose $id = 1

$result = Cache::rememberForever('user'.$id, function ()  use ($id) {
    return DB::table('users')->where('id', $id)->get();
});

$result = Cache::rememberForever('user'.$id, function () use ($id){
    return User::where('id', $id)->get();
});

the result contains the data for user1.

# Remove Data

1.  if we need to remove data from the cache then we can use Cache::forgot() method.

Cache::forget('key');

2. If we need to remove all cache data then we can use Cache::flush() method.

Cache::flush();

Leave Your Comment