How to store all records in laravel 5.3 Cache – onlinecode

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.

See also  Add Remove input fields dynamically using jQuery demo

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.

  1. if( Cache::has('key')){
  2. return Cache::get('key');
  3. }
if( Cache::has( 'key' ) ) {
  return Cache::get( 'key' );
}


Model::remember()

See also  create pagination using the codeigniter pagination library - onlinecode

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.

  1. $allproducts= Cache::remember('allproducts',60,function(){
  2. return DB::table('products')->get();
  3. });
$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.

See also  Python - can only concatenate str (not float) to str

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

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  3  =  4

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US