From 6d479ba52c215df7bf152010e78bbee0e9cd58f8 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 19 Sep 2024 11:45:06 -0400 Subject: [PATCH] Update WireCacheDatabase to improve the efficiency of the general cache maintenance by adding a custom maintenance() method rather than relying on the slower one in WireCache. This also corrects the issue of past caches with an 0000-00-00 expiration date that would never expire, ensuring they don't stick around any longer. --- wire/core/WireCacheDatabase.php | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/wire/core/WireCacheDatabase.php b/wire/core/WireCacheDatabase.php index 8c0c9905..42db4ad9 100644 --- a/wire/core/WireCacheDatabase.php +++ b/wire/core/WireCacheDatabase.php @@ -3,7 +3,7 @@ /** * Database cache handler for WireCache * - * ProcessWire 3.x, Copyright 2023 by Ryan Cramer + * ProcessWire 3.x, Copyright 2024 by Ryan Cramer * https://processwire.com * * @since 2.0.218 @@ -205,6 +205,38 @@ class WireCacheDatabase extends Wire implements WireCacheInterface { return $result; } + /** + * Database cache maintenance (every 10 minutes) + * + * @param Template|Page $obj + * @return bool + * @throws WireException + * @since 3.0.242 + * + */ + public function maintenance($obj) { + + if($obj) return false; // let WireCache handle when object value is provided + + $sql = + 'DELETE FROM caches ' . + 'WHERE (expires<=:now AND expires>:never) ' . + 'OR expires<:then'; + + $query = $this->wire()->database->prepare($sql); + $query->bindValue(':now', date(WireCache::dateFormat, time())); + $query->bindValue(':never', WireCache::expireNever); + $query->bindValue(':then', '1974-10-10 10:10:10'); + $query->execute(); + $qty = $query->rowCount(); + + if($qty) $this->wire->cache->log( + sprintf('DB cache maintenance expired %d cache(s)', $qty) + ); + + return $qty > 0; + } + /** * Create the caches table if it happens to have been deleted *