Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
darkperis committed Jan 25, 2024
0 parents commit cd0cfd5
Show file tree
Hide file tree
Showing 9 changed files with 1,086 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## v1.0.0
- Added support for setting ADNCache Cache-Control via a middleware
- Added support for purging the cache via a Facade
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
## Laravel ADNCache

This package allows you to use ADNcache together with Laravel.

It provides two middlewares and one facade:

- ADNCache Middleware to control the cache-control header for Edgeport ADN Cache
- ADNCache facade to handle purging

## Installation

Require this package using composer.

```
composer require darkperis/dpadn-laravel
```

Laravel uses Auto-Discovery, so you won't have to make any changes to your application, the two middlewares and facade will be available right from the beginning.

#### Steps for Laravel >=5.1 and <=5.4

The package can be used for Laravel 5.1 to 5.4 as well, however due to lack of Auto-Discovery, a few additional steps have to be performed.

In `config/app.php` you have to add the following code in your `aliases`:

```
'aliases' => [
...
'ADNCache' => Darkpony\ADNCache\ADNCache::class,
],
```

In `app/Http/Kernel.php` you have to add the two middlewares under `middleware` and `routeMiddleware`:

```
protected $middleware = [
...
\Darkpony\ADNCache\ADNCacheMiddleware::class
];
protected $routeMiddleware = [
...
'adncache' => \Darkpony\ADNCache\ADNCacheMiddleware::class
];
```

Copy `adncache.php` to `config/`:

Copy the package `config/adncache.php` file to your `config/` directory.

**important**: Do not add the ServiceProvider under `providers` in `config/app.php`.

#### Steps for Laravel 5.5 and above

You should publish the package configuration, which allows you to set the defaults for the `Cache-Control` header:

```
php artisan vendor:publish --provider="Darkpony\ADNCache\ADNCacheServiceProvider"
```

## Usage

The package comes with 2 functionalities: Setting the cache control headers for adncache and purging.

### cache-control

You'll be able to configure defaults in the `config/adncache.php` file, here you can set the max-age (`default_ttl`), the cacheability (`default_cacheability`) such as public, private or no-cache or enable esi (`esi`) in the `Cache-Control` response header.

If the `default_ttl` is set to `0`, then we won't return the `Cache-Control` response header.

You can control the config settings in your `.env` file as such:

- `ADNCACHE_API_KEY` - accepts api key
- `ADNCACHE_ENDPOINT` - accepts endpoint
- `ADNCACHE_ESI_ENABLED` - accepts `true` or `false` to whether you want ESI enabled or not globally; Default `false`
- `ADNCACHE_DEFAULT_TTL` - accepts an integer, this value is in seconds; Default: `0`
- `ADNCACHE_DEFAULT_CACHEABILITY` - accepts a string, you can use values such as `private`, `no-cache`, `public` or `no-vary`; Default: `no-cache`
- `ADNCACHE_GUEST_ONLY` - accepts `true` or `false` to decide if the cache should be enabled for guests only; Defaults to `false`

You set the cache-control header for adncache using a middleware, so we can in our routes do something like this:

```php
Route::get('/', function() {
return view('frontpage');
});

Route::get('/about-us', function() {
return view('about-us');
})->middleware('adncache:max-age=300;public');

Route::get('/contact', function() {
return view('contact');
})->middleware('adncache:max-age=10;private;esi=on');

Route::get('/admin', function() {
return view('admin');
})->middleware('adncache:no-cache');
```

### purge

If we have an admin interface that controls for example a blog, when you publish a new article, you might want to purge the frontpage of the blog so the article appears in the overview.

You'd do this in your controller by doing

```php
<?php

namespace App\Http\Controllers;

use ADNCache;

class BlogController extends BaseController
{
// Your article logic here

ADNCache::purge('/');
}
```

You can also purge everything by doing:

```php
ADNCache::purge('*');
// or
ADNCache::purgeAll();
```

One or multiple URIs can be purged by using a comma-separated list:

```php
ADNCache::purge('/blog,/about-us,/');
// or
ADNCache::purgeItems(['/blog', '/about-us', '/']);
````

```php
ADNCache::purge('*', false);
// or
ADNCache::purgeAll(false);
```


### Laravel Authentication

If you use authentication in Laravel for handling guests and logged-in users, you'll likely want to also separate the cache for people based on this.

This can be done in the `.htaccess` file simply by using the cache-vary on the Authorization cookie:

```apache
RewriteEngine On
RewriteRule .* - [E=Cache-Vary:Authorization]
```

**Note: In the above example we use `Authorization`, this may have a different name depending on your setup, so it has to be changed accordingly.**
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "darkperis/dpadn-laravel",
"description": "ADN Cache Implementation for Laravel",
"require": {
"illuminate/support": "^5.1|^6|^7|^8|^9|^10.0",
"guzzlehttp/guzzle": "^7.0.1"
},
"license": "GPL-3.0-only",
"authors": [
{
"name": "Periklis Diamantidis",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Darkpony\\ADNCache\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Darkpony\\ADNCache\\ADNCacheServiceProvider"
],
"aliases": {
"ADNCache": "Darkpony\\ADNCache\\ADNCache"
}
}
}
}
34 changes: 34 additions & 0 deletions config/adncache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

return [
/**
* Edgeport API KEY
*/
'api_key' => env('ADNCACHE_API_KEY', ''),

/**
* Edgeport Endpoint
*/
'endpoint' => env('ADNCACHE_ENDPOINT', 'https://api.edgeport.com/api/v1/wordpress/purge'),

/**
* Enable ESI
*/
'esi' => env('ADNCACHE_ESI_ENABLED', false),

/**
* Default cache TTL in seconds
*/
'default_ttl' => env('ADNCACHE_DEFAULT_TTL', 0),

/**
* Default cache storage
* private,no-cache,public,no-vary
*/
'default_cacheability' => env('ADNCACHE_DEFAULT_CACHEABILITY', 'no-cache'),

/**
* Guest only mode (Do not cache logged in users)
*/
'guest_only' => env('ADNCACHE_GUEST_ONLY', false),
];
13 changes: 13 additions & 0 deletions src/ADNCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Darkpony\ADNCache;

use Illuminate\Support\Facades\Facade;

class ADNCache extends Facade
{
protected static function getFacadeAccessor()
{
return EdgeportCache::class;
}
}
59 changes: 59 additions & 0 deletions src/ADNCacheMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Darkpony\ADNCache;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class ADNCacheMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $adncache_control
* @return mixed
*/
public function handle($request, Closure $next, string $adncache_control = null)
{
$response = $next($request);

if (!in_array($request->getMethod(), ['GET', 'HEAD']) || !$response->getContent()) {
return $response;
}

$esi_enabled = config('adncache.esi');
$maxage = config('adncache.default_ttl', 0);
$cacheability = config('adncache.default_cacheability');
$guest_only = config('adncache.guest_only', false);

if ($maxage === 0 && $adncache_control === null) {
return $response;
}

//'cache.headers:public;max_age=2628000;etag'
if ($guest_only && Auth::check()) {
$response->headers->set('Cache-Control', 'no-cache');

return $response;
}

$adncache_string = "max-age=$maxage,$cacheability";

if (isset($adncache_control)) {
$adncache_string = str_replace(';', ',', $adncache_control);
}

if (Str::contains($adncache_string, 'esi=on') == false) {
$adncache_string = $adncache_string.($esi_enabled ? ',esi=on' : null);
}

if ($response->headers->has('Cache-Control') == false) {
$response->headers->set('Cache-Control', $adncache_string);
}

return $response;
}
}
35 changes: 35 additions & 0 deletions src/ADNCacheServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Darkpony\ADNCache;

use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;
use Illuminate\Contracts\Http\Kernel;

class ADNCacheServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot(Router $router, Kernel $kernel)
{
$router->aliasMiddleware('adncache', ADNCacheMiddleware::class);
$kernel->pushMiddleware(ADNCacheMiddleware::class);

$this->publishes([
__DIR__ . '/../config/adncache.php' => config_path('adncache.php'),
], 'config');
}
}
Loading

0 comments on commit cd0cfd5

Please sign in to comment.