CachedAction.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Abstract action class with scaffolding for caching HTML and other values
  4. * in a single blob.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup Actions
  23. * @author Jeroen De Dauw < jeroendedauw@gmail.com >
  24. * @since 1.20
  25. */
  26. /**
  27. * Abstract action class with scaffolding for caching HTML and other values
  28. * in a single blob.
  29. *
  30. * Before using any of the caching functionality, call startCache.
  31. * After the last call to either getCachedValue or addCachedHTML, call saveCache.
  32. *
  33. * To get a cached value or compute it, use getCachedValue like this:
  34. * $this->getCachedValue( $callback );
  35. *
  36. * To add HTML that should be cached, use addCachedHTML like this:
  37. * $this->addCachedHTML( $callback );
  38. *
  39. * The callback function is only called when needed, so do all your expensive
  40. * computations here. This function should returns the HTML to be cached.
  41. * It should not add anything to the PageOutput object!
  42. *
  43. * @ingroup Actions
  44. */
  45. abstract class CachedAction extends FormlessAction implements ICacheHelper {
  46. /**
  47. * CacheHelper object to which we forward the non-SpecialPage specific caching work.
  48. * Initialized in startCache.
  49. *
  50. * @since 1.20
  51. * @var CacheHelper
  52. */
  53. protected $cacheHelper;
  54. /**
  55. * If the cache is enabled or not.
  56. *
  57. * @since 1.20
  58. * @var bool
  59. */
  60. protected $cacheEnabled = true;
  61. /**
  62. * Sets if the cache should be enabled or not.
  63. *
  64. * @since 1.20
  65. * @param bool $cacheEnabled
  66. */
  67. public function setCacheEnabled( $cacheEnabled ) {
  68. $this->cacheHelper->setCacheEnabled( $cacheEnabled );
  69. }
  70. /**
  71. * Initializes the caching.
  72. * Should be called before the first time anything is added via addCachedHTML.
  73. *
  74. * @since 1.20
  75. *
  76. * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
  77. * @param bool|null $cacheEnabled Sets if the cache should be enabled or not.
  78. */
  79. public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
  80. $this->cacheHelper = new CacheHelper();
  81. $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
  82. $this->cacheHelper->setOnInitializedHandler( [ $this, 'onCacheInitialized' ] );
  83. $keyArgs = $this->getCacheKey();
  84. if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
  85. unset( $keyArgs['action'] );
  86. }
  87. $this->cacheHelper->setCacheKey( $keyArgs );
  88. if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
  89. $this->cacheHelper->rebuildOnDemand();
  90. }
  91. $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
  92. }
  93. /**
  94. * Get a cached value if available or compute it if not and then cache it if possible.
  95. * The provided $computeFunction is only called when the computation needs to happen
  96. * and should return a result value. $args are arguments that will be passed to the
  97. * compute function when called.
  98. *
  99. * @since 1.20
  100. *
  101. * @param callable $computeFunction
  102. * @param array|mixed $args
  103. * @param string|null $key
  104. *
  105. * @return mixed
  106. */
  107. public function getCachedValue( $computeFunction, $args = [], $key = null ) {
  108. return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
  109. }
  110. /**
  111. * Add some HTML to be cached.
  112. * This is done by providing a callback function that should
  113. * return the HTML to be added. It will only be called if the
  114. * item is not in the cache yet or when the cache has been invalidated.
  115. *
  116. * @since 1.20
  117. *
  118. * @param callable $computeFunction
  119. * @param array $args
  120. * @param string|null $key
  121. */
  122. public function addCachedHTML( $computeFunction, $args = [], $key = null ) {
  123. $html = $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
  124. $this->getOutput()->addHTML( $html );
  125. }
  126. /**
  127. * Saves the HTML to the cache in case it got recomputed.
  128. * Should be called after the last time anything is added via addCachedHTML.
  129. *
  130. * @since 1.20
  131. */
  132. public function saveCache() {
  133. $this->cacheHelper->saveCache();
  134. }
  135. /**
  136. * Sets the time to live for the cache, in seconds or a unix timestamp
  137. * indicating the point of expiry.
  138. *
  139. * @since 1.20
  140. *
  141. * @param int $cacheExpiry
  142. */
  143. public function setExpiry( $cacheExpiry ) {
  144. $this->cacheHelper->setExpiry( $cacheExpiry );
  145. }
  146. /**
  147. * Returns the variables used to constructed the cache key in an array.
  148. *
  149. * @since 1.20
  150. *
  151. * @return array
  152. */
  153. protected function getCacheKey() {
  154. return [
  155. get_class( $this->page ),
  156. $this->getName(),
  157. $this->getLanguage()->getCode()
  158. ];
  159. }
  160. /**
  161. * Gets called after the cache got initialized.
  162. *
  163. * @since 1.20
  164. *
  165. * @param bool $hasCached
  166. */
  167. public function onCacheInitialized( $hasCached ) {
  168. if ( $hasCached ) {
  169. $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
  170. }
  171. }
  172. }