From 02e3e0cd1b14ed958d0df969af4f1923719e5f18 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Tue, 30 May 2023 15:28:03 -0400 Subject: [PATCH] Add support for a WireCacheInterface::maintenance($obj) method as mentioned by @BitPoet --- wire/core/Interfaces.php | 23 +++++++++++++++++++++++ wire/core/WireCache.php | 17 +++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/wire/core/Interfaces.php b/wire/core/Interfaces.php index da4bacef..88829507 100644 --- a/wire/core/Interfaces.php +++ b/wire/core/Interfaces.php @@ -796,4 +796,27 @@ interface WireCacheInterface { * */ public function expireAll(); + + /** + * Optional method to perform maintenance + * + * When present, this method should return true if it handled maintenance or false if it did not. + * If it returns false, WireCache will attempt to perform maintenance instead by calling find and + * delete methods where appropriate. + * + * WireCache passes either null, a Page object or a Template object as the single argument. + * When null is passed, it means "general maintenance". When a Page or Template object is + * passed then it means that the given Page or Template was just saved, and to perform any + * necessary maintenance for that case. If the method handles general maintenance but not + * object maintenance, then it should return true when it receives null, and false when it + * receives a Page or Template. + * + * @param Page|Template|null $obj + * @return bool + * @since 3.0.219 + * + * The method below is commented out because it optional and only used only if present: + * + */ + // public function maintenance($obj = null); } diff --git a/wire/core/WireCache.php b/wire/core/WireCache.php index c7436d94..b9284f4f 100644 --- a/wire/core/WireCache.php +++ b/wire/core/WireCache.php @@ -799,13 +799,19 @@ class WireCache extends Wire { $forceRun = false; $database = $this->wire()->database; $config = $this->wire()->config; + $cacher = $this->cacher(); + $useCacherMaint = method_exists($cacher, 'maintenance'); if(!$database || !$config) return false; if(is_object($obj)) { + if($useCacherMaint) { + $result = call_user_func_array(array($cacher, 'maintenance'), array($obj)); + if($result) return true; + } // check to see if it is worthwhile to perform this kind of maintenance at all if($this->usePageTemplateMaintenance === null) { - $rows = $this->cacher()->find(array( + $rows = $cacher->find(array( 'get' => array('name'), 'expiresMode' => 'OR', 'expires' => array( @@ -863,7 +869,14 @@ class WireCache extends Wire { } // perform general maintenance now - return $this->maintenanceGeneral(); + if($useCacherMaint) { + $result = (bool) call_user_func_array(array($cacher, 'maintenance'), array(null)); + } else { + $result = $this->maintenanceGeneral(); + } + + return $result; + } /**