@@ -39,39 +39,46 @@ class PurgeCache
39
39
*
40
40
* @var int
41
41
*/
42
- private $ requestSize = 7680 ;
42
+ private $ requestSize ;
43
43
44
44
/**
45
45
* Constructor
46
46
*
47
47
* @param \Magento\PageCache\Model\Cache\Server $cacheServer
48
48
* @param \Magento\CacheInvalidate\Model\SocketFactory $socketAdapterFactory
49
49
* @param InvalidateLogger $logger
50
+ * @param int $maxHeaderSize
50
51
*/
51
52
public function __construct (
52
53
\Magento \PageCache \Model \Cache \Server $ cacheServer ,
53
54
\Magento \CacheInvalidate \Model \SocketFactory $ socketAdapterFactory ,
54
- InvalidateLogger $ logger
55
+ InvalidateLogger $ logger ,
56
+ $ maxHeaderSize = 7680
55
57
) {
56
58
$ this ->cacheServer = $ cacheServer ;
57
59
$ this ->socketAdapterFactory = $ socketAdapterFactory ;
58
60
$ this ->logger = $ logger ;
61
+ $ this ->requestSize = $ maxHeaderSize ;
59
62
}
60
63
61
64
/**
62
65
* Send curl purge request to invalidate cache by tags pattern
63
66
*
64
- * @param string $tagsPattern
67
+ * @param array| string $tags
65
68
* @return bool Return true if successful; otherwise return false
66
69
*/
67
- public function sendPurgeRequest ($ tagsPattern )
70
+ public function sendPurgeRequest ($ tags )
68
71
{
72
+ if (!is_string ($ tags )) {
73
+ $ tags = [$ tags ];
74
+ }
75
+
69
76
$ successful = true ;
70
77
$ socketAdapter = $ this ->socketAdapterFactory ->create ();
71
78
$ servers = $ this ->cacheServer ->getUris ();
72
79
$ socketAdapter ->setOptions (['timeout ' => 10 ]);
73
80
74
- $ formattedTagsChunks = $ this ->splitTags ( $ tagsPattern );
81
+ $ formattedTagsChunks = $ this ->chunkTags ( $ tags );
75
82
foreach ($ formattedTagsChunks as $ formattedTagsChunk ) {
76
83
if (!$ this ->sendPurgeRequestToServers ($ socketAdapter , $ servers , $ formattedTagsChunk )) {
77
84
$ successful = false ;
@@ -82,24 +89,24 @@ public function sendPurgeRequest($tagsPattern)
82
89
}
83
90
84
91
/**
85
- * Split tags by batches
92
+ * Split tags into batches to suit Varnish max. header size
86
93
*
87
- * @param string $tagsPattern
94
+ * @param array $tags
88
95
* @return \Generator
89
96
*/
90
- private function splitTags ( $ tagsPattern )
97
+ private function chunkTags ( $ tags )
91
98
{
92
- $ tagsBatchSize = 0 ;
99
+ $ currentBatchSize = 0 ;
93
100
$ formattedTagsChunk = [];
94
- $ formattedTags = explode ( ' | ' , $ tagsPattern );
95
- foreach ( $ formattedTags as $ formattedTag ) {
96
- if ($ tagsBatchSize + strlen ($ formattedTag ) > $ this -> requestSize - count ($ formattedTagsChunk ) - 1 ) {
101
+ foreach ( $ tags as $ formattedTag ) {
102
+ // Check if (currentBatchSize + length of next tag + number of pipe delimiters) would exceed header size.
103
+ if ($ currentBatchSize + strlen ($ formattedTag ) + count ($ formattedTagsChunk ) > $ this -> requestSize ) {
97
104
yield implode ('| ' , $ formattedTagsChunk );
98
105
$ formattedTagsChunk = [];
99
- $ tagsBatchSize = 0 ;
106
+ $ currentBatchSize = 0 ;
100
107
}
101
108
102
- $ tagsBatchSize += strlen ($ formattedTag );
109
+ $ currentBatchSize += strlen ($ formattedTag );
103
110
$ formattedTagsChunk [] = $ formattedTag ;
104
111
}
105
112
if (!empty ($ formattedTagsChunk )) {
0 commit comments