From c7877d236c750abde0cf06d71cf320925307caf5 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 27 Nov 2017 13:55:38 -0500 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20bust=20the=20cache=20for=20elem?= =?UTF-8?q?ents=20that=20are=20not=20ENABLED=20or=20LIVE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Welch --- CHANGELOG.md | 5 +++++ composer.json | 2 +- src/FastcgiCacheBust.php | 28 +++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49f7434..2880df4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # FastCGI Cache Bust Changelog +## 1.0.3 - 2017.11.27 +### Changed +* Don't bust the FastCGI Cache unless the element being saved is ENABLED or LIVE +* Don't bust the FastCGI Cache for certain custom ElementTypes + ## 1.0.2 - 2017.08.12 ### Changed * Added 'FastCGI Cache' to the list of things that can be cleared via Craft's Clear Caches tool diff --git a/composer.json b/composer.json index e6dc7fd..541d2cd 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft3-fastcgicachebust", "description": "Bust the Nginx FastCGI Cache when entries are saved or created.", "type": "craft-plugin", - "version": "1.0.2", + "version": "1.0.3", "keywords": [ "craft", "cms", diff --git a/src/FastcgiCacheBust.php b/src/FastcgiCacheBust.php index 3951248..6e0ace8 100644 --- a/src/FastcgiCacheBust.php +++ b/src/FastcgiCacheBust.php @@ -14,7 +14,9 @@ use nystudio107\fastcgicachebust\models\Settings; use Craft; +use craft\base\Element; use craft\base\Plugin; +use craft\elements\Entry; use craft\events\ElementEvent; use craft\events\RegisterCacheOptionsEvent; use craft\services\Elements; @@ -61,7 +63,31 @@ function (ElementEvent $event) { 'Elements::EVENT_AFTER_SAVE_ELEMENT', __METHOD__ ); - FastcgiCacheBust::$plugin->cache->clearAll(); + /** @var Element $element */ + $element = $event->element; + $isNewElement = $event->isNew; + $bustCache = true; + // Only bust the cache if the element is ENABLED or LIVE + if (($element->getStatus() != Element::STATUS_ENABLED) + && ($element->getStatus() != Entry::STATUS_LIVE) + ) { + $bustCache = false; + } + // Only bust the cache if it's not certain excluded element types + /* @TODO: These need to be updated once the plugins are released for Craft 3 + if (($element instanceof 'SproutSeo_Redirect') + || ($element instanceof 'PushNotifications_Device') + ) { + $bustCache = false; + } + */ + if ($bustCache) { + Craft::trace( + "Cache busted due to saving: " . $element::className() . " - " . $element->title, + __METHOD__ + ); + FastcgiCacheBust::$plugin->cache->clearAll(); + } } );