Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added chunk support for large key number purges #133

Merged
merged 1 commit into from
Jan 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions Model/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Api
const FASTLY_HEADER_SOFT_PURGE = 'Fastly-Soft-Purge';
const PURGE_TIMEOUT = 10;
const PURGE_TOKEN_LIFETIME = 30;
const FASTLY_MAX_HEADER_KEY_SIZE = 256;

/**
* @var Config $config,
Expand Down Expand Up @@ -140,15 +141,30 @@ public function cleanUrl($url)
public function cleanBySurrogateKey($keys)
{
$uri = $this->_getApiServiceUri() . 'purge';
$payload = json_encode(['surrogate_keys' => $keys]);
if ($result = $this->_purge($uri, \Zend_Http_Client::POST, $payload)) {
foreach ($keys as $key) {
$this->logger->execute('surrogate key: ' . $key);
}
$num = count($keys);
if ($num >= self::FASTLY_MAX_HEADER_KEY_SIZE) {
$parts = $num / self::FASTLY_MAX_HEADER_KEY_SIZE;
$additional = ($parts > (int)$parts) ? 1 : 0;
$parts = (int)$parts + (int)$additional;
$chunks = ceil($num/$parts);
$collection = array_chunk($keys, $chunks);
} else {
$collection = [$keys];
}

if ($this->config->areWebHooksEnabled() && $this->config->canPublishKeyUrlChanges()) {
$this->sendWebHook('*clean by key on ' . join(" ", $keys) . '*');
foreach($collection as $keys) {

$payload = json_encode(['surrogate_keys' => $keys]);
if ($result = $this->_purge($uri, \Zend_Http_Client::POST, $payload)) {
foreach ($keys as $key) {
$this->logger->execute('surrogate key: ' . $key);
}
}

if ($this->config->areWebHooksEnabled() && $this->config->canPublishKeyUrlChanges()) {
$this->sendWebHook('*clean by key on ' . join(" ", $keys) . '*');
}

}

return $result;
Expand Down