-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathCache.php
More file actions
66 lines (57 loc) · 1.96 KB
/
Cache.php
File metadata and controls
66 lines (57 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Query;
/**
* Handles caching results
*/
class Cache
{
/** @var (string|int|null)[][][] Table data cache */
private array $tableCache = [];
/**
* Caches table data so Table does not require to issue
* SHOW TABLE STATUS again
*
* @param (string|int|null)[][] $tables information for tables of some databases
*/
public function cacheTableData(string $database, array $tables): void
{
// Note: This function must not use array_merge because numerical indices must be preserved.
// When an entry already exists for the database in cache, we merge the incoming data with existing data.
// The union operator appends elements from right to left unless they exists on the left already.
// Doing the union with incoming data on the left ensures that when we reread table status from DB,
// we overwrite whatever was in cache with the new data.
if (isset($this->tableCache[$database])) {
$this->tableCache[$database] = $tables + $this->tableCache[$database];
} else {
$this->tableCache[$database] = $tables;
}
}
/**
* Set an item in the cache
*/
public function cacheTableValue(string $db, string $table, string $key, string|int|null $value): void
{
$this->tableCache[$db][$table][$key] = $value;
}
/**
* Get a cached value from table cache.
*
* @param T $key
*
* @return (T is null ? (string|int|null)[] : (string|int|null))|null
*
* @template T of string|null
*/
public function getCachedTableContent(string $db, string $table, string|null $key = null): array|string|int|null
{
if ($key === null) {
return $this->tableCache[$db][$table] ?? null;
}
return $this->tableCache[$db][$table][$key] ?? null;
}
public function clearTableCache(): void
{
$this->tableCache = [];
}
}