/
MetadataCache.php
267 lines (239 loc) · 6.72 KB
/
MetadataCache.php
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace Elgg\Cache;
use Elgg\Database\Clauses\OrderByClause;
use ElggSharedMemoryCache;
/**
* In memory cache of known metadata values stored by entity.
*
* @access private
*/
class MetadataCache {
/**
* The cached values (or null for known to be empty).
*
* @var array
*/
protected $values = [];
/**
* @var array
*/
protected $ids = [];
/**
* @var ElggSharedMemoryCache
*/
protected $cache;
/**
* Constructor
*
* @param ElggSharedMemoryCache $cache Cache
*/
public function __construct(ElggSharedMemoryCache $cache = null) {
if (!$cache) {
$cache = new NullCache();
}
$this->cache = $cache;
}
/**
* Set the visible metadata for an entity in the cache
*
* Note this does NOT invalidate any other part of the cache.
*
* @param int $entity_guid The GUID of the entity
* @param array $values The metadata values to cache
* @param array $ids The metadata ids
* @return void
*
* @access private For testing only
*/
public function inject($entity_guid, array $values, array $ids = []) {
$this->values[$entity_guid] = $values;
$this->ids[$entity_guid] = $ids;
}
/**
* Get the metadata for a particular name. Note, this can return an array of values.
*
* Warning: You should always call isLoaded() beforehand to verify that this
* function's return value can be trusted.
*
* @see isLoaded
*
* @param int $entity_guid The GUID of the entity
* @param string $name The metadata name
*
* @return array|string|int|null null = value does not exist
*/
public function getSingle($entity_guid, $name) {
if (isset($this->values[$entity_guid])
&& array_key_exists($name, $this->values[$entity_guid])) {
return $this->values[$entity_guid][$name];
} else {
return null;
}
}
/**
* Get the metadata id for a particular name
*
* Warning: You should always call isLoaded() beforehand to verify that this
* function's return value can be trusted.
*
* @see isLoaded
*
* @param int $entity_guid The GUID of the entity
* @param string $name The metadata name
*
* @return int[]|int|null
*/
public function getSingleId($entity_guid, $name) {
if (isset($this->ids[$entity_guid])
&& array_key_exists($name, $this->ids[$entity_guid])) {
return $this->ids[$entity_guid][$name];
} else {
return null;
}
}
/**
* Forget about all metadata for an entity.
*
* @param int $entity_guid The GUID of the entity
* @return void
*/
public function clear($entity_guid) {
unset($this->values[$entity_guid]);
unset($this->ids[$entity_guid]);
$this->cache->delete($entity_guid);
}
/**
* If true, getSingle() will return an accurate values from the DB
*
* @param int $entity_guid The GUID of the entity
* @return bool
*/
public function isLoaded($entity_guid) {
return array_key_exists($entity_guid, $this->values) && array_key_exists($entity_guid, $this->ids);
}
/**
* Clear entire cache
*
* @return void
*/
public function clearAll() {
foreach (array_keys($this->values) as $guid) {
$this->cache->delete($guid);
}
$this->values = [];
$this->ids = [];
}
/**
* Invalidate based on options passed to the global *_metadata functions
*
* @param array $options Options passed to elgg_(delete|disable|enable)_metadata
* "guid" if given, invalidation will be limited to this entity
* @return void
*/
public function invalidateByOptions(array $options) {
if (empty($options['guid'])) {
$this->clearAll();
_elgg_services()->entityCache->clear();
} else {
$this->clear($options['guid']);
_elgg_services()->entityCache->remove($options['guid']);
}
}
/**
* Populate the cache from a set of entities
*
* @param int|array $guids Array of or single GUIDs
* @return void
*/
public function populateFromEntities($guids) {
if (empty($guids)) {
return;
}
$version = (int) _elgg_config()->version;
if (!empty($version) && ($version < 2016110900)) {
// can't use this during upgrade from 2.x to 3.0
return;
}
$guids = array_unique((array) $guids);
foreach ($guids as $i => $guid) {
$value = $this->cache->load($guid);
if ($value !== false) {
$this->values[$guid] = unserialize($value);
unset($guids[$i]);
}
}
if (empty($guids)) {
return;
}
// could be useful at some point in future
//$guids = $this->filterMetadataHeavyEntities($guids);
$options = [
'guids' => $guids,
'limit' => 0,
'callback' => false,
'distinct' => false,
'order_by' => [
new OrderByClause('n_table.entity_guid', 'asc'),
new OrderByClause('n_table.time_created', 'asc'),
new OrderByClause('n_table.id', 'asc')
],
];
// We already have a loaded entity, so we can ignore entity access clauses
$ia = elgg_set_ignore_access(true);
$data = _elgg_services()->metadataTable->getAll($options);
elgg_set_ignore_access($ia);
// make sure we show all entities as loaded
foreach ($guids as $guid) {
$this->values[$guid] = null;
$this->ids[$guid] = null;
}
// build up metadata for each entity, save when GUID changes (or data ends)
foreach ($data as $i => $row) {
$id = $row->id;
$name = $row->name;
$value = ($row->value_type === 'text') ? $row->value : (int) $row->value;
$guid = $row->entity_guid;
if (isset($this->values[$guid][$name])) {
$this->values[$guid][$name] = (array) $this->values[$guid][$name];
$this->values[$guid][$name][] = $value;
} else {
$this->values[$guid][$name] = $value;
}
if (isset($this->ids[$guid][$name])) {
$this->ids[$guid][$name] = (array) $this->ids[$guid][$name];
$this->ids[$guid][$name][] = $id;
} else {
$this->ids[$guid][$name] = $id;
}
}
foreach ($guids as $guid) {
$this->cache->save($guid, serialize($this->values[$guid]));
}
}
/**
* Filter out entities whose concatenated metadata values (INTs casted as string)
* exceed a threshold in characters. This could be used to avoid overpopulating the
* cache if RAM usage becomes an issue.
*
* @param array $guids GUIDs of entities to examine
* @param int $limit Limit in characters of all metadata (with ints casted to strings)
* @return array
*/
public function filterMetadataHeavyEntities(array $guids, $limit = 1024000) {
$data = _elgg_services()->metadataTable->getAll([
'guids' => $guids,
'limit' => 0,
'callback' => false,
'selects' => ['SUM(LENGTH(n_table.value)) AS bytes'],
'order_by' => 'n_table.entity_guid, n_table.time_created ASC',
'group_by' => 'n_table.entity_guid',
]);
// don't cache if metadata for entity is over 10MB (or rolled INT)
foreach ($data as $row) {
if ($row->bytes > $limit || $row->bytes < 0) {
array_splice($guids, array_search($row->entity_guid, $guids), 1);
}
}
return $guids;
}
}