Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
SystemCache.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Cache;
3 
5 use Elgg\Config;
6 use ElggFileCache;
8 
18 class SystemCache {
19  use Profilable;
20 
24  private $config;
25 
29  private $cache;
30 
34  private $datalist;
35 
43  public function __construct(ElggFileCache $cache, Config $config, Datalist $datalist) {
44  $this->cache = $cache;
45  $this->config = $config;
46  $this->datalist = $datalist;
47  }
48 
54  function reset() {
55  $this->cache->clear();
56  }
57 
65  function save($type, $data) {
66  if ($this->isEnabled()) {
67  return $this->cache->save($type, $data);
68  }
69 
70  return false;
71  }
72 
79  function load($type) {
80  if ($this->isEnabled()) {
81  $cached_data = $this->cache->load($type);
82  if ($cached_data) {
83  return $cached_data;
84  }
85  }
86 
87  return null;
88  }
89 
95  function isEnabled() {
96  return (bool)$this->config->getVolatile('system_cache_enabled');
97  }
98 
107  function enable() {
108  $this->datalist->set('system_cache_enabled', 1);
109  $this->config->set('system_cache_enabled', 1);
110  $this->reset();
111  }
112 
121  function disable() {
122  $this->datalist->set('system_cache_enabled', 0);
123  $this->config->set('system_cache_enabled', 0);
124  $this->reset();
125  }
126 
133  function loadAll() {
134  if ($this->timer) {
135  $this->timer->begin([__METHOD__]);
136  }
137 
138  $this->config->set('system_cache_loaded', false);
139 
140  if (!_elgg_services()->views->configureFromCache($this)) {
141  return;
142  }
143 
144  $data = $this->load('view_types');
145  if (!is_string($data)) {
146  return;
147  }
148  $GLOBALS['_ELGG']->view_types = unserialize($data);
149 
150  // Note: We don't need view_overrides for operation. Inspector can pull this from the cache
151 
152  $this->config->set('system_cache_loaded', true);
153 
154  if ($this->timer) {
155  $this->timer->end([__METHOD__]);
156  }
157  }
158 
165  function init() {
166  if (!$this->isEnabled()) {
167  return;
168  }
169 
170  // cache system data if enabled and not loaded
171  if (!$this->config->getVolatile('system_cache_loaded')) {
172  $this->save('view_types', serialize($GLOBALS['_ELGG']->view_types));
173 
174  _elgg_services()->views->cacheConfiguration($this);
175  }
176 
177  if (!$GLOBALS['_ELGG']->i18n_loaded_from_cache) {
178 
179  _elgg_services()->translator->reloadAllTranslations();
180 
181  foreach ($GLOBALS['_ELGG']->translations as $lang => $map) {
182  $this->save("$lang.lang", serialize($map));
183  }
184  }
185  }
186 }
$lang
Definition: html.php:12
reset()
Reset the system cache by deleting the caches.
Definition: SystemCache.php:54
isEnabled()
Is system cache enabled.
Definition: SystemCache.php:95
__construct(ElggFileCache $cache, Config $config, Datalist $datalist)
Constructor.
Definition: SystemCache.php:43
$_ELGG translations
String translations for the current language.
Definition: config.php:321
$data
Definition: opendd.php:13
Access to configuration values.
Definition: Config.php:11
trait Profilable
Make an object accept a timer.
Definition: Profilable.php:9
$_ELGG view_types
A list of valid view types as discovered.
Definition: config.php:349
enable()
Enables the system disk cache.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
loadAll()
Loads the system cache during engine boot.
save($type, $data)
Saves a system cache.
Definition: SystemCache.php:65
init()
Initializes the simplecache lastcache variable and creates system cache files when appropriate...
disable()
Disables the system disk cache.
load($type)
Retrieve the contents of a system cache.
Definition: SystemCache.php:79
if(!$display_name) $type
Definition: delete.php:27