<?php
namespace App\Models;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable {
// use HasApiTokens, HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable, Cachable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
C:\xampp\htdocs\reset\routes\web.php
<?php
use Illuminate\Support\Facades\Route;
// use App\Http\Controllers\ModelCacheController;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
// Route::controller(ModelCacheController::class)->group(function () {
// Route::get('/', 'index');
// });
// Auth::routes();
// Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('users', [UserController::class,'index']);
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class City extends Model {
use HasFactory;
public $table = 'city';
public $fillable = [
'name',
'desc',
];
public function companies() {
return $this->hasMany(City::class);
}
}
C:\xampp\htdocs\reset\app\Models\Company.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Company extends Model {
use HasFactory;
public $table = 'company';
public function city() {
return $this->belongsTo(City::class);
}
}
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ModelCacheController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::controller(ModelCacheController::class)->group(function () {
Route::get('/', 'index');
});
// Auth::routes();
// Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder {
/**
* Seed the application's database.
*
* @return void
*/
public function run() {
// \App\Models\User::factory(10)->create();
$this->call(DemoSeeder::class);
}
}
Tạo dữ liệu :)
php artisan db:seed
Kết quả:
Sau khi đăng ký sử dụng cache Model con số không thể tin nổi 👍👍)
C:\xampp\htdocs\reset\app\Models\City.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
class City extends Model {
// use HasFactory;
use HasFactory, Cachable;
public $table = 'city';
public $fillable = [
'name',
'desc',
];
public function companies() {
return $this->hasMany(City::class);
}
}
C:\xampp\htdocs\reset\app\Models\Company.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
class Company extends Model {
// use HasFactory;
use HasFactory, Cachable;
public $table = 'company';
public function city() {
return $this->belongsTo(City::class);
}
}
Nhưng nhận thấy khi ta thay đổi bằng tay dữ liệu nó vẫn không thay đổi 😒😒😒
Giờ là lúc chúng ta nghiên cứu cách để xóa cache Model mỗi khi chúng ta cập nhật dữ liệu
How to reduce 1002 MySQL queries in one page to 0 (zero) queries? In this tutorial, I will show how to use laravel model caching. This great library allows to fully cache entire eloquent objects and reduce queries to 0. Self-invalidating feature allows to regenerate data from database on model save, update, create etc..
If You have menu on each page, complex form with many drop-downs populated from database, lot data loaded in background (Language, Date Format, Time Format , Dictionaries etc) Cache it.
1. Create new application
Create a new application with composer. (Note: I have composer installed on Ubuntu, if you are using windows download composer.phar)
cd into project and install DebugBar and laravel-model-caching.
2. Installing required libs
Use artisan composer command to download libs. Don’t forget to publish vendors at the end.
— DebugBar will allow us to see queries to MySQLcomposer require barryvdh/laravel-debugbar –dev— Laravel Model Caching will allow us to cache entire modelscomposer require genealabs/laravel-model-caching
publish vendors
php artisan vendor:publish
3. Creating models
I will create 2 models Company and City. Company will have City as belongTo relation.
We also need to create migrations for database tables.
php artisan make:migration create_entities<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class CreateEntities extends Migration{/*** Run the migrations.** @return void*/public function up(){Schema::create(‘company’, function (Blueprint $table) {$table->increments(‘id’);$table->string(‘name’);$table->string(‘desc’);$table->integer(‘city_id’);$table->timestamps();});Schema::create(‘city’, function (Blueprint $table) {$table->increments(‘id’);$table->string(‘name’);$table->string(‘desc’);$table->timestamps();});}/*** Reverse the migrations.** @return void*/public function down(){Schema::drop(‘company’);Schema::drop(‘city’);}}
5. Creating Seeders
We need some data to run tests
php artian make:seeder DemoSeeder<?phpuse Illuminate\Database\Seeder;class DemoSeeder extends Seeder{/*** Run the database seeds.** @return void*/public function run(){\Illuminate\Database\Eloquent\Model::unguard();\App\Company::truncate();\App\City::truncate();for ($i = 0; $i <= 100; $i++) {$faker = \Faker\Factory::create();$city = new \App\City();$city->name = $faker->city;$city->desc = $faker->sentence();$city->save();}for ($i = 0; $i <= 1000; $i++) {$faker = \Faker\Factory::create();$company = new \App\Company();$company->name = $faker->company;$company->desc = $faker->sentence();$company->city()->associate(rand(1,100));$company->save();}}}
Update DatabaseSeeder.php and add our newly created seeder to default Laravel seeder.
<?phpuse Illuminate\Database\Seeder;class DatabaseSeeder extends Seeder{/*** Run the database seeds.** @return void*/public function run(){$this->call(DemoSeeder::class);}}
Now what will happen when You will load localhost:8000 in the browser ?
What is happening? Because Laravel uses “lazy load” approach page is generating 1002 queries. (Yes, yes You can use “eager load” as I described in the previous article about performance but here I will show what we can do with caching).
8. Updating model
What I did. I added Cachable trait — with this all data from database will be stored in cache. Now if You will run again Your web app You will see 0 database queries.
<?phpnamespace App;use GeneaLabs\LaravelModelCaching\Traits\Cachable;use Illuminate\Database\Eloquent\Model;class City extends Model{use Cachable;public $table = ‘city’;public $fillable = [‘name’,‘desc’];public function companies(){return $this->hasMany(City::class);}}<?phpnamespace App;use GeneaLabs\LaravelModelCaching\Traits\Cachable;use Illuminate\Database\Eloquent\Model;class Company extends Model{use Cachable;public $table = ‘company’;public $fillable = [‘name’,‘desc’];public function city(){return $this->belongsTo(City::class);}}
Now when you will run localhost:8000 again.
Quite amazing, right?
9. Summary
I use this library for all my Laravel project where I need to get data from database and those data are mostly static. For example all dictionaries (Date Format, Time Format, Countries etc).
This library is self-invalidating models this means its refreshing cache when you will create, update, delete record via eloquent.
Laravel Model Caching - Performance Boost Tutorial
By Hardik Savani December 7, 2019 Category : LaravelPauseUnmuteLoaded: 2.45%FullscreenIn this tutorial, we will discuss how to improve performance in laravel 6 using laravel model caching. we can Performance Boost using eloquent model caching via genealabs/laravel-model-caching git composer package in laravel 6, laravel 7, laravel 8 and laravel 9.
As we know, website performance is very important for site owner. If your website working smooth then traffic will be more and if you used eloquent cache then it's no longer load on server too. so in this post i will show you how to make mode caching and make performance boost you laravel application.
there are several ways to improve the performance of laravel website. As you know we almost looking to cache using htaccess and cache images, css, js and html file. if you cache css, js, html then it improve performance of website that i did in my previous tutorial, you can read from here: How to optimize Website Speed and Performance in Laravel?.
But if you have to fetch many records from a database and from many tables. so when you open link then more than one query always run on your web page. it takes time to fetch data from the database because it same process always when you load page, so basically it loads every time. At a long time when you have more data and mode visitors comes in our website then your server will be broken, but if you use laravel cache then it can save your server and improve page performance. Here we will use genealabs/laravel-model-caching package for model caching.
Laravel cache is very easy and simple way to use. Using Cache you will optimize your website page speed. So let's just follow bellow step and see how it works. We will use barryvdh/laravel-debugbar package also, this package will help to debug how many query fire on your page so you can see that. So just follow bellow step.
Step 1: Install Laravel 6 App
we are going to from scratch so, we need to get fresh Laravel 6 application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Installation Of barryvdh/laravel-debugbar Package
Now we will install barryvdh/laravel-debugbar composer package using by following command in our laravel 6 application. So let's run bellow command.
composer require barryvdh/laravel-debugbar --dev
Ok, after install package successfully, we will add service provider in app.php configuration file. So let's add as bellow:
Step 3: Installation Of genealabs/laravel-model-caching Package
Now we will install genealabs/laravel-model-caching composer package using by following command in our laravel 6 application. So let's run bellow command.
composer require genealabs/laravel-model-caching
Step 4: Update User Model
In this step, we will use GeneaLabs package class in User model So you have to update user model like as bellow:
app/User.php
<?phpnamespace 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 will create some dummy users records using factory. so you can run bellow command to create dummy users in your database.
now will create one route for display users in view. so let's create one route as bellow listed:
routes/web.php
Route::get('users', 'UserController@index');
Step 7: Create Controller
we need to create new controller as UserController and then we will create index() on controller. we will get all users using User model and print in view:
app/Http/Controllers/UserController.php
<?phpnamespace 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 at last we will create users.blade.php file and write code of display users lists. so let's create blade file: