cache.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Elgg cache
  4. * Cache file interface for caching data.
  5. *
  6. * @package Elgg.Core
  7. * @subpackage Cache
  8. */
  9. /* Filepath Cache */
  10. /**
  11. * Returns an \ElggCache object suitable for caching system information
  12. *
  13. * @todo Can this be done in a cleaner way?
  14. * @todo Swap to memcache etc?
  15. *
  16. * @return \ElggFileCache
  17. */
  18. function elgg_get_system_cache() {
  19. return _elgg_services()->systemCache->getFileCache();
  20. }
  21. /**
  22. * Reset the system cache by deleting the caches
  23. *
  24. * @return void
  25. */
  26. function elgg_reset_system_cache() {
  27. _elgg_services()->systemCache->reset();
  28. }
  29. /**
  30. * Saves a system cache.
  31. *
  32. * @param string $type The type or identifier of the cache
  33. * @param string $data The data to be saved
  34. * @return bool
  35. */
  36. function elgg_save_system_cache($type, $data) {
  37. return _elgg_services()->systemCache->save($type, $data);
  38. }
  39. /**
  40. * Retrieve the contents of a system cache.
  41. *
  42. * @param string $type The type of cache to load
  43. * @return string
  44. */
  45. function elgg_load_system_cache($type) {
  46. return _elgg_services()->systemCache->load($type);
  47. }
  48. /**
  49. * Enables the system disk cache.
  50. *
  51. * Uses the 'system_cache_enabled' datalist with a boolean value.
  52. * Resets the system cache.
  53. *
  54. * @return void
  55. */
  56. function elgg_enable_system_cache() {
  57. _elgg_services()->systemCache->enable();
  58. }
  59. /**
  60. * Disables the system disk cache.
  61. *
  62. * Uses the 'system_cache_enabled' datalist with a boolean value.
  63. * Resets the system cache.
  64. *
  65. * @return void
  66. */
  67. function elgg_disable_system_cache() {
  68. _elgg_services()->systemCache->disable();
  69. }
  70. /* Simplecache */
  71. /**
  72. * Registers a view to simple cache.
  73. *
  74. * Simple cache is a caching mechanism that saves the output of
  75. * a view and its extensions into a file. If the view is called
  76. * by the {@link engine/handlers/cache_handler.php} file, the Elgg
  77. * engine will not be loaded and the contents of the view will returned
  78. * from file.
  79. *
  80. * @warning Simple cached views must take no parameters and return
  81. * the same content no matter who is logged in.
  82. *
  83. * @param string $view_name View name
  84. *
  85. * @return void
  86. * @see elgg_get_simplecache_url()
  87. * @since 1.8.0
  88. */
  89. function elgg_register_simplecache_view($view_name) {
  90. _elgg_services()->simpleCache->registerView($view_name);
  91. }
  92. /**
  93. * Get the URL for the cached file
  94. *
  95. * This automatically registers the view with Elgg's simplecache.
  96. *
  97. * @example
  98. * $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft');
  99. * elgg_register_js('elgg.blog', $blog_js);
  100. * elgg_load_js('elgg.blog');
  101. *
  102. * @param string $type The file type: css or js
  103. * @param string $view The view name after css/ or js/
  104. * @return string
  105. * @since 1.8.0
  106. */
  107. function elgg_get_simplecache_url($type, $view) {
  108. return _elgg_services()->simpleCache->getUrl($type, $view);
  109. }
  110. /**
  111. * Get the base url for simple cache requests
  112. *
  113. * @return string The simplecache root url for the current viewtype.
  114. * @access private
  115. */
  116. function _elgg_get_simplecache_root() {
  117. return _elgg_services()->simpleCache->getRoot();
  118. }
  119. /**
  120. * Returns the type of output expected from the view.
  121. *
  122. * css/* views always return "css"
  123. * js/* views always return "js"
  124. *
  125. * @todo why isn't this in the CacheHandler class? It is not used anywhere else.
  126. *
  127. * @todo view/name.suffix returns "suffix"
  128. *
  129. * Otherwise, returns "unknown"
  130. *
  131. * @param string $view The view name
  132. * @return string
  133. * @access private
  134. */
  135. function _elgg_get_view_filetype($view) {
  136. if (preg_match('~(?:^|/)(css|js)(?:$|/)~', $view, $m)) {
  137. return $m[1];
  138. } else {
  139. return 'unknown';
  140. }
  141. }
  142. /**
  143. * Is simple cache enabled
  144. *
  145. * @return bool
  146. * @since 1.8.0
  147. */
  148. function elgg_is_simplecache_enabled() {
  149. return _elgg_services()->simpleCache->isEnabled();
  150. }
  151. /**
  152. * Enables the simple cache.
  153. *
  154. * @see elgg_register_simplecache_view()
  155. * @return void
  156. * @since 1.8.0
  157. */
  158. function elgg_enable_simplecache() {
  159. _elgg_services()->simpleCache->enable();
  160. }
  161. /**
  162. * Disables the simple cache.
  163. *
  164. * @warning Simplecache is also purged when disabled.
  165. *
  166. * @see elgg_register_simplecache_view()
  167. * @return void
  168. * @since 1.8.0
  169. */
  170. function elgg_disable_simplecache() {
  171. _elgg_services()->simpleCache->disable();
  172. }
  173. /**
  174. * Recursively deletes a directory, including all hidden files.
  175. *
  176. * TODO(ewinslow): Move to filesystem package
  177. *
  178. * @param string $dir
  179. * @return boolean Whether the dir was successfully deleted.
  180. * @access private
  181. */
  182. function _elgg_rmdir($dir) {
  183. $files = array_diff(scandir($dir), array('.', '..'));
  184. foreach ($files as $file) {
  185. if (is_dir("$dir/$file")) {
  186. _elgg_rmdir("$dir/$file");
  187. } else {
  188. unlink("$dir/$file");
  189. }
  190. }
  191. return rmdir($dir);
  192. }
  193. /**
  194. * Deletes all cached views in the simplecache and sets the lastcache and
  195. * lastupdate time to 0 for every valid viewtype.
  196. *
  197. * @return bool
  198. * @since 1.7.4
  199. */
  200. function elgg_invalidate_simplecache() {
  201. _elgg_services()->simpleCache->invalidate();
  202. }
  203. /**
  204. * Flush all the registered caches
  205. *
  206. * @return void
  207. * @since 1.11
  208. */
  209. function elgg_flush_caches() {
  210. _elgg_services()->events->trigger('cache:flush', 'system');
  211. }
  212. /**
  213. * Initializes the simplecache lastcache variable and creates system cache files
  214. * when appropriate.
  215. *
  216. * @access private
  217. */
  218. function _elgg_cache_init() {
  219. _elgg_services()->simpleCache->init();
  220. _elgg_services()->systemCache->init();
  221. }
  222. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  223. $events->registerHandler('ready', 'system', '_elgg_cache_init');
  224. // register plugin hooks for cache reset
  225. $events->registerHandler('cache:flush', 'system', 'elgg_reset_system_cache');
  226. $events->registerHandler('cache:flush', 'system', 'elgg_invalidate_simplecache');
  227. };