DEV Community

Robin  Dev
Robin Dev

Posted on • Originally published at larachamp.com on

How to use Cache In Laravel Query – Larachamp

what Is Cache In Laravel
cache In laravel

Hello devs, Today we are going to learn about Cache in Laravel and I’ll also be discussing how you can use Cache while making a query to the database. Usually, I would say cache is my favorite topic because it helps on the very main point when it comes to speeding up your query result on the browser.

Each time while executing Queries and performing tasks, It takes seconds or microseconds to return the result. But when It comes to caching the result, It helps to retrieve the result quickly.

What Is Cache In Laravel

The Cache is something that we use to store data for a particular time and also it can be forever. We use Cache in Laravel so that we can access the result faster as much as possible. It saves data from the server (when we do use cache). We don’t need to make requests for the same data on the server and also don’t need to wait for seconds to get the result.

Why We Use Cache In Laravel

This is the main question that everyone should ask who is new to using cache. Why do we need to use cache? and the answer is simple. Cache stores all the data locally and it helps to improve loading speed and efficiency. When users do visit any site which loads data very slow, the simple solution is there you can use cache and it will pre-download all the data and later it will retrieve it from local storage which improves user experience.

Cache Configuration In Laravel

You can find the cache file in your config directory config/cache.php. Laravel provides different types of cache drivers and you can use any of them as your cache driver.

Laravel supports popular caching backends like Memcached, Redis, DynamoDB, etc.

You will have a detailed overview of the drivers in the cache.php. The default, stores Cache in the file. That you find under your storage directory. storage\framework\cache\data.

Let’s Use Cache

Let’s see how we can use Cache in Laravel with some practical examples.

Store Item In The Cache

You can easily store any item in the Cache having said that you have to use Cache Facade at the top of your file.

Cache::put('key', 'value', $seconds = 10);
Enter fullscreen mode Exit fullscreen mode

if we would not pass any specific time in the put method then the cache will store for an indefinite time.

Store Item and Check if not exist

Cache::add('key', 'value', $seconds);

//add method will do this, If the item will be stored then it will return true otherwise false
Enter fullscreen mode Exit fullscreen mode

If you do want to store the item forever then you can use the forever method for this thing.

Cache::forever('key', 'value');
Enter fullscreen mode Exit fullscreen mode

Remove Items from Cache

You can remove items from the Cache by using the forget method.

Cache::forget('key');
// here you have to pass the key, which you want to remove.
Enter fullscreen mode Exit fullscreen mode

If you want to remove the cache for all items then you can use the flush() method.

Cache::flush();.
Enter fullscreen mode Exit fullscreen mode

Tags Cache In Laravel

Cache tags don’t work with all drivers, It only works with Memcached so, I would prefer If you do want to know about it you can read it on the official documentation.

Practical Example Of Using Cache In Laravel Query Result

We have a user table and we want to fetch all the users. We will write a query to fetch the users and then we will get all the users through Cache and then If we will hit the request again to get the users list then we will check if we would have that data cached or not.

This is how this process will work. Let’s see the example. And If you will have any query let me know in the comment box.

I am into my controller and I’ll have the show function which will do all the tasks.

 public function show()
    {
        $parentId = Auth::User()->id;
        $ClientsData = Cache::remember('clients', 60 * 60, function () use ($parentId) {
            return User::where('parent_id', $parentId)->orderByDesc('id')->with('file')->get();
        });
        return view('usersListing', ['clients' => $ClientsData]);
    }
Enter fullscreen mode Exit fullscreen mode

Let’s understand the process of function.

When we use the Cache::remember function that means we are checking if clients the named cache exists or not. If not then fetch from the database.

If you are using CRUD operation with the listing then don’t forget to use the Observer to forget the Cache. The below video will help you to understand it more deeply.

Learn about Observers

Thanks For Watching and Reading. I hope It will help you somewhere in your developer journey. Look at Laravel Tips Page, Maybe you will find something useful.

The post How to use Cache In Laravel Query – Larachamp first appeared on larachamp.com.

The post How to use Cache In Laravel Query – Larachamp appeared first on larachamp.com.

Top comments (0)