Code Monkey home page Code Monkey logo

cdn's Introduction

Laravel CDN Cache Control and Invalidations

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package was created to help managing CDN granular caching and invalidations. While using Akamai or CloudFront, we usually bust the whole cache when we update something on our backend. This pacakge will do it granularly by doing the following:

  • Granular invalidation this package will create a collection of all models that impacts one page and when one of those models change, all pages that had that model rendered in previous requests will be purged from CDN.
  • Granular control of Cache-Control headers: you will be able to configure it differently per request, telling the CDN to store some pages for one week and others for 5 seconds, for example.
  • Define different strategies for Cache-Control: web pages may have a different cache strategy than api endpoints.
  • Prevents from caching pages containing forms.
  • Caches only frontend pages, leaving the CMS uncashed, if needed.
  • Re-warm pages purged from cache.
  • Strip cookies from cachable responses.
  • Disable caching for some pages using a middlware.
  • Define HTTP methods that allow caching or not: cache GET but not POST.
  • Define HTTP response status codes that allow caching or not: Cache 200 and 301 but not 400+ status codes.
  • Define what routes can and cannot be cached by CDN.
  • Define what type of responses can be cached: cache Response but not JsonResponse, for example.
  • Define what Model classes can be cached or not.
  • Remember what pages have been cached and command your CDN service to burst only those when you save something on your backend.
  • Supports CloudFront invalidations.
  • Supports Akamai EdgeCacheTags invalidations.
  • Allow override of Services and easy implementation to support new CDN Services.
  • Spatie's Laravel Response Cache granular invalidations.

Installation

You can install the package via composer:

composer require area17/cdn

You can publish the config file with:

php artisan vendor:publish --provider="A17\CDN\ServiceProvider"

And run the migrations:

php artisan migrate

Configuration

Dependencies

The supported CDN services have these package dependencies that you need to install yourself:

Akamai: akamai-open/edgegrid-auth CloudFront: aws/aws-sdk-php

Usage

Do a full read on the config/cdn.php there's a lot of configuration items and we tried to document them all.

Define your CDN service class on config/cdn.php:

'classes' => [
    'cdn' => A17\CDN\Services\CloudFront\Service::class,
    
    ...
]

Add the trait A17\CDN\Behaviours\CachedOnCDN to your models and repositories.

Call $this->invalidateCDNCache($model) every time a model (on your base model or repository save() method). This example takes in consideration Twill's repositories:

public function afterSave($object, $fields)
{
    $this->invalidateCDNCache($object);

    parent::afterSave($object, $fields);
}

Call $this->cacheModelOnCDN($model) method on model's getAttribute():

public function getAttribute($key)
{
    $this->cacheModelOnCDN($this);

    return parent::getAttribute($key);
}

Add the Middlware to the Kernel.php file:

protected $middleware = [
    \A17\CDN\Middleware::class,
    ...
];

Cache-Control max-age is set automatically, but if you need to change it depending on the current request you can use the following method:

CacheControl::setMaxAge(5000);

If you want to invalidate your paths in batches, add a scheduler setting the desired frequency for this to happen:

protected function schedule(Schedule $schedule)
{
    $schedule->job(new PurgeTags())->everyMinute();
}

You need to enable the package and the warmer on your .env file

CDN_ENABLED=true
CDN_WARMER_ENABLED=true

CDN third-party service configuration

Please check the respective environment variables needed for supported services to work:

Rewarming cache

Purged cache pages can load slowly for the next users or even Google Bot, if you want to prevent this you can enable (on config) the cache warmer and add the job to the schedule:

protected function schedule(Schedule $schedule)
{
    $schedule->job(new WarmCache())->everyMinute();
}

Note that the most hit (or frequently updated) pages will be warmed first.

Laravel Response Cache integration

If you have Response Cache installed, this package will automatically store its tags per page and when a model is changed it will not only purge the CDN pages related to this page but also bust the Response Cache internal cache. After, when the warmer passes back to warm those pages, Response Cache should also cache those pages again. This ensures that even if CDN hits your origin from a different not-yet-cached-region, responses will be still be blazing fast.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

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.