Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / actions / CachedAction.php
blobbfdda7b9393372582128dc11cc072c634acbf93c
1 <?php
2 /**
3 * Abstract action class with scaffolding for caching HTML and other values
4 * in a single blob.
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.
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.
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
21 * @file
22 * @ingroup Actions
23 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
24 * @since 1.20
27 /**
28 * Abstract action class with scaffolding for caching HTML and other values
29 * in a single blob.
31 * Before using any of the caching functionality, call startCache.
32 * After the last call to either getCachedValue or addCachedHTML, call saveCache.
34 * To get a cached value or compute it, use getCachedValue like this:
35 * $this->getCachedValue( $callback );
37 * To add HTML that should be cached, use addCachedHTML like this:
38 * $this->addCachedHTML( $callback );
40 * The callback function is only called when needed, so do all your expensive
41 * computations here. This function should returns the HTML to be cached.
42 * It should not add anything to the PageOutput object!
44 * @ingroup Actions
46 abstract class CachedAction extends FormlessAction implements ICacheHelper {
48 /**
49 * CacheHelper object to which we forward the non-SpecialPage specific caching work.
50 * Initialized in startCache.
52 * @since 1.20
53 * @var CacheHelper
55 protected $cacheHelper;
57 /**
58 * If the cache is enabled or not.
60 * @since 1.20
61 * @var boolean
63 protected $cacheEnabled = true;
65 /**
66 * Sets if the cache should be enabled or not.
68 * @since 1.20
69 * @param boolean $cacheEnabled
71 public function setCacheEnabled( $cacheEnabled ) {
72 $this->cacheHelper->setCacheEnabled( $cacheEnabled );
75 /**
76 * Initializes the caching.
77 * Should be called before the first time anything is added via addCachedHTML.
79 * @since 1.20
81 * @param integer|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
82 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
84 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
85 $this->cacheHelper = new CacheHelper();
87 $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
88 $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
90 $keyArgs = $this->getCacheKey();
92 if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
93 unset( $keyArgs['action'] );
96 $this->cacheHelper->setCacheKey( $keyArgs );
98 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
99 $this->cacheHelper->rebuildOnDemand();
102 $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
106 * Get a cached value if available or compute it if not and then cache it if possible.
107 * The provided $computeFunction is only called when the computation needs to happen
108 * and should return a result value. $args are arguments that will be passed to the
109 * compute function when called.
111 * @since 1.20
113 * @param {function} $computeFunction
114 * @param array|mixed $args
115 * @param string|null $key
117 * @return mixed
119 public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
120 return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
124 * Add some HTML to be cached.
125 * This is done by providing a callback function that should
126 * return the HTML to be added. It will only be called if the
127 * item is not in the cache yet or when the cache has been invalidated.
129 * @since 1.20
131 * @param {function} $computeFunction
132 * @param array $args
133 * @param string|null $key
135 public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
136 $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) );
140 * Saves the HTML to the cache in case it got recomputed.
141 * Should be called after the last time anything is added via addCachedHTML.
143 * @since 1.20
145 public function saveCache() {
146 $this->cacheHelper->saveCache();
150 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
152 * @since 1.20
154 * @param integer $cacheExpiry
156 public function setExpiry( $cacheExpiry ) {
157 $this->cacheHelper->setExpiry( $cacheExpiry );
161 * Returns the variables used to constructed the cache key in an array.
163 * @since 1.20
165 * @return array
167 protected function getCacheKey() {
168 return array(
169 get_class( $this->page ),
170 $this->getName(),
171 $this->getLanguage()->getCode()
176 * Gets called after the cache got initialized.
178 * @since 1.20
180 * @param boolean $hasCached
182 public function onCacheInitialized( $hasCached ) {
183 if ( $hasCached ) {
184 $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );