1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg\Cache; |
4
|
|
|
|
5
|
|
|
use Elgg\Loggable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Volatile cache for select queries |
9
|
|
|
* |
10
|
|
|
* Queries and their results are stored in this cache as: |
11
|
|
|
* <code> |
12
|
|
|
* $DB_QUERY_CACHE[query hash] => array(result1, result2, ... resultN) |
13
|
|
|
* </code> |
14
|
|
|
* |
15
|
|
|
* @access private |
16
|
|
|
*/ |
17
|
|
|
class QueryCache extends LRUCache { |
18
|
|
|
use Loggable; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool Is this cache disabled by a config flag |
22
|
|
|
*/ |
23
|
|
|
protected $config_disabled = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool Is this cache disabled during runtime |
27
|
|
|
*/ |
28
|
|
|
protected $runtime_enabled = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
* |
33
|
|
|
* @param int $size max items in LRU cache |
34
|
|
|
* @param bool $config_disabled is this cache allowed to be used |
35
|
|
|
*/ |
36
|
4888 |
|
public function __construct(int $size = 50, bool $config_disabled = false) { |
37
|
|
|
|
38
|
4888 |
|
$this->config_disabled = $config_disabled; |
39
|
|
|
|
40
|
4888 |
|
parent::__construct($size); |
41
|
4888 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Enable the query cache |
45
|
|
|
* |
46
|
|
|
* This does not take precedence over the \Elgg\Database\Config setting. |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
5223 |
|
public function enable() { |
51
|
5223 |
|
$this->runtime_enabled = true; |
52
|
5223 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Disable the query cache |
56
|
|
|
* |
57
|
|
|
* This is useful for special scripts that pull large amounts of data back |
58
|
|
|
* in single queries. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
366 |
|
public function disable() { |
63
|
366 |
|
$this->runtime_enabled = false; |
64
|
|
|
|
65
|
366 |
|
$this->clear(); |
66
|
366 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks if this cache is enabled |
70
|
|
|
* |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
1265 |
|
public function isEnabled() { |
74
|
1265 |
|
if ($this->config_disabled) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
1265 |
|
return $this->runtime_enabled; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
5648 |
|
public function clear() { |
85
|
5648 |
|
parent::clear(); |
86
|
|
|
|
87
|
5648 |
|
if ($this->logger) { |
88
|
5648 |
|
$this->logger->info('Query cache invalidated'); |
89
|
|
|
} |
90
|
5648 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
1265 |
|
public function get($key, $default = null) { |
96
|
1265 |
|
if (!$this->isEnabled()) { |
97
|
|
|
return $default; |
98
|
|
|
} |
99
|
|
|
|
100
|
1265 |
|
$result = parent::get($key, $default); |
101
|
|
|
|
102
|
1265 |
|
if ($this->logger) { |
103
|
1264 |
|
$this->logger->info("DB query results returned from cache (hash: $key)"); |
104
|
|
|
} |
105
|
|
|
|
106
|
1265 |
|
return $result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
1219 |
|
public function set($key, $value) { |
113
|
1219 |
|
if (!$this->isEnabled()) { |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
1219 |
|
parent::set($key, $value); |
118
|
|
|
|
119
|
1219 |
|
if ($this->logger) { |
120
|
1218 |
|
$this->logger->info("DB query results cached (hash: $key)"); |
121
|
|
|
} |
122
|
1219 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns a hashed key for storage in the cache |
126
|
|
|
* |
127
|
|
|
* @param string $sql query |
128
|
|
|
* @param array $params optional params |
129
|
|
|
* @param string $extras optional extras |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
1265 |
|
public function getHash(string $sql, array $params = [], string $extras = '') { |
134
|
|
|
|
135
|
1265 |
|
$query_id = $sql . '|'; |
136
|
1265 |
|
if (!empty($params)) { |
137
|
1256 |
|
$query_id .= serialize($params) . '|'; |
138
|
|
|
} |
139
|
|
|
|
140
|
1265 |
|
$query_id .= $extras; |
141
|
|
|
|
142
|
|
|
// MD5 yields smaller mem usage for cache and cleaner logs |
143
|
1265 |
|
return md5($query_id); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|