Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Varnish: initalize = [] + purgeAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Stark committed Jul 10, 2018
1 parent ef83f89 commit 5c4895e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.3.7] - 2018-07-10
### Added
- Varnish: `headers` config option
- Varnish: purgeAll()

## [1.3.6] - 2018-06-18
### Changed
- DB fallback: Schema - changed `url` field from `varchar(255) to `text` to allow longer urls
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ostark/upper",
"description": "A cache plugin for Craft - supporting multiple Edge Caches",
"type": "craft-plugin",
"version": "1.3.6",
"version": "1.3.7",
"keywords": [
"craft",
"cms",
Expand Down
2 changes: 1 addition & 1 deletion src/config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'tagHeaderName' => 'XKEY',
'purgeHeaderName' => 'XKEY-PURGE',
'purgeUrl' => getenv('VARNISH_URL') ?: 'http://127.0.0.1:80/',
'headers' => ['Host' => getenv('VARNISH_HOST')]
'headers' => getenv('VARNISH_HOST') ? ['Host' => getenv('VARNISH_HOST')] : []
],

// Fastly config
Expand Down
24 changes: 22 additions & 2 deletions src/drivers/Varnish.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Varnish extends AbstractPurger implements CachePurgeInterface
/**
* @var array
*/
public $headers;
public $headers = [];

/**
* @param string $tag
Expand Down Expand Up @@ -65,11 +65,31 @@ public function purgeUrls(array $urls)

}


/**
* Purge entire cache
*
* Requires a custom vcl config
*
* @see https://varnish-cache.org/docs/6.0/users-guide/purging.html#bans
*
* @return bool
*/
public function purgeAll()
{
// TODO: Implement purgeAll() method.
$options = [
'base_uri' => $this->purgeUrl,
'headers' => $this->headers
];

$response = (new Client($options))->request('BAN');

return (in_array($response->getStatusCode(), [204, 200]))
? true
: false;
}


protected function sendPurgeRequest(array $options = [])
{
$response = (new Client($options))->request('PURGE');
Expand Down

0 comments on commit 5c4895e

Please sign in to comment.