|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * A simple, unified cache handler (last modified: 2023.02.23). |
| 3 | + * A simple, unified cache handler (last modified: 2024.05.30). |
4 | 4 | * |
5 | 5 | * This file is a part of the "common classes package", utilised by a number of |
6 | 6 | * packages and projects, including CIDRAM and phpMussel. |
@@ -62,6 +62,12 @@ class Cache |
62 | 62 | */ |
63 | 63 | public $RedisTimeout = 2.5; |
64 | 64 |
|
| 65 | + /** |
| 66 | + * @var int Which database number Redis should select. |
| 67 | + * @link https://redis.io/commands/select/ |
| 68 | + */ |
| 69 | + public $RedisDatabaseNumber = 0; |
| 70 | + |
65 | 71 | /** |
66 | 72 | * @var string The DSN to use for PDO connections. |
67 | 73 | */ |
@@ -180,15 +186,15 @@ class Cache |
180 | 186 | * be needed by some implementations to ensure compatibility). |
181 | 187 | * @link https://github.com/Maikuolan/Common/tags |
182 | 188 | */ |
183 | | - const VERSION = '1.9.5'; |
| 189 | + const VERSION = '1.11.0'; |
184 | 190 |
|
185 | 191 | /** |
186 | 192 | * Construct object and set working data if needed. |
187 | 193 | * |
188 | 194 | * @param array|null $WorkingData An optional array of default cache data. |
189 | 195 | * @return void |
190 | 196 | */ |
191 | | - public function __construct(array $WorkingData = null) |
| 197 | + public function __construct($WorkingData = null) |
192 | 198 | { |
193 | 199 | if (is_array($WorkingData)) { |
194 | 200 | $this->WorkingData = $WorkingData; |
@@ -281,6 +287,12 @@ public function connect() |
281 | 287 | $this->WorkingData = new \Redis(); |
282 | 288 | if ($this->WorkingData->connect($this->RedisHost, $this->RedisPort, $this->RedisTimeout)) { |
283 | 289 | $this->Using = 'Redis'; |
| 290 | + if ($this->RedisDatabaseNumber !== 0) { |
| 291 | + $this->WorkingData->select($this->RedisDatabaseNumber); |
| 292 | + if ($this->WorkingData->getDbNum() !== $this->RedisDatabaseNumber) { |
| 293 | + return false; |
| 294 | + } |
| 295 | + } |
284 | 296 | return true; |
285 | 297 | } |
286 | 298 | $this->WorkingData = null; |
@@ -347,13 +359,13 @@ public function connect() |
347 | 359 | public function checkTablesPDO() |
348 | 360 | { |
349 | 361 | /** Try to determine which kind of query to build. */ |
350 | | - if (preg_match('~^sqlite\:[^\:]~i', $this->PDOdsn)) { |
| 362 | + if (preg_match('~^sqlite:[^:]~i', $this->PDOdsn)) { |
351 | 363 | /** SQLite (excluding usage for in-memory and temporary tables). */ |
352 | 364 | $Check = 'SELECT count(*) FROM `sqlite_master` WHERE `type` = \'table\' AND `name` = \'Cache\''; |
353 | | - } elseif (preg_match('~^informix\:~i', $this->PDOdsn)) { |
| 365 | + } elseif (preg_match('~^informix:~i', $this->PDOdsn)) { |
354 | 366 | /** Informix. */ |
355 | 367 | $Check = 'SELECT count(*) FROM `systables` WHERE `tabname` = \'Cache\''; |
356 | | - } elseif (preg_match('~^firebird\:~i', $this->PDOdsn)) { |
| 368 | + } elseif (preg_match('~^firebird:~i', $this->PDOdsn)) { |
357 | 369 | /** Firebird/Interbase. */ |
358 | 370 | $Check = 'SELECT 1 FROM RDB$RELATIONS WHERE RDB$RELATION_NAME = \'Cache\''; |
359 | 371 | } else { |
@@ -637,13 +649,16 @@ public function incEntry($Key, $Value = 1, $TTL = 0) |
637 | 649 | $Success = ($PDO->rowCount() > 0); |
638 | 650 | } |
639 | 651 | } elseif (is_array($this->WorkingData)) { |
640 | | - if ( |
641 | | - isset($this->WorkingData[$Key]) && |
642 | | - is_array($this->WorkingData[$Key]) && |
643 | | - isset($this->WorkingData[$Key]['Data']) && |
644 | | - is_numeric($this->WorkingData[$Key]['Data']) |
645 | | - ) { |
646 | | - $Value += $this->WorkingData[$Key]['Data']; |
| 652 | + if (isset($this->WorkingData[$Key])) { |
| 653 | + if ( |
| 654 | + is_array($this->WorkingData[$Key]) && |
| 655 | + isset($this->WorkingData[$Key]['Data']) && |
| 656 | + is_numeric($this->WorkingData[$Key]['Data']) |
| 657 | + ) { |
| 658 | + $Value += $this->WorkingData[$Key]['Data']; |
| 659 | + } elseif (is_numeric($this->WorkingData[$Key])) { |
| 660 | + $Value += $this->WorkingData[$Key]; |
| 661 | + } |
647 | 662 | } |
648 | 663 | if ($TTL > 0) { |
649 | 664 | $TTL += time(); |
@@ -699,13 +714,16 @@ public function decEntry($Key, $Value = 1, $TTL = 0) |
699 | 714 | $Success = ($PDO->rowCount() > 0); |
700 | 715 | } |
701 | 716 | } elseif (is_array($this->WorkingData)) { |
702 | | - if ( |
703 | | - isset($this->WorkingData[$Key]) && |
704 | | - is_array($this->WorkingData[$Key]) && |
705 | | - isset($this->WorkingData[$Key]['Data']) && |
706 | | - is_numeric($this->WorkingData[$Key]['Data']) |
707 | | - ) { |
708 | | - $Value -= $this->WorkingData[$Key]['Data']; |
| 717 | + if (isset($this->WorkingData[$Key])) { |
| 718 | + if ( |
| 719 | + is_array($this->WorkingData[$Key]) && |
| 720 | + isset($this->WorkingData[$Key]['Data']) && |
| 721 | + is_numeric($this->WorkingData[$Key]['Data']) |
| 722 | + ) { |
| 723 | + $Value -= $this->WorkingData[$Key]['Data']; |
| 724 | + } elseif (is_numeric($this->WorkingData[$Key])) { |
| 725 | + $Value -= $this->WorkingData[$Key]; |
| 726 | + } |
709 | 727 | } |
710 | 728 | if ($TTL > 0) { |
711 | 729 | $TTL += time(); |
@@ -1066,7 +1084,7 @@ public function clearExpiredPDO() |
1066 | 1084 | */ |
1067 | 1085 | public function unserializeEntry($Entry) |
1068 | 1086 | { |
1069 | | - if (!is_string($Entry) || !preg_match('~^a\:\d+\:\{.*\}$~', $Entry)) { |
| 1087 | + if (!is_string($Entry) || !preg_match('~^a:\d+:\{.*\}$~', $Entry)) { |
1070 | 1088 | return $Entry; |
1071 | 1089 | } |
1072 | 1090 | $Arr = unserialize($Entry); |
|
0 commit comments