Wednesday, March 4, 2020

laravel model Cache clear tutorials


laravel model Cache clear tutorials

In this tutorial we will discuss how you can improve performance in Laravel 6 model cache clear tutorials.We can improve performance by caching eloquent models through the Git Composer package from Genealabs / Laravel model cache clear tutorials.

As we know, website performance is very important to the website owner. If your website works smoothly, the traffic will be higher and if you have used an eloquent cache it will no longer be loaded on the server. In this post, I'm going to show you how to set caching mode and increase the performance of your Laravel application.

There are several ways you can improve the performance of the Laravel website. As you know, we almost wanted to cache with htaccess and cache images, CSS, JS and HTML files. Caching CSS, JS, HTML improves the performance of the website

But if you have to search a lot of records in a database and lots of tables. When you open the link, more than one query is always executed on your website. Restoring data from the database takes time, since the same process is always loaded when the page is loaded, so that it is always loaded every time. If you have more data and visitors to enter our website over a longer period of time, your server will be damaged. However, if you use the Laravel cache, you can save your server and improve the performance of the page. Here we use the genealabs / laravel-model-caching package to cache models.

The Laravel cache is a very simple and easy way. Cache optimizes the page speed of your website. So let's follow the next step and see how it works. We will also use the barryvdh / laravel-debugbar package. This package helps you debug how many queries are enabled on your page so you can see this. So just follow the next step.

Step 1: Install Laravel 6 App


We're going to start over, so we need to get a new Laravel 6 application with the following command. So open your terminal or command prompt and run the following command:

composer create-project --prefer-dist laravel/laravel blog


Step 2: Installation Of barryvdh/laravel-debugbar Package


Now we install the composer package barryvdh / laravel-debugbar with the following command in our Laravel 6 application. So run the following command.

composer require barryvdh/laravel-debugbar --dev

Ok, after successfully installing the package, we add the service provider in the configuration file app.php. So let's add below:

config/app.php
<?php



return [

 ....

 'providers' => [

  ....

  Barryvdh\Debugbar\ServiceProvider::class,

 ],

 'aliases' => [

  ....

  'Debugbar' => Barryvdh\Debugbar\Facade::class,

 ]

]


You can then publish configuration files using the following command:

php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"


Step 3: Installation Of genealabs/laravel-model-caching Package


Now we install the Composer package genealabs / laravel-model-caching with the following command in our Laravel 6 application. So run the following command.

composer require genealabs/laravel-model-caching


Step 4: Update User Model


In this step we use the GeneaLabs package class in the user model. Therefore, you need to update the user model as follows:

app/User.php
<?php



namespace App;



use Illuminate\Notifications\Notifiable;

use Illuminate\Foundation\Auth\User as Authenticatable;

use GeneaLabs\LaravelModelCaching\Traits\Cachable;



class User extends Authenticatable

{

    use Cachable;

    use Notifiable;



    /**

     * The attributes that are mass assignable.

     *

     * @var array

     */

    protected $fillable = [

        'name', 'email', 'password',

    ];



    /**

     * The attributes that should be hidden for arrays.

     *

     * @var array

     */

    protected $hidden = [

        'password', 'remember_token',

    ];

}


Step 5: Create Dummy Users


Here we are going to create some fictional user records with Factory. So you can run the following command to create dummy users in your database.

php artisan tinker

factory(App\User::class, 100)->create();


Step 6: Create Route


A route is now created to show users in sight. So let's create a route as shown below:

routes/web.php
Route::get('users', 'UserController@index');


Step 7: Create Controller


We have to create a new controller like UserController and then create index () in the controller. We receive all users using the user model and print on request:

app/Http/Controllers/UserController.php
<?php



namespace App\Http\Controllers;



use Illuminate\Http\Request;

use App\User;



class UserController extends Controller

{

 

 /**

     * The application's index

     *

     * @var array

     */

    public function index()

    {

     $users = User::where('id', 1)->get();

     $users = User::where('id', 5)->get();

     $users = User::get();



     return view('users',compact('users'));

    }

}


Step 8: Create Blade File


Now we will finally create the file users.blade.php and write the code of the user lists. So let's create a blade file:

resources/views/users.blade.php
<!DOCTYPE html>

<html>

<head>

 <title>User List </title>

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">

</head>

<body>



<div class="container">

 <h1>User List- Myitbuddies.com</h1>

 <table class="table table-bordered">

  <tr>

   <th>Id</th>

   <th>name</th>

   <th>email</th>

  </tr>

  @foreach($users as $user)

  <tr>

   <td>{{ $user->id }}</td>

   <td>{{ $user->name }}</td>

   <td>{{ $user->email }}</td>

  </tr>

  @endforeach

 </table>

</div>



</body>

</html>


Now you can run the project. Let's check this out.

When you open the first page, three queries are loaded as follows:

Then update and check again that the query is no longer running on the server:

You can clear the cache and check again:

php artisan cache:clear


I hope this Laravel model cache clear tutorials could help you ...

Thanks.



0 comments:

Post a Comment

Please don't enter any spam link in the comment box.