public function VarnishBundledPurger::invalidate in Varnish purger 8
Same name and namespace in other branches
- 8.2 src/Plugin/Purge/Purger/VarnishBundledPurger.php \Drupal\varnish_purger\Plugin\Purge\Purger\VarnishBundledPurger::invalidate()
Invalidate content from external caches.
Implementations of this method have the responsibility of invalidating the given list of invalidation objects from their external caches. Besides the invalidation itself, it also needs to call ::setState() on each object to reflect the correct state after invalidation.
You can set it to the following states:
- \Drupal\purge\Plugin\Purge\Invalidation\InvStatesInterface::SUCCEEDED
- \Drupal\purge\Plugin\Purge\Invalidation\InvStatesInterface::FAILED
- \Drupal\purge\Plugin\Purge\Invalidation\InvStatesInterface::PROCESSING
- \Drupal\purge\Plugin\Purge\Invalidation\InvStatesInterface::NOT_SUPPORTED
PROCESSING is a special state only intended to be used on caching platforms where more time is required than 1-2 seconds to clear its cache. Usually CDNs with special status API calls where you can later find out if the object succeeded invalidation. When set to this state, the object flows back to the queue to be offered to your plugin again later.
NOT_SUPPORTED will be rarely needed, as invalidation types not listed as supported by your plugin will already be put to this state before it is offered to your plugin by PurgersServiceInterface::invalidate(). However, if there is any technical reason why you couldn't support a particular invalidation at that given time, you can set it as such and it will be offered again later.
Parameters
\Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface[] $invalidations: Non-associative array of invalidation objects that each describe what needs to be invalidated by the external caching system. Usually these objects originate from the queue but direct invalidation is also possible, in either cases the behavior of your plugin stays the same.
The number of objects given is dictated by the outer limit of Purge's capacity tracking mechanism and is dynamically calculated. The lower your ::getTimeHint() implementation returns, the more that will be offered at once. However, your real execution time can and should never exceed the defined hint, to protect system stability.
Overrides PurgerInterface::invalidate
See also
\Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::setState()
\Drupal\purge\Plugin\Purge\Purger\PurgerCapacityDataInterface::getTimeHint()
File
- src/
Plugin/ Purge/ Purger/ VarnishBundledPurger.php, line 26
Class
- VarnishBundledPurger
- Varnish Bundled Purger.
Namespace
Drupal\varnish_purger\Plugin\Purge\PurgerCode
public function invalidate(array $invalidations) {
// Create a simple closure to mass-update states on the objects.
$set_state = function ($state) use ($invalidations) {
foreach ($invalidations as $invalidation) {
$invalidation
->setState($state);
}
};
// Build up a single HTTP request, execute it and log errors.
$token_data = [
'invalidations' => $invalidations,
];
$uri = $this
->getUri($token_data);
$opt = $this
->getOptions($token_data);
try {
$this->client
->request($this->settings->request_method, $uri, $opt);
$set_state(InvalidationInterface::SUCCEEDED);
} catch (\Exception $e) {
$set_state(InvalidationInterface::FAILED);
// Log as much useful information as we can.
$headers = $opt['headers'];
unset($opt['headers']);
$debug = json_encode(str_replace("\n", ' ', [
'msg' => $e
->getMessage(),
'uri' => $uri,
'method' => $this->settings->request_method,
'guzzle_opt' => $opt,
'headers' => $headers,
]));
$this
->logger()
->emergency("item failed due @e, details (JSON): @debug", [
'@e' => get_class($e),
'@debug' => $debug,
]);
}
}