area17/edge-flush is a Laravel package for cdn cache control and invalidation.
It currently has 9 GitHub stars and 2.831 downloads on Packagist (latest version v2.0.7).
Install it with composer require area17/edge-flush.
Discover more Laravel packages by area17
or browse all Laravel packages to compare alternatives.
Last updated
EdgeFlush is Laravel package intended to help developers manage CDN granular caching and invalidations. Having Akamai, CloudFront (or any other CDN) in front of a website, data modification usually forces us to bust the whole cache, leading to a website slow (for the first users) until the whole cache is rebuilt, and if the "first user" is Google Bot, for example, this can also impact on your website's rank. This pacakge aims to do invalidations granularly.
Install the package via composer:
composer require area17/edge-flush
Publish the config file with:
php artisan vendor:publish --provider="A17\EdgeFlush\ServiceProvider"
And run the migrations:
php artisan migrate
The supported CDN services have these package dependencies that you need to choose according to your setup:
Akamai: akamai-open/edgegrid-auth CloudFront: aws/aws-sdk-php
Do a full read on the config/edge-flush.php there's a lot of configuration items and we tried to document them all.
Define your CDN service class on config/edge-flush.php:
'classes' => [
'cdn' => A17\EdgeFlush\Services\CloudFront\Service::class,
...
]
Add the trait A17\EdgeFlush\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\EdgeFlush\Middleware::class,
...
];
Cache-Control max-age and s-maxage is set automatically, but if you need to change it depending on the current request you can use the following method:
CacheControl::setMaxAge(5000); // in seconds
CacheControl::setMaxAge('1 month'); // as a DateTime string period
CacheControl::setSMaxAge('2 weeks');
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
EDGE_FLUSH_ENABLED=true
EDGE_FLUSH_WARMER_ENABLED=true
Please check the respective environment variables needed for supported services to work:
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.
Akamai has a 128 bytes limit for the tag list, so if one page is impacted by lots of models, we would have no other way than busting the whole cache every time. This package creates a single Edge Cache Tag that relates to all models touched when the page was rendered, and adds it yo the response header:
edge-cache-tag: app-production-7e0ae085d699003a64e5fa7b75daae3d78ace842
In case you need to invalidate the whole CDN cache locally or on a deployment routing, you can:
php artisan edge-flush:invalidate-all
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.