1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-07 15:26:54 +02:00

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.

This commit is contained in:
Ryan Cramer
2024-09-19 11:45:06 -04:00
parent fef2a76f39
commit 6d479ba52c

View File

@@ -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
*