How to store all records in laravel 5.3 Cache – onlinecode
In this post we will give you information about How to store all records in laravel 5.3 Cache – onlinecode. Hear we will give you detail about How to store all records in laravel 5.3 Cache – onlinecodeAnd how to use it also give you demo for it if it is necessary.
How to store all records in laravel 5.3 Cache
In this tutorials, i am going to tell you how to work with cache in Laravel.
You can easily save database record in cache and easily you can get that record from cache in Laravel.
Cache is knows as persistence layer between database and application and it is used to improve website performance because sometime database query is going very slow due to lots of traffic at same time on website then retrieving data from cache is much quicker rather than from a database.
Memcached is know as very popular caching system which is used by many high trafficked websites.
Putting an item in the cache
We can easily add or update value in cache by using put()
method and we pass 3 parameters in this method.
Cache::put('key', 'value', 30);
In 3rd parameters we pass expiration time in minutes.
Getting an item from the cache
We can easily get item from cache and also we can check if cache has the key you are looking for.
- if( Cache::has('key')){
- return Cache::get('key');
- }
if( Cache::has( 'key' ) ) { return Cache::get( 'key' ); }
Model::remember()
We use remember()
to cache the database result while querying from database.
$users = User::remember(10)->get();
Sometime you want to retrieve data from cache but requested data does not exist in cache then store default data in cache.
- $allproducts= Cache::remember('allproducts',60,function(){
- return DB::table('products')->get();
- });
$allproducts = Cache::remember('allproducts',60, function() { return DB::table('products')->get(); });
Removing Cache records
You can remove records from cache using forget
method.
Cache::forget('allproducts');
You can clear entire records from cache using flush
method.
Cache::flush();
Don’t forget to include use Cache
keyword on top of class where you will work with cache.
Hope this code and post will helped you for implement How to store all records in laravel 5.3 Cache – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs