Home Laravel How to store all record in laravel Cache

How to store all record in laravel Cache

by Shahriar Sagor

Today now in this blog i will show you how to store all record in laravel cache. If we are work with a large application which depend on large query and also required large number of record at that time our application performance little bit down and slow. On that problem we can overcome with laravel Cache functionality.

On Laravel Cache provide to us to store our large query in cache so our application performance improve more.

So at first of all we must write “use Cache” keyword on top of the your class

Now here is an example by using Laravel’s Fluent Query Builder:

$users = DB::table('order')
           ->orderBy('price', 'desc')

So of course, we can do the same thing by using laravel Eloquent

$users = Order::orderBy('price', 'desc')

But, it’s working fine also with very simple database query

So if we must work with more complex database query we need to use cache this way.

1.)Cache all records.

$orderTable = Cache::remember('orderTable', 60, function()
    return  DB::table('order')
            "SOME COMPLEX JOINS ETC.."

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

2.)Get records from Cache

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

3.) For remove Cache records from the Cache memory. we know it must be required when we insert new record other wise we want to update any records.


Read Also : How to push item to 0 index or first of $scope object in AngularJS?

Thanks for read. I hope it help you. For more you can follow us on facebook.


You may also like