Skip to content

Commit 1c7844a

Browse files
Merge pull request #1 from webscale-networks/per-event-cache-flush
Per event cache flush
2 parents 7be9b2b + 18e3a64 commit 1c7844a

File tree

12 files changed

+229
-17
lines changed

12 files changed

+229
-17
lines changed

Controller/Adminhtml/Purge/All.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public function execute(): ResponseInterface
2222
{
2323
try {
2424
if ($this->cacheConfig->getType() == CacheConfig::VARNISH && $this->config->isAvailable()) {
25-
if ($this->purgeCache->sendPurgeRequest(['tagsPattern' => ['.*']])) {
25+
if ($this->purgeCache->sendPurgeRequest([
26+
'tagsPattern' => ['.*'],
27+
'event' => 'adminhtml_manual_flush_all'
28+
])) {
2629
$this->messageManager->addSuccessMessage(
2730
__('Varnish cache flushed successfully.')
2831
);

Helper/Config.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class Config extends AbstractHelper
2424

2525
public const XML_PATH_DEBUG = 'webscale_varnish/developer/debug';
2626

27+
public const XML_PATH_EVENTS_ALL = 'webscale_varnish/cache_events/flush_all_events';
28+
29+
public const XML_PATH_EVENTS_PARTIAL = 'webscale_varnish/cache_events/partial_invalidate_events';
30+
2731

2832
/** @var ModuleListInterface $moduleList */
2933
private $moduleList;
@@ -103,6 +107,30 @@ public function getApplicationId(): string
103107
return (string) $this->scopeConfig->getValue(self::XML_PATH_APPLICATION);
104108
}
105109

110+
/**
111+
* Retrieve flush all events array
112+
*
113+
* @return array
114+
*/
115+
public function getEventsFlushAll()
116+
{
117+
$events = $this->scopeConfig->getValue(self::XML_PATH_EVENTS_ALL);
118+
119+
return is_array($events) ? $events : explode(',', $events);
120+
}
121+
122+
/**
123+
* Retrieve partial invalidate events array
124+
*
125+
* @return array
126+
*/
127+
public function getEventsPartialInvalidate()
128+
{
129+
$events = $this->scopeConfig->getValue(self::XML_PATH_EVENTS_PARTIAL);
130+
131+
return is_array($events) ? $events : explode(',', $events);
132+
}
133+
106134
/**
107135
* Prepare service URL
108136
*
@@ -121,12 +149,12 @@ public function getCacheUri()
121149
*/
122150
public function generateCacheParams(array $purge = []) {
123151
$params = [
124-
"json" => [
125-
"type" => "invalidate-cache",
126-
"target" => "/v2/applications/" . $this->getApplicationId(),
127-
"parameters" => [
128-
"urls" => ["*://*/*"],
129-
"tags" => !empty($purge['tagsPattern']) ? $purge['tagsPattern'] : [".*"],
152+
'json' => [
153+
'type' => 'invalidate-cache',
154+
'target' => '/v2/applications/' . $this->getApplicationId(),
155+
'parameters' => [
156+
'urls' => ['*://*/*'],
157+
'tags' => !empty($purge['tagsPattern']) ? $purge['tagsPattern'] : ['.*'],
130158
]
131159
],
132160
];
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Webscale. All rights reserved.
4+
* See LICENSE for license details.
5+
*/
6+
7+
namespace Webscale\Varnish\Model\Config\Source;
8+
9+
use Magento\Framework\Data\OptionSourceInterface;
10+
11+
class FlushAllEvents implements OptionSourceInterface
12+
{
13+
/**
14+
* @var array
15+
*/
16+
protected $options;
17+
18+
/**
19+
* @param bool $isMultiselect
20+
* @return array
21+
*/
22+
public function toOptionArray($isMultiselect = false)
23+
{
24+
if (!$this->options) {
25+
$this->options = [
26+
[
27+
'value' => 'clean_media_cache_after',
28+
'label' => 'clean_media_cache_after'
29+
],
30+
[
31+
'value' => 'clean_catalog_images_cache_after',
32+
'label' => 'clean_catalog_images_cache_after'
33+
],
34+
[
35+
'value' => 'adminhtml_cache_refresh_type',
36+
'label' => 'adminhtml_cache_refresh_type'
37+
],
38+
[
39+
'value' => 'adminhtml_cache_flush_all',
40+
'label' => 'adminhtml_cache_flush_all'
41+
],
42+
[
43+
'value' => 'assign_theme_to_stores_after',
44+
'label' => 'assign_theme_to_stores_after'
45+
],
46+
];
47+
}
48+
49+
$options = $this->options;
50+
51+
if (!$isMultiselect) {
52+
array_unshift($options, ['value' => '', 'label' => __('--Please Select--')]);
53+
}
54+
55+
return $options;
56+
}
57+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Webscale. All rights reserved.
4+
* See LICENSE for license details.
5+
*/
6+
7+
namespace Webscale\Varnish\Model\Config\Source;
8+
9+
use Magento\Framework\Data\OptionSourceInterface;
10+
11+
class InvalidateEvents implements OptionSourceInterface
12+
{
13+
/**
14+
* @var array
15+
*/
16+
protected $options;
17+
18+
/**
19+
* @param bool $isMultiselect
20+
* @return array
21+
*/
22+
public function toOptionArray($isMultiselect = false)
23+
{
24+
if (!$this->options) {
25+
$this->options = [
26+
[
27+
'value' => 'clean_cache_by_tags',
28+
'label' => 'clean_cache_by_tags'
29+
],
30+
[
31+
'value' => 'assigned_theme_changed',
32+
'label' => 'assigned_theme_changed'
33+
],
34+
[
35+
'value' => 'catalogrule_after_apply',
36+
'label' => 'catalogrule_after_apply'
37+
],
38+
[
39+
'value' => 'controller_action_postdispatch_adminhtml_system_currency_saveRates',
40+
'label' => 'controller_action_postdispatch_adminhtml_system_currency_saveRates'
41+
],
42+
[
43+
'value' => 'controller_action_postdispatch_adminhtml_system_config_save',
44+
'label' => 'controller_action_postdispatch_adminhtml_system_config_save'
45+
],
46+
[
47+
'value' => 'controller_action_postdispatch_adminhtml_catalog_product_action_attribute_save',
48+
'label' => 'controller_action_postdispatch_adminhtml_catalog_product_action_attribute_save'
49+
],
50+
[
51+
'value' => 'controller_action_postdispatch_adminhtml_catalog_product_massStatus',
52+
'label' => 'controller_action_postdispatch_adminhtml_catalog_product_massStatus'
53+
],
54+
[
55+
'value' => 'controller_action_postdispatch_adminhtml_system_currencysymbol_save',
56+
'label' => 'controller_action_postdispatch_adminhtml_system_currencysymbol_save'
57+
],
58+
[
59+
'value' => 'clean_cache_after_reindex',
60+
'label' => 'clean_cache_after_reindex'
61+
],
62+
[
63+
'value' => 'adminhtml_cache_flush_system',
64+
'label' => 'adminhtml_cache_flush_system'
65+
],
66+
];
67+
}
68+
69+
$options = $this->options;
70+
71+
if (!$isMultiselect) {
72+
array_unshift($options, ['value' => '', 'label' => __('--Please Select--')]);
73+
}
74+
75+
return $options;
76+
}
77+
}

Model/PurgeCache.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public function sendPurgeRequest(array $purge = []): bool
6262

6363
try {
6464
$uri = $this->config->getCacheUri();
65+
6566
$params = $this->config->generateCacheParams($purge);
67+
$params['event'] = !empty($purge['event']) ? $purge['event'] : '';
68+
6669
$response = $this->api->doRequest($uri, $params, Request::HTTP_METHOD_POST);
6770

6871
$tagsPattern = (!empty($purge['tagsPattern']) && is_array($purge['tagsPattern']))

Observer/FlushAllCacheObserver.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ public function __construct(
4747
*/
4848
public function execute(Observer $observer): void
4949
{
50+
$event = $observer->getEvent();
51+
$events = $this->config->getEventsFlushAll();
52+
5053
try {
51-
if ($this->cacheConfig->getType() == CacheConfig::VARNISH && $this->config->isAvailable()) {
52-
$this->purgeCache->sendPurgeRequest(['tagsPattern' => ['.*']]);
54+
if ($this->cacheConfig->getType() == CacheConfig::VARNISH
55+
&& $this->config->isAvailable()
56+
&& in_array($event->getName(), $events)
57+
) {
58+
$this->purgeCache->sendPurgeRequest(['tagsPattern' => ['.*'], 'event' => $event->getName()]);
5359
}
5460
} catch (\Exception $e) {
5561
$this->config->log($e->getMessage() . PHP_EOL . $e->getTraceAsString(), 'critical');

Observer/InvalidateVarnishObserver.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ public function __construct(
5555
*/
5656
public function execute(Observer $observer): void
5757
{
58+
$event = $observer->getEvent();
59+
$events = $this->config->getEventsPartialInvalidate();
60+
5861
try {
59-
if ($this->cacheConfig->getType() == CacheConfig::VARNISH && $this->config->isAvailable()) {
60-
$object = $observer->getEvent()->getObject();
62+
if ($this->cacheConfig->getType() == CacheConfig::VARNISH
63+
&& $this->config->isAvailable()
64+
&& in_array($event->getName(), $events)
65+
) {
66+
$object = $event->getObject();
6167
$tags = [];
6268
if ($object instanceof IdentityInterface) {
6369
foreach ($object->getIdentities() as $tag) {
@@ -71,7 +77,7 @@ public function execute(Observer $observer): void
7177
}
7278

7379
if (!empty($tags)) {
74-
$this->purgeCache->sendPurgeRequest(['tagsPattern' => $tags]);
80+
$this->purgeCache->sendPurgeRequest(['tagsPattern' => $tags, 'event' => $event->getName()]);
7581
}
7682
}
7783
}

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22
Allows to setup and configure Webscale varnish cache implementation.
33

44
## Installation
5-
To install extension run the following in magento root directory:
5+
To install extension start with the following in magento root directory to add repository:
6+
```console
7+
composer config repositories.webscale-networks-api git https://github.com/webscale-networks/magento-varnish-api.git
8+
```
9+
10+
To avoid issues with CI/CD and github add `"no-api": true` to the repo settings, so it looks like this:
11+
```console
12+
"webscale-networks-api": {
13+
"type": "git",
14+
"url": "https://github.com/webscale-networks/magento-varnish-api.git",
15+
"no-api": true
16+
}
17+
```
618

19+
Now require extension itself:
720
```console
8-
composer config repositories.webscale-networks git https://github.com/webscale-networks/magento-varnish-api.git
921
composer require webscale-networks/magento-varnish-api
1022
```
1123

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": [
66
"MIT"
77
],
8-
"version": "1.0.0",
8+
"version": "1.1.0",
99
"require": {
1010
"php": "^7.2 || ^8.1",
1111
"magento/magento2-base": "2.3.* || 2.4.*"

etc/adminhtml/system.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,23 @@
3838
<label>Application Id</label>
3939
</field>
4040
</group>
41-
<group id="developer" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
41+
<group id="cache_events" translate="label" type="text" sortOrder="25" showInDefault="1" showInWebsite="0" showInStore="0">
42+
<label>Cache Events</label>
43+
<depends>
44+
<field id="webscale_varnish/general/enabled">1</field>
45+
</depends>
46+
<field id="flush_all_events" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1">
47+
<label>Flush All Events</label>
48+
<source_model>Webscale\Varnish\Model\Config\Source\FlushAllEvents</source_model>
49+
<can_be_empty>1</can_be_empty>
50+
</field>
51+
<field id="partial_invalidate_events" translate="label" type="multiselect" sortOrder="2" showInDefault="1" showInWebsite="1" canRestore="1">
52+
<label>Partial Invalidate Events</label>
53+
<source_model>Webscale\Varnish\Model\Config\Source\InvalidateEvents</source_model>
54+
<can_be_empty>1</can_be_empty>
55+
</field>
56+
</group>
57+
<group id="developer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
4258
<label>Developer</label>
4359
<depends>
4460
<field id="webscale_varnish/general/enabled">1</field>

0 commit comments

Comments
 (0)