@@ -26,6 +26,18 @@ class PurgeCache
26
26
*/
27
27
private $ logger ;
28
28
29
+ /**
30
+ * Batch size of the purge request.
31
+ *
32
+ * Based on default Varnish 4 http_req_hdr_len size minus a 512 bytes margin for method,
33
+ * header name, line feeds etc.
34
+ *
35
+ * @see https://varnish-cache.org/docs/4.1/reference/varnishd.html
36
+ *
37
+ * @var int
38
+ */
39
+ private $ requestSize = 7680 ;
40
+
29
41
/**
30
42
* Constructor
31
43
*
@@ -52,10 +64,59 @@ public function __construct(
52
64
*/
53
65
public function sendPurgeRequest ($ tagsPattern )
54
66
{
67
+ $ successful = true ;
55
68
$ socketAdapter = $ this ->socketAdapterFactory ->create ();
56
69
$ servers = $ this ->cacheServer ->getUris ();
57
- $ headers = [self ::HEADER_X_MAGENTO_TAGS_PATTERN => $ tagsPattern ];
58
70
$ socketAdapter ->setOptions (['timeout ' => 10 ]);
71
+
72
+ $ formattedTagsChunks = $ this ->splitTags ($ tagsPattern );
73
+ foreach ($ formattedTagsChunks as $ formattedTagsChunk ) {
74
+ if (!$ this ->sendPurgeRequestToServers ($ socketAdapter , $ servers , $ formattedTagsChunk )) {
75
+ $ successful = false ;
76
+ }
77
+ }
78
+
79
+ return $ successful ;
80
+ }
81
+
82
+ /**
83
+ * Split tags by batches
84
+ *
85
+ * @param string $tagsPattern
86
+ * @return \Generator
87
+ */
88
+ private function splitTags ($ tagsPattern )
89
+ {
90
+ $ tagsBatchSize = 0 ;
91
+ $ formattedTagsChunk = [];
92
+ $ formattedTags = explode ('| ' , $ tagsPattern );
93
+ foreach ($ formattedTags as $ formattedTag ) {
94
+ if ($ tagsBatchSize + strlen ($ formattedTag ) > $ this ->requestSize - count ($ formattedTagsChunk ) - 1 ) {
95
+ yield implode ('| ' , array_unique ($ formattedTagsChunk ));
96
+ $ formattedTagsChunk = [];
97
+ $ tagsBatchSize = 0 ;
98
+ }
99
+
100
+ $ tagsBatchSize += strlen ($ formattedTag );
101
+ $ formattedTagsChunk [] = $ formattedTag ;
102
+ }
103
+ if (!empty ($ formattedTagsChunk )) {
104
+ yield implode ('| ' , array_unique ($ formattedTagsChunk ));
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Send curl purge request to servers
110
+ * to invalidate cache by tags pattern
111
+ *
112
+ * @param \Zend\Http\Client\Adapter\Socket $socketAdapter
113
+ * @param \Zend\Uri\Uri[] $servers
114
+ * @param string $formattedTagsChunk
115
+ * @return bool Return true if successful; otherwise return false
116
+ */
117
+ private function sendPurgeRequestToServers ($ socketAdapter , $ servers , $ formattedTagsChunk )
118
+ {
119
+ $ headers = [self ::HEADER_X_MAGENTO_TAGS_PATTERN => $ formattedTagsChunk ];
59
120
foreach ($ servers as $ server ) {
60
121
$ headers ['Host ' ] = $ server ->getHost ();
61
122
try {
@@ -69,12 +130,11 @@ public function sendPurgeRequest($tagsPattern)
69
130
$ socketAdapter ->read ();
70
131
$ socketAdapter ->close ();
71
132
} catch (\Exception $ e ) {
72
- $ this ->logger ->critical ($ e ->getMessage (), compact ('server ' , 'tagsPattern ' ));
133
+ $ this ->logger ->critical ($ e ->getMessage (), compact ('server ' , 'formattedTagsChunk ' ));
73
134
return false ;
74
135
}
75
136
}
76
-
77
- $ this ->logger ->execute (compact ('servers ' , 'tagsPattern ' ));
137
+ $ this ->logger ->execute (compact ('servers ' , 'formattedTagsChunk ' ));
78
138
return true ;
79
139
}
80
140
}
0 commit comments