Elgg  Version 1.9
cache.php
Go to the documentation of this file.
1 <?php
10 /* Filepath Cache */
11 
22 
26  static $FILE_PATH_CACHE;
27 
28  if (!$FILE_PATH_CACHE) {
29  $FILE_PATH_CACHE = new ElggFileCache($CONFIG->dataroot . 'system_cache/');
30  }
31 
32  return $FILE_PATH_CACHE;
33 }
34 
41  $cache = elgg_get_system_cache();
42  $cache->clear();
43 }
44 
54 
55  if ($CONFIG->system_cache_enabled) {
56  $cache = elgg_get_system_cache();
57  return $cache->save($type, $data);
58  }
59 
60  return false;
61 }
62 
71 
72  if ($CONFIG->system_cache_enabled) {
73  $cache = elgg_get_system_cache();
74  $cached_data = $cache->load($type);
75 
76  if ($cached_data) {
77  return $cached_data;
78  }
79  }
80 
81  return null;
82 }
83 
94 
95  datalist_set('system_cache_enabled', 1);
96  $CONFIG->system_cache_enabled = 1;
98 }
99 
109  global $CONFIG;
110 
111  datalist_set('system_cache_enabled', 0);
112  $CONFIG->system_cache_enabled = 0;
114 }
115 
116 /* Simplecache */
117 
136 function elgg_register_simplecache_view($view_name) {
137  elgg_register_external_view($view_name, true);
138 }
139 
156  // handle file type passed with view name
157  if (($type === 'js' || $type === 'css') && 0 === strpos($view, $type . '/')) {
158  $view = substr($view, strlen($type) + 1);
159  }
160 
161  elgg_register_simplecache_view("$type/$view");
162  return _elgg_get_simplecache_root() . "$type/$view";
163 }
164 
165 
175  // stored in datalist as 'simplecache_lastupdate'
176  $lastcache = (int)elgg_get_config('lastcache');
177  } else {
178  $lastcache = 0;
179  }
180 
181  return elgg_normalize_url("/cache/$lastcache/$viewtype/");
182 }
183 
201  if (preg_match('~(?:^|/)(css|js)(?:$|/)~', $view, $m)) {
202  return $m[1];
203  } else {
204  return 'unknown';
205  }
206 }
207 
215  return (bool) elgg_get_config('simplecache_enabled');
216 }
217 
226  datalist_set('simplecache_enabled', 1);
227  elgg_set_config('simplecache_enabled', 1);
229 }
230 
241  if (elgg_get_config('simplecache_enabled')) {
242  datalist_set('simplecache_enabled', 0);
243  elgg_set_config('simplecache_enabled', 0);
244 
245  // purge simple cache
246  _elgg_rmdir(elgg_get_data_path() . "views_simplecache");
247  }
248 }
249 
257 function _elgg_rmdir($dir) {
258  $files = array_diff(scandir($dir), array('.', '..'));
259 
260  foreach ($files as $file) {
261  if (is_dir("$dir/$file")) {
262  _elgg_rmdir("$dir/$file");
263  } else {
264  unlink("$dir/$file");
265  }
266  }
267 
268  return rmdir($dir);
269 }
270 
279  global $CONFIG;
280 
281  if (!isset($CONFIG->views->simplecache) || !is_array($CONFIG->views->simplecache)) {
282  return false;
283  }
284 
285  _elgg_rmdir("{$CONFIG->dataroot}views_simplecache");
286  mkdir("{$CONFIG->dataroot}views_simplecache");
287 
288  $time = time();
289  datalist_set("simplecache_lastupdate", $time);
290  $CONFIG->lastcache = $time;
291 
292  return true;
293 }
294 
301 function _elgg_load_cache() {
302  global $CONFIG;
303 
304  $CONFIG->system_cache_loaded = false;
305 
306  $CONFIG->views = new stdClass();
307  $data = elgg_load_system_cache('view_locations');
308  if (!is_string($data)) {
309  return;
310  }
311  $CONFIG->views->locations = unserialize($data);
312 
313  $data = elgg_load_system_cache('view_types');
314  if (!is_string($data)) {
315  return;
316  }
317  $CONFIG->view_types = unserialize($data);
318 
319  $CONFIG->system_cache_loaded = true;
320 }
321 
328 function _elgg_cache_init() {
329  global $CONFIG;
330 
331  if (!defined('UPGRADING') && empty($CONFIG->lastcache)) {
332  $CONFIG->lastcache = (int)datalist_get('simplecache_lastupdate');
333  }
334 
335  // cache system data if enabled and not loaded
336  if ($CONFIG->system_cache_enabled && !$CONFIG->system_cache_loaded) {
337  elgg_save_system_cache('view_locations', serialize($CONFIG->views->locations));
338  elgg_save_system_cache('view_types', serialize($CONFIG->view_types));
339  }
340 
341  if ($CONFIG->system_cache_enabled && !$CONFIG->i18n_loaded_from_cache) {
343  foreach ($CONFIG->translations as $lang => $map) {
344  elgg_save_system_cache("$lang.lang", serialize($map));
345  }
346  }
347 }
348 
349 elgg_register_event_handler('ready', 'system', '_elgg_cache_init');
elgg_enable_system_cache()
Enables the system disk cache.
Definition: cache.php:92
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$view
Definition: crop.php:68
elgg_reset_system_cache()
Reset the system cache by deleting the caches.
Definition: cache.php:40
_elgg_cache_init()
Initializes the simplecache lastcache variable and creates system cache files when appropriate...
Definition: cache.php:328
elgg_invalidate_simplecache()
Deletes all cached views in the simplecache and sets the lastcache and lastupdate time to 0 for every...
Definition: cache.php:278
$m
Definition: metadata.php:11
elgg_normalize_url($url)
Definition: output.php:290
$lang
Definition: html.php:12
elgg_register_simplecache_view($view_name)
Registers a view to simple cache.
Definition: cache.php:136
elgg_disable_system_cache()
Disables the system disk cache.
Definition: cache.php:108
elgg_get_system_cache()
Returns an ElggCache object suitable for caching system information.
Definition: cache.php:20
$data
Definition: opendd.php:13
$files
Definition: crop.php:36
elgg_register_external_view($view, $cacheable=false)
Registers a view as being available externally (i.e.
Definition: views.php:231
reload_all_translations()
Reload all translations from all registered paths.
Definition: languages.php:365
_elgg_get_view_filetype($view)
Returns the type of output expected from the view.
Definition: cache.php:200
elgg_get_viewtype()
Return the current view type.
Definition: views.php:91
elgg_get_simplecache_url($type, $view)
Definition: cache.php:155
_elgg_load_cache()
Loads the system cache during engine boot.
Definition: cache.php:301
datalist_get($name)
Get the value of a datalist element.
elgg_set_config($name, $value)
Set an Elgg configuration value.
global $CONFIG
datalist_set($name, $value)
Set the value for a datalist element.
elgg global
Pointer to the global context.
Definition: elgglib.js:12
_elgg_get_simplecache_root()
Get the base url for simple cache requests.
Definition: cache.php:172
$type
Definition: add.php:8
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Register a callback as an Elgg event handler.
Definition: elgglib.php:669
elgg_load_system_cache($type)
Retrieve the contents of a system cache.
Definition: cache.php:69
elgg_is_simplecache_enabled()
Is simple cache enabled.
Definition: cache.php:214
elgg_disable_simplecache()
Disables the simple cache.
Definition: cache.php:240
elgg_save_system_cache($type, $data)
Saves a system cache.
Definition: cache.php:52
$lastcache
Definition: languages.php:8
_elgg_rmdir($dir)
Recursively deletes a directory, including all hidden files.
Definition: cache.php:257
elgg_enable_simplecache()
Enables the simple cache.
Definition: cache.php:225
elgg_get_data_path()
Get the data directory path for this installation.
$viewtype
Definition: start.php:132