Looking to hire Laravel developers? Try LaraJobs

laravel-cache-route maintained by stounhandj

Description
Cache (HTML) output of entire route to speedup your Laravel application
Last update
2022/06/27 18:41 (dev-master)
License
Links
Downloads
223

Comments
comments powered by Disqus

Cache contents of entire route in Laravel

Installation

$ composer require stounhandj/laravel-cache-route

Or

{
    "require": {
        "stounhandj/laravel-cache-route": "^v1.1"
    }
}

Usage

Add middleware to the file kernel.php:

'cache.page' => \StounhandJ\LaravelCacheRoute\Middleware\CacheRoteMiddleware::class,

Now, use the middleware to cache the HTML output of an entire page from your route like so:

  1. In your route:

    Route::get('/', function () {
         //
    })->middleware("cache.page")
    

    You may also use route groups. Please look up Laravel documentation on Middleware to learn more here

Configuration Options

You can configure the TTL (Time-To-Live) to cast per second:

  1. In your route:
    Route::get('/', function () {
         //
    })->middleware("cache.page:10")
    
  2. Environment (On all routes at once):
    CACHE_TTL=10
    

Thoughts

Be VERY cautions when using a whole page cache such as this. Remember contents of the cache are visible to ALL your users.

  1. For, "mostly static" content, go for it!
  2. For, "mostly dynamic" content or heavily user-customized content, AVOID this strategy. User specific information is gathered server side. So, you essentially WANT to hit the server.

Good rule of thumb: If two different users see different pages on hitting the same URL, DO NOT cache the output using this strategy. An alternative may be to cache database queries.