-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPurgeAsyncCache.php
More file actions
77 lines (67 loc) · 2.1 KB
/
Copy pathPurgeAsyncCache.php
File metadata and controls
77 lines (67 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace IntegerNet\AsyncVarnish\Model;
use Magento\CacheInvalidate\Model\PurgeCache;
use IntegerNet\AsyncVarnish\Model\TagRepository;
use Magento\Framework\App\Config\ScopeConfigInterface;
class PurgeAsyncCache
{
const VARNISH_PURGE_TAG_GLUE = "|";
const MAX_HEADER_LENGTH_CONFIG_PATH = 'system/full_page_cache/async_varnish/varnish_max_header_length';
/**
* @var PurgeCache
*/
private $purgeCache;
/**
* @var TagRepository
*/
private $tagRepository;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
public function __construct(
PurgeCache $purgeCache,
ScopeConfigInterface $scopeConfig,
TagRepository $tagRepository
) {
$this->purgeCache = $purgeCache;
$this->scopeConfig = $scopeConfig;
$this->tagRepository = $tagRepository;
}
private function getMaxHeaderLengthFromConfig()
{
return $this->scopeConfig->getValue(self::MAX_HEADER_LENGTH_CONFIG_PATH);
}
/**
* @throws \Zend_Db_Statement_Exception
* @throws \Exception
*/
public function run(): int
{
$tags = $this->tagRepository->getAll();
$maxHeaderLength = $this->getMaxHeaderLengthFromConfig();
if (!empty($tags)) {
$tagChunks = [];
$index = 0;
foreach ($tags as $tag) {
$nextChunkString = (isset($tagChunks[$index])
? $tagChunks[$index] . self::VARNISH_PURGE_TAG_GLUE
: '') . $tag;
if (strlen($nextChunkString) <= $maxHeaderLength) {
$tagChunks[$index] = $nextChunkString;
} else {
$index ++;
$tagChunks[$index] = $tag;
}
}
foreach ($tagChunks as $tagChunk) {
$this->purgeCache->sendPurgeRequest($tagChunk);
}
}
if ($lastUsedId = $this->tagRepository->getLastUsedId()) {
$this->tagRepository->deleteUpToId($lastUsedId);
}
return count($tags);
}
}