SystemCache.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 SystemCache {
  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. * Returns an \ElggCache object suitable for caching system information
  28. *
  29. * @todo Can this be done in a cleaner way?
  30. * @todo Swap to memcache etc?
  31. *
  32. * @return \ElggFileCache
  33. */
  34. function getFileCache() {
  35. /**
  36. * A default filestore cache using the dataroot.
  37. */
  38. static $FILE_PATH_CACHE;
  39. if (!$FILE_PATH_CACHE) {
  40. $FILE_PATH_CACHE = new \ElggFileCache($this->CONFIG->dataroot . 'system_cache/');
  41. }
  42. return $FILE_PATH_CACHE;
  43. }
  44. /**
  45. * Reset the system cache by deleting the caches
  46. *
  47. * @return void
  48. */
  49. function reset() {
  50. $this->getFileCache()->clear();
  51. }
  52. /**
  53. * Saves a system cache.
  54. *
  55. * @param string $type The type or identifier of the cache
  56. * @param string $data The data to be saved
  57. * @return bool
  58. */
  59. function save($type, $data) {
  60. if ($this->CONFIG->system_cache_enabled) {
  61. return $this->getFileCache()->save($type, $data);
  62. }
  63. return false;
  64. }
  65. /**
  66. * Retrieve the contents of a system cache.
  67. *
  68. * @param string $type The type of cache to load
  69. * @return string
  70. */
  71. function load($type) {
  72. if ($this->CONFIG->system_cache_enabled) {
  73. $cached_data = $this->getFileCache()->load($type);
  74. if ($cached_data) {
  75. return $cached_data;
  76. }
  77. }
  78. return null;
  79. }
  80. /**
  81. * Enables the system disk cache.
  82. *
  83. * Uses the 'system_cache_enabled' datalist with a boolean value.
  84. * Resets the system cache.
  85. *
  86. * @return void
  87. */
  88. function enable() {
  89. _elgg_services()->datalist->set('system_cache_enabled', 1);
  90. $this->CONFIG->system_cache_enabled = 1;
  91. $this->reset();
  92. }
  93. /**
  94. * Disables the system disk cache.
  95. *
  96. * Uses the 'system_cache_enabled' datalist with a boolean value.
  97. * Resets the system cache.
  98. *
  99. * @return void
  100. */
  101. function disable() {
  102. _elgg_services()->datalist->set('system_cache_enabled', 0);
  103. $this->CONFIG->system_cache_enabled = 0;
  104. $this->reset();
  105. }
  106. /**
  107. * Loads the system cache during engine boot
  108. *
  109. * @see elgg_reset_system_cache()
  110. * @access private
  111. */
  112. function loadAll() {
  113. $this->CONFIG->system_cache_loaded = false;
  114. $this->CONFIG->views = new \stdClass();
  115. $data = $this->load('view_locations');
  116. if (!is_string($data)) {
  117. return;
  118. }
  119. $this->CONFIG->views->locations = unserialize($data);
  120. $data = $this->load('view_types');
  121. if (!is_string($data)) {
  122. return;
  123. }
  124. $this->CONFIG->view_types = unserialize($data);
  125. // Note: We don't need view_overrides for operation. Inspector can pull this from the cache
  126. $this->CONFIG->system_cache_loaded = true;
  127. }
  128. /**
  129. * Initializes the simplecache lastcache variable and creates system cache files
  130. * when appropriate.
  131. *
  132. * @access private
  133. */
  134. function init() {
  135. if (!$this->CONFIG->system_cache_enabled) {
  136. return;
  137. }
  138. // cache system data if enabled and not loaded
  139. if (!$this->CONFIG->system_cache_loaded) {
  140. $this->save('view_locations', serialize($this->CONFIG->views->locations));
  141. $this->save('view_types', serialize($this->CONFIG->view_types));
  142. // this is saved just for the inspector and is not loaded in loadAll()
  143. $this->save('view_overrides', serialize(_elgg_services()->views->getOverriddenLocations()));
  144. }
  145. if (!$this->CONFIG->i18n_loaded_from_cache) {
  146. _elgg_services()->translator->reloadAllTranslations();
  147. foreach ($this->CONFIG->translations as $lang => $map) {
  148. $this->save("$lang.lang", serialize($map));
  149. }
  150. }
  151. }
  152. }