Giter Site home page Giter Site logo

laravel-cache-route's Introduction

Cache contents of entire route in Laravel

Total Downloads Latest Stable Version License

This is the Laravel 7.0+ / PHP 7.2+ package, which provides the ability to cache routes for the allotted time.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.