Skip to content

Instantly share code, notes, and snippets.

@ohthehugemanatee
Last active May 14, 2017 13:47
Show Gist options
  • Save ohthehugemanatee/c82050b3052b2890cdd3198abff15a2e to your computer and use it in GitHub Desktop.
Save ohthehugemanatee/c82050b3052b2890cdd3198abff15a2e to your computer and use it in GitHub Desktop.
how I invalidated keycdn
<?php
// Invalidate and update the item state.
$hashes = KeycdnCacheTagHeaderGenerator::cacheTagsToHashes($tags);
// Break the list of tags into multiple requests.
$hash_sets = array_chunk($hashes, 1000);
foreach ($hash_sets as $hash_set) {
$invalidation_state = $this->invalidateItems('tags', $hash_set);
}
// ...
/**
* Invalidate Key CDN cache.
*
* @param mixed $type
* Type to purge like tags/url. If null, will purge everything.
* @param string[] $invalidates
* A list of items to invalidate.
*
* @return int
* Returns invalidate items.
*/
protected function invalidateItems($type = NULL, array $invalidates = []) {
// Get zone info.
$zone = $this->settings->get('zone');
$uri = $this->getPurgeUri($type) . "/{$zone}.json";
// This will contain the info that need to be cleared. For the case of
// 'everything', this will be empty and thus nothing.
$params = [];
// If url/tag.
if ($type) {
$params['json'] = [$type => $invalidates];
}
try {
// Purge everything for the given zone.
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $this->httpClient->request('DELETE', $uri, [
'auth' => [$this->settings->get('api_key'), ''],
'headers' => ['Content-Type' => 'application/json'],
'connect_timeout' => 2,
] + $params);
// If successfully clears cache.
if ($response->getStatusCode() == 200) {
return InvalidationInterface::SUCCEEDED;
}
return InvalidationInterface::FAILED;
}
catch (\Exception $e) {
// If something bad happens.
return InvalidationInterface::FAILED;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment