Elgg  Version master
MetadataCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
9 use Elgg\Values;
10 
17 
23  public function __construct(protected BaseCache $cache) {
24  }
25 
38  public function inject($entity_guid, array $values = []) {
39  $metadata = [];
40  foreach ($values as $key => $value) {
41  if ($value instanceof \ElggMetadata) {
42  $md = $value;
43  } else {
44  $md = new \ElggMetadata();
45  $md->name = $key;
46  $md->value = $value;
47  $md->entity_guid = $entity_guid;
48  }
49 
50  $metadata[] = $md->toObject();
51  }
52 
53  $this->cache->save($entity_guid, $metadata);
54  }
55 
62  public function getAll($entity_guid) {
64  if (empty($metadata)) {
65  return [];
66  }
67 
68  $metadata_values = [];
69 
70  foreach ($metadata as $md) {
71  $metadata_values[$md->name][] = $md->value;
72  }
73 
74  return array_map(function($values) {
75  return count($values) > 1 ? $values : $values[0];
76  }, $metadata_values);
77  }
78 
92  public function getSingle($entity_guid, $name) {
94  if (empty($metadata)) {
95  return null;
96  }
97 
98  $values = [];
99 
100  foreach ($metadata as $md) {
101  if ($md->name !== $name) {
102  continue;
103  }
104 
105  $values[] = $md->value;
106  }
107 
108  if (empty($values)) {
109  return null;
110  }
111 
112  return count($values) > 1 ? $values : $values[0];
113  }
114 
128  public function getSingleId($entity_guid, $name) {
130  if (empty($metadata)) {
131  return null;
132  }
133 
134  $ids = [];
135 
136  foreach ($metadata as $md) {
137  if ($md->name !== $name) {
138  continue;
139  }
140 
141  $ids[] = $md->id;
142  }
143 
144  if (empty($ids)) {
145  return null;
146  }
147 
148  return count($ids) > 1 ? $ids : $ids[0];
149  }
150 
158  public function clear($entity_guid) {
159  $this->invalidateByOptions([
160  'guid' => $entity_guid,
161  ]);
162  }
163 
171  public function isLoaded($entity_guid) {
172  return $this->cache->load($entity_guid) !== null;
173  }
174 
180  public function clearAll() {
181  $this->invalidateByOptions([]);
182  }
183 
191  public function getEntityMetadata($entity_guid) {
192  $entity_guid = (int) $entity_guid;
193  $metadata = $this->cache->load($entity_guid);
194  if ($metadata === null) {
196  }
197 
198  return $metadata;
199  }
200 
208  public function invalidateByOptions(array $options) {
209  if (empty($options['guid'])) {
210  _elgg_services()->sessionCache->clear();
211  _elgg_services()->dataCache->clear();
212  } else {
213  _elgg_services()->entityTable->invalidateCache($options['guid']);
214  }
215  }
216 
223  public function populateFromEntities(...$guids) {
224  try {
226  } catch (DataFormatException $e) {
227  return null;
228  }
229 
230  if (empty($guids)) {
231  return null;
232  }
233 
234  $cached_values = [];
235 
236  foreach ($guids as $i => $guid) {
237  $value = $this->cache->load($guid);
238  if ($value !== null) {
239  $cached_values[$guid] = $value;
240  unset($guids[$i]);
241  }
242  }
243 
244  if (empty($guids)) {
245  return $cached_values;
246  }
247 
248  $data = _elgg_services()->metadataTable->getRowsForGuids($guids);
249 
250  $values = [];
251  foreach ($data as $row) {
252  $values[$row->entity_guid][] = $row;
253  }
254 
255  // store always for every guid, even if there is no metadata
256  foreach ($guids as $guid) {
257  $metadata = elgg_extract($guid, $values, []);
258 
259  $this->cache->save($guid, $metadata);
260  $cached_values[$guid] = $metadata;
261  }
262 
263  return $cached_values;
264  }
265 
276  public function filterMetadataHeavyEntities(array $guids, $limit = 1024000) {
277  $main_alias = MetadataTable::DEFAULT_JOIN_ALIAS;
278 
279  $guids = _elgg_services()->metadataTable->getAll([
280  'guids' => $guids,
281  'limit' => false,
282  'callback' => function($e) {
283  return (int) $e->entity_guid;
284  },
285  'selects' => ["SUM(LENGTH({$main_alias}.value)) AS bytes"],
286  'order_by' => [
287  new OrderByClause("{$main_alias}.entity_guid"),
288  new OrderByClause("{$main_alias}.time_created"),
289  ],
290  'group_by' => [
291  new GroupByClause("{$main_alias}.entity_guid"),
292  ],
293  'having' => [
294  "bytes < {$limit}",
295  ]
296  ]);
297 
298  return $guids ?: [];
299  }
300 }
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.
$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($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
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:351
__construct(protected BaseCache $cache)
Constructor.
$guid
Reset an ElggUpgrade.
Definition: reset.php:6