Elgg  Version 6.1
MetadataCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
5 use Elgg\Config;
7 use Elgg\Values;
8 
14 class MetadataCache extends CacheService {
15 
21  public function __construct(protected Config $config) {
23 
24  $this->cache = new CompositeCache('metadata_cache', $this->config, $flags);
25  }
26 
33  public function populateFromEntities(...$guids): ?array {
34  try {
36  } catch (DataFormatException $e) {
37  return null;
38  }
39 
40  if (empty($guids)) {
41  return null;
42  }
43 
44  $cached_values = [];
45 
46  foreach ($guids as $i => $guid) {
47  $value = $this->cache->load($guid);
48  if ($value !== null) {
49  $cached_values[$guid] = $value;
50  unset($guids[$i]);
51  }
52  }
53 
54  if (empty($guids)) {
55  return $cached_values;
56  }
57 
58  $data = _elgg_services()->metadataTable->getRowsForGuids($guids);
59 
60  $values = [];
61  foreach ($data as $row) {
62  $values[$row->entity_guid][] = $row;
63  }
64 
65  // store always for every guid, even if there is no metadata
66  foreach ($guids as $guid) {
67  $metadata = elgg_extract($guid, $values, []);
68 
69  $this->cache->save($guid, $metadata);
70  $cached_values[$guid] = $metadata;
71  }
72 
73  return $cached_values;
74  }
75 }
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
Definition: Values.php:141
__construct(protected Config $config)
Constructor.
Saves user notification settings.
$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:256
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
Composite cache pool.
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
populateFromEntities(...$guids)
Populate the cache from a set of entities.
$metadata
Output annotation metadata.
Definition: metadata.php:9
An exception thrown when there is a problem in the format of some data.
In memory cache of known metadata values stored by entity.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
$guid
Reset an ElggUpgrade.
Definition: reset.php:6