SimpleCache.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Elgg\Cache;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * @access private
  7. *
  8. * @package Elgg.Core
  9. * @subpackage Cache
  10. * @since 1.10.0
  11. */
  12. class SimpleCache {
  13. /**
  14. * Global Elgg configuration
  15. *
  16. * @var \stdClass
  17. */
  18. private $CONFIG;
  19. /**
  20. * Constructor
  21. */
  22. public function __construct() {
  23. global $CONFIG;
  24. $this->CONFIG = $CONFIG;
  25. }
  26. /**
  27. * Registers a view to simple cache.
  28. *
  29. * Simple cache is a caching mechanism that saves the output of
  30. * a view and its extensions into a file. If the view is called
  31. * by the {@link engine/handlers/cache_handler.php} file, the Elgg
  32. * engine will not be loaded and the contents of the view will returned
  33. * from file.
  34. *
  35. * @warning Simple cached views must take no parameters and return
  36. * the same content no matter who is logged in.
  37. *
  38. * @param string $view_name View name
  39. *
  40. * @return void
  41. * @see elgg_get_simplecache_url()
  42. */
  43. function registerView($view_name) {
  44. elgg_register_external_view($view_name, true);
  45. }
  46. /**
  47. * Get the URL for the cached file
  48. *
  49. * This automatically registers the view with Elgg's simplecache.
  50. *
  51. * @example
  52. * $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft');
  53. * elgg_register_js('elgg.blog', $blog_js);
  54. * elgg_load_js('elgg.blog');
  55. *
  56. * @param string $type The file type: css or js
  57. * @param string $view The view name after css/ or js/
  58. * @return string
  59. */
  60. function getUrl($type, $view) {
  61. // handle file type passed with view name
  62. if (($type === 'js' || $type === 'css') && 0 === strpos($view, $type . '/')) {
  63. $view = substr($view, strlen($type) + 1);
  64. }
  65. elgg_register_simplecache_view("$type/$view");
  66. return _elgg_get_simplecache_root() . "$type/$view";
  67. }
  68. /**
  69. * Get the base url for simple cache requests
  70. *
  71. * @return string The simplecache root url for the current viewtype.
  72. * @access private
  73. */
  74. function getRoot() {
  75. $viewtype = elgg_get_viewtype();
  76. if (elgg_is_simplecache_enabled()) {
  77. // stored in datalist as 'simplecache_lastupdate'
  78. $lastcache = (int)_elgg_services()->config->get('lastcache');
  79. } else {
  80. $lastcache = 0;
  81. }
  82. return elgg_normalize_url("/cache/$lastcache/$viewtype/");
  83. }
  84. /**
  85. * Is simple cache enabled
  86. *
  87. * @return bool
  88. */
  89. function isEnabled() {
  90. return (bool) _elgg_services()->config->get('simplecache_enabled');
  91. }
  92. /**
  93. * Enables the simple cache.
  94. *
  95. * @see elgg_register_simplecache_view()
  96. * @return void
  97. */
  98. function enable() {
  99. _elgg_services()->datalist->set('simplecache_enabled', 1);
  100. _elgg_services()->config->set('simplecache_enabled', 1);
  101. elgg_invalidate_simplecache();
  102. }
  103. /**
  104. * Disables the simple cache.
  105. *
  106. * @warning Simplecache is also purged when disabled.
  107. *
  108. * @see elgg_register_simplecache_view()
  109. * @return void
  110. */
  111. function disable() {
  112. if (_elgg_services()->config->get('simplecache_enabled')) {
  113. _elgg_services()->datalist->set('simplecache_enabled', 0);
  114. _elgg_services()->config->set('simplecache_enabled', 0);
  115. // purge simple cache
  116. _elgg_rmdir(_elgg_services()->config->getDataPath() . "views_simplecache");
  117. }
  118. }
  119. /**
  120. * Deletes all cached views in the simplecache and sets the lastcache and
  121. * lastupdate time to 0 for every valid viewtype.
  122. *
  123. * @return bool
  124. */
  125. function invalidate() {
  126. if (!isset($this->CONFIG->views->simplecache) || !is_array($this->CONFIG->views->simplecache)) {
  127. return false;
  128. }
  129. _elgg_rmdir("{$this->CONFIG->dataroot}views_simplecache");
  130. mkdir("{$this->CONFIG->dataroot}views_simplecache");
  131. $time = time();
  132. _elgg_services()->datalist->set("simplecache_lastupdate", $time);
  133. $this->CONFIG->lastcache = $time;
  134. return true;
  135. }
  136. /**
  137. * Set up $CONFIG appropriately on engine boot.
  138. *
  139. * @return void
  140. */
  141. function init() {
  142. if (!defined('UPGRADING') && empty($this->CONFIG->lastcache)) {
  143. $this->CONFIG->lastcache = (int)_elgg_services()->datalist->get('simplecache_lastupdate');
  144. }
  145. }
  146. }