Elgg  Version 3.0
EntityCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
5 use ElggCache;
6 use ElggEntity;
7 use ElggSession;
8 
14 class EntityCache {
15 
16  const MAX_SIZE = 256;
17 
21  private $session;
22 
26  private $cache;
27 
31  private $size = 0;
32 
39  public function __construct(ElggSession $session, ElggCache $cache) {
40  $this->session = $session;
41  $this->cache = $cache;
42  }
43 
51  public function load($guid) {
52  return $this->cache->load((int) $guid);
53  }
54 
62  public function save(ElggEntity $entity) {
63  if (!$entity->guid || !$entity->isCacheable()) {
64  return;
65  }
66 
67  if ($this->size > self::MAX_SIZE) {
68  // Don't store too many or we'll have memory problems
69  return;
70  }
71 
72  $this->cache->save($entity->guid, $entity);
73  $this->size++;
74  }
75 
83  public function delete($guid) {
84  if (!$guid) {
85  return;
86  }
87 
88  $entity = $this->cache->load($guid);
89  if (!$entity instanceof ElggEntity) {
90  return;
91  }
92 
93  $this->cache->delete($guid);
94  $this->size--;
95  }
96 
102  public function clear() {
103  $this->cache->clear();
104  $this->size = 0;
105  }
106 
107 }
__construct(ElggSession $session, ElggCache $cache)
Constructor.
Definition: EntityCache.php:39
$guid
Removes an admin notice.
load($guid)
Retrieve a entity from the cache.
Definition: EntityCache.php:51
save(ElggEntity $entity)
Cache an entity.
Definition: EntityCache.php:62
$entity
Definition: reset.php:8
Volatile cache for entities.
Definition: EntityCache.php:14
clear()
Clear the entity cache.
isCacheable()
Is entity cacheable in the runtime cache.
elgg ElggEntity
Definition: ElggEntity.js:15