Skip to content

Commit a8eb093

Browse files
committed
Cache handler patch.
Changelog excerpt: - Optimised the logic for when clearExpiredPDO triggers, which should improve performance a little for those using PDO for caching.
1 parent 8011af2 commit a8eb093

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ found at:
2929

3030
- [2026.03.18]: Use RedisException for failed Redis connections.
3131

32+
- [2026.03.22]: Optimised the logic for when clearExpiredPDO triggers, which
33+
should improve performance a little for those using PDO for caching.
34+
3235
=== Version/Release 2.15.0 ===
3336
MINOR RELEASE.
3437

src/Cache.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* A simple, unified cache handler (last modified: 2026.03.20).
3+
* A simple, unified cache handler (last modified: 2026.03.22).
44
*
55
* This file is a part of the "common classes package", utilised by a number of
66
* packages and projects, including CIDRAM and phpMussel.
@@ -144,7 +144,7 @@ class Cache extends CommonAbstract implements \ArrayAccess, \Countable
144144
/**
145145
* @var string Prepared get query for PDO.
146146
*/
147-
public const GET_QUERY = 'SELECT `Data` FROM `Cache` WHERE `Key` = :key LIMIT 1';
147+
public const GET_QUERY = 'SELECT * FROM `Cache` WHERE `Key` = :key LIMIT 1';
148148

149149
/**
150150
* @var string Prepared delete query for PDO.
@@ -460,11 +460,14 @@ public function getEntry(string $Entry)
460460
return $this->unserializeEntry($this->WorkingData->get($Entry));
461461
}
462462
if ($this->Using === 'PDO') {
463-
$this->clearExpiredPDO();
464463
$PDO = $this->WorkingData->prepare(self::GET_QUERY);
465464
if ($PDO !== false && $PDO->execute([':key' => $Entry])) {
466465
$Data = $PDO->fetch(\PDO::FETCH_ASSOC);
467-
if (!isset($Data['Data'])) {
466+
if (!isset($Data['Data'], $Data['Time'])) {
467+
return false;
468+
}
469+
if ($Data['Time'] > 0 && $Data['Time'] < \time()) {
470+
$this->clearExpiredPDO();
468471
return false;
469472
}
470473
if (\substr($Data['Data'], 0, 3) === 'gz:') {
@@ -1397,7 +1400,7 @@ public function offsetExists($Offset): bool
13971400
$PDO = $this->WorkingData->prepare(self::GET_QUERY);
13981401
if ($PDO !== false && $PDO->execute([':key' => $Entry])) {
13991402
$Data = $PDO->fetch(\PDO::FETCH_ASSOC);
1400-
return isset($Data['Data']);
1403+
return isset($Data['Data'], $Data['Time']) && !($Data['Time'] > 0 && $Data['Time'] < \time());
14011404
}
14021405
return false;
14031406
}

0 commit comments

Comments
 (0)