Elgg  Version 5.1
MetadataCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
8 use Elgg\Values;
9 
16 
20  protected $cache;
21 
27  public function __construct(BaseCache $cache) {
28  $this->cache = $cache;
29  }
30 
43  public function inject($entity_guid, array $values = []) {
44  $metadata = [];
45  foreach ($values as $key => $value) {
46  if ($value instanceof \ElggMetadata) {
47  $md = $value;
48  } else {
49  $md = new \ElggMetadata();
50  $md->name = $key;
51  $md->value = $value;
52  $md->entity_guid = $entity_guid;
53  }
54 
55  $metadata[] = $md->toObject();
56  }
57 
58  $this->cache->save($entity_guid, $metadata);
59  }
60 
67  public function getAll($entity_guid) {
69  if (empty($metadata)) {
70  return [];
71  }
72 
73  $metadata_values = [];
74 
75  foreach ($metadata as $md) {
76  $metadata_values[$md->name][] = $md->value;
77  }
78 
79  return array_map(function($values) {
80  return count($values) > 1 ? $values : $values[0];
81  }, $metadata_values);
82  }
83 
97  public function getSingle($entity_guid, $name) {
99  if (empty($metadata)) {
100  return null;
101  }
102 
103  $values = [];
104 
105  foreach ($metadata as $md) {
106  if ($md->name !== $name) {
107  continue;
108  }
109 
110  $values[] = $md->value;
111  }
112 
113  if (empty($values)) {
114  return null;
115  }
116 
117  return count($values) > 1 ? $values : $values[0];
118  }
119 
133  public function getSingleId($entity_guid, $name) {
135  if (empty($metadata)) {
136  return null;
137  }
138 
139  $ids = [];
140 
141  foreach ($metadata as $md) {
142  if ($md->name !== $name) {
143  continue;
144  }
145 
146  $ids[] = $md->id;
147  }
148 
149  if (empty($ids)) {
150  return null;
151  }
152 
153  return count($ids) > 1 ? $ids : $ids[0];
154  }
155 
163  public function clear($entity_guid) {
164  $this->invalidateByOptions([
165  'guid' => $entity_guid,
166  ]);
167  }
168 
176  public function isLoaded($entity_guid) {
177  return $this->cache->load($entity_guid) !== null;
178  }
179 
185  public function clearAll() {
186  $this->invalidateByOptions([]);
187  }
188 
196  public function getEntityMetadata($entity_guid) {
197  $entity_guid = (int) $entity_guid;
198  $metadata = $this->cache->load($entity_guid);
199  if ($metadata === null) {
201  }
202 
203  return $metadata;
204  }
205 
213  public function invalidateByOptions(array $options) {
214  if (empty($options['guid'])) {
215  _elgg_services()->sessionCache->clear();
216  _elgg_services()->dataCache->clear();
217  } else {
218  _elgg_services()->entityTable->invalidateCache($options['guid']);
219  }
220  }
221 
228  public function populateFromEntities(...$guids) {
229  try {
231  } catch (DataFormatException $e) {
232  return null;
233  }
234 
235  if (empty($guids)) {
236  return null;
237  }
238 
239  $cached_values = [];
240 
241  foreach ($guids as $i => $guid) {
242  $value = $this->cache->load($guid);
243  if ($value !== null) {
244  $cached_values[$guid] = $value;
245  unset($guids[$i]);
246  }
247  }
248 
249  if (empty($guids)) {
250  return $cached_values;
251  }
252 
253  $data = _elgg_services()->metadataTable->getRowsForGuids($guids);
254 
255  $values = [];
256  foreach ($data as $row) {
257  $values[$row->entity_guid][] = $row;
258  }
259 
260  // store always for every guid, even if there is no metadata
261  foreach ($guids as $guid) {
262  $metadata = elgg_extract($guid, $values, []);
263 
264  $this->cache->save($guid, $metadata);
265  $cached_values[$guid] = $metadata;
266  }
267 
268  return $cached_values;
269  }
270 
281  public function filterMetadataHeavyEntities(array $guids, $limit = 1024000) {
282  $guids = _elgg_services()->metadataTable->getAll([
283  'guids' => $guids,
284  'limit' => false,
285  'callback' => function($e) {
286  return (int) $e->entity_guid;
287  },
288  'selects' => ['SUM(LENGTH(n_table.value)) AS bytes'],
289  'order_by' => [
290  new OrderByClause('n_table.entity_guid'),
291  new OrderByClause('n_table.time_created'),
292  ],
293  'group_by' => [
294  new GroupByClause('n_table.entity_guid'),
295  ],
296  'having' => [
297  "bytes < $limit",
298  ]
299  ]);
300 
301  return $guids ?: [];
302  }
303 }
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
Definition: Values.php:141
invalidateByOptions(array $options)
Invalidate based on options passed to the global *_metadata functions.
Saves user notification settings.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
clear($entity_guid)
Forget about all metadata for an entity.
Extends QueryBuilder with GROUP BY statements.
getEntityMetadata($entity_guid)
Returns loaded entity metadata.
isLoaded($entity_guid)
If true, getSingle() will return an accurate values from the DB.
getSingle($entity_guid, $name)
Get the metadata for a particular name.
$options
Elgg admin footer.
Definition: footer.php:6
$value
Definition: generic.php:51
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
clearAll()
Clear entire cache.
$limit
Definition: pagination.php:28
ElggMetadata.
The Elgg cache base class.
Definition: BaseCache.php:9
inject($entity_guid, array $values=[])
Set the visible metadata for an entity in the cache.
getSingleId($entity_guid, $name)
Get the metadata id for a particular name.
filterMetadataHeavyEntities(array $guids, $limit=1024000)
Filter out entities whose concatenated metadata values (INTs casted as string) exceed a threshold in ...
getAll($entity_guid)
Get all entity metadata.
Extends QueryBuilder with ORDER BY clauses.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
populateFromEntities(...$guids)
Populate the cache from a set of entities.
An exception thrown when there is a problem in the format of some data.
$metadata
Output annotation metadata.
Definition: metadata.php:9
In memory cache of known metadata values stored by entity.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
__construct(BaseCache $cache)
Constructor.
$guid
Reset an ElggUpgrade.
Definition: reset.php:6