Auto-translation used

How to Make Your Laravel Website Super Fast with Page Caching

1. Install the package:

composer require silber/page-cache

2. Add a caching intermediary to http/kernel.php:

'page-cache' => \Silber\PageCache\Middleware\CacheResponse::class,

3. Apply an intermediary to the routes:

Route::get('/post/{slug}', 'SiteController@post')->middleware('page-cache');

4. Set it up.htaccess:

# Serve Cached Page If Available...
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{DOCUMENT_ROOT}/page-cache/pc__index__pc.html -f
RewriteRule .? page-cache/pc__index__pc.html [L]
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.html -f
RewriteRule . page-cache%{REQUEST_URI}.html [L]
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.json -f
RewriteRule . page-cache%{REQUEST_URI}.json [L]

5. Clearing the cache:

6. Automatic cleaning when updating the model:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Artisan;

class Post extends Model
{
    public static function boot()
    {
        parent::boot();

        static::updated(function ($model) {
            Artisan::call("page-cache:clear $model->slug");
        });
    }
}

Caching pages significantly improves the performance of your site by reducing response time and server load. It is especially useful for high-traffic sites where every millisecond counts.

Using page caching with the silber/page-cache package helps to improve the performance of your Laravel application, making it fast and responsive. This method is easy to implement and maintain, making it an ideal choice for developers looking to improve the user experience on their sites.

Comments 2

Login to leave a comment

Комментарий для получения 1хр

Reply