How to store all record in laravel Cache

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

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')
           ->take(10)
           ->remember(60)
           ->get();

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

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

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')
        ->select(DB::raw(
            "SOME COMPLEX JOINS ETC.."
        ))->get();
});

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.

Cache::forget('orderTable');

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.

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 →