How to store all record in laravel Cache?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Here we know how to store all record in laravel Cache. When we are work with any large application which is depend on large query and also required large number of record at that time we need our application performance littel bit down. So that time need to overcome problem with laravel Cache functionality.

Laravel Cache provide to us store our large query in cache so we can increase our application performance.Let’s see how to store all record in laravel Cache

First of all we must write “use Cache” keyword on top of the our class.

Here is an a good example of using Laravel’s Fluent Query Builder:

$users = DB::table('order')
           ->orderBy('price', 'desc')
           ->take(10)
           ->remember(60)
           ->get();

Of course, we can also do the same thing by using laravel Eloquent:

$users = Order::orderBy('price', 'desc')
             ->take(10)
             ->remember(60)
             ->get();

But, it’s work fine with a very small and simple database query.

When  we have to work with more complex database query then we need to cache data by use this way.

1.)Cache all records.

$orderTable = Cache::remember('orderTable', 60, function()
{
    return  DB::table('order')
        ->select(DB::raw(
            "SOME COMPLEX JOINS ETC.."
        ))->get();
});

Here the ‘orderTable’ is a cache variable so we can use this ,when we get all records from the cache like that,

Read Also : How to check user login or not in Laravel?

2.)Get records from Cache

$data = Cache::get('orderTable');

3.)Also remove Cache records from our Cache memory. It must be required when we insert new record otherwise we want to update any records.

Cache::forget('orderTable');

Thanks for read this, I hope it will you. You can also follow us on Facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →