Elgg  Version 2.3
EntityCache.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Cache;
3 
6 
12 class EntityCache {
13 
14  // @todo Pick a less arbitrary limit
15  const MAX_SIZE = 256;
16 
20  private $entities = [];
21 
25  private $disabled_guids = [];
26 
30  private $session;
31 
35  private $metadata_cache;
36 
40  private $username_cache = [];
41 
48  public function __construct(ElggSession $session, MetadataCache $metadata_cache) {
49  $this->session = $session;
50  $this->metadata_cache = $metadata_cache;
51 
52  $GLOBALS['ENTITY_CACHE'] = $this->entities;
53  }
54 
62  public function get($guid) {
63  $this->checkGlobal();
64 
65  $guid = (int) $guid;
66 
67  if (isset($this->entities[$guid]) && $this->entities[$guid]->isFullyLoaded()) {
68  return $this->entities[$guid];
69  }
70 
71  return false;
72  }
73 
80  public function getByUsername($username) {
81  if (isset($this->username_cache[$username])) {
82  return $this->get($this->username_cache[$username]);
83  }
84  return false;
85  }
86 
93  public function set(ElggEntity $entity) {
94  $this->checkGlobal();
95 
96  $guid = $entity->guid;
97 
98  if (!$guid || isset($this->entities[$guid]) || isset($this->disabled_guids[$guid])) {
99  // have it or not saved
100  return;
101  }
102 
103  // Don't cache non-plugin entities while access control is off, otherwise they could be
104  // exposed to users who shouldn't see them when control is re-enabled.
105  if (!($entity instanceof \ElggPlugin) && $this->session->getIgnoreAccess()) {
106  return;
107  }
108 
109  // Don't store too many or we'll have memory problems
110  if (count($this->entities) > self::MAX_SIZE) {
111  $this->remove(array_rand($this->entities));
112  }
113 
114  $this->entities[$guid] = $entity;
115  $GLOBALS['ENTITY_CACHE'] = $this->entities;
116 
117  if ($entity instanceof \ElggUser) {
118  $this->username_cache[$entity->username] = $entity->guid;
119  }
120  }
121 
128  public function remove($guid) {
129  $this->checkGlobal();
130 
131  $guid = (int)$guid;
132 
133  if (!isset($this->entities[$guid])) {
134  return;
135  }
136 
137  unset($this->entities[$guid]);
138  $GLOBALS['ENTITY_CACHE'] = $this->entities;
139 
140  $username = array_search($guid, $this->username_cache);
141  if ($username !== false) {
142  unset($this->username_cache[$username]);
143  }
144 
145  // Purge separate metadata cache. Original idea was to do in entity destructor, but that would
146  // have caused a bunch of unnecessary purges at every shutdown. Doing it this way we have no way
147  // to know that the expunged entity will be GCed (might be another reference living), but that's
148  // OK; the metadata will reload if necessary.
149  $this->metadata_cache->clear($guid);
150  }
151 
157  public function clear() {
158  $this->checkGlobal();
159  $this->entities = [];
160  $this->username_cache = [];
161  $GLOBALS['ENTITY_CACHE'] = $this->entities;
162  }
163 
172  public function disableCachingForEntity($guid) {
173  $this->remove($guid);
174  $this->disabled_guids[$guid] = true;
175  }
176 
183  public function enableCachingForEntity($guid) {
184  unset($this->disabled_guids[$guid]);
185  }
186 
193  private function checkGlobal() {
194  if (!isset($GLOBALS['ENTITY_CACHE']) || ($GLOBALS['ENTITY_CACHE'] !== $this->entities)) {
195  $GLOBALS['ENTITY_CACHE'] = $this->entities;
196  elgg_deprecated_notice('Do not access or write to the global $ENTITY_CACHE.', '2.2');
197  }
198  }
199 }
$username
Definition: delete.php:22
$guid
Removes an admin notice.
disableCachingForEntity($guid)
Remove this entity from the entity cache and make sure it is not re-added.
Volatile cache for entities.
Definition: EntityCache.php:12
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
getByUsername($username)
Returns cached user entity by username.
Definition: EntityCache.php:80
__construct(ElggSession $session, MetadataCache $metadata_cache)
Constructor.
Definition: EntityCache.php:48
clear()
Clear the entity cache.
In memory cache of known metadata values stored by entity.
$entity
Definition: delete.php:7
elgg ElggEntity
Definition: ElggEntity.js:16
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
enableCachingForEntity($guid)
Allow this entity to be stored in the entity cache.