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