1 | <?php |
||
103 | class CacheHelper implements ICacheHelper { |
||
104 | /** |
||
105 | * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry. |
||
106 | * |
||
107 | * @since 1.20 |
||
108 | * @var int |
||
109 | */ |
||
110 | protected $cacheExpiry = 3600; |
||
111 | |||
112 | /** |
||
113 | * List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached). |
||
114 | * If not cached already, then the newly computed chunks are added here, |
||
115 | * if it as cached already, chunks are removed from this list as they are needed. |
||
116 | * |
||
117 | * @since 1.20 |
||
118 | * @var array |
||
119 | */ |
||
120 | protected $cachedChunks; |
||
121 | |||
122 | /** |
||
123 | * Indicates if the to be cached content was already cached. |
||
124 | * Null if this information is not available yet. |
||
125 | * |
||
126 | * @since 1.20 |
||
127 | * @var bool|null |
||
128 | */ |
||
129 | protected $hasCached = null; |
||
130 | |||
131 | /** |
||
132 | * If the cache is enabled or not. |
||
133 | * |
||
134 | * @since 1.20 |
||
135 | * @var bool |
||
136 | */ |
||
137 | protected $cacheEnabled = true; |
||
138 | |||
139 | /** |
||
140 | * Function that gets called when initialization is done. |
||
141 | * |
||
142 | * @since 1.20 |
||
143 | * @var callable |
||
144 | */ |
||
145 | protected $onInitHandler = false; |
||
146 | |||
147 | /** |
||
148 | * Elements to build a cache key with. |
||
149 | * |
||
150 | * @since 1.20 |
||
151 | * @var array |
||
152 | */ |
||
153 | protected $cacheKey = []; |
||
154 | |||
155 | /** |
||
156 | * Sets if the cache should be enabled or not. |
||
157 | * |
||
158 | * @since 1.20 |
||
159 | * @param bool $cacheEnabled |
||
160 | */ |
||
161 | public function setCacheEnabled( $cacheEnabled ) { |
||
164 | |||
165 | /** |
||
166 | * Initializes the caching. |
||
167 | * Should be called before the first time anything is added via addCachedHTML. |
||
168 | * |
||
169 | * @since 1.20 |
||
170 | * |
||
171 | * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp. |
||
172 | * @param bool|null $cacheEnabled Sets if the cache should be enabled or not. |
||
173 | */ |
||
174 | public function startCache( $cacheExpiry = null, $cacheEnabled = null ) { |
||
187 | |||
188 | /** |
||
189 | * Returns a message that notifies the user he/she is looking at |
||
190 | * a cached version of the page, including a refresh link. |
||
191 | * |
||
192 | * @since 1.20 |
||
193 | * |
||
194 | * @param IContextSource $context |
||
195 | * @param bool $includePurgeLink |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) { |
||
230 | |||
231 | /** |
||
232 | * Initializes the caching if not already done so. |
||
233 | * Should be called before any of the caching functionality is used. |
||
234 | * |
||
235 | * @since 1.20 |
||
236 | */ |
||
237 | protected function initCaching() { |
||
249 | |||
250 | /** |
||
251 | * Get a cached value if available or compute it if not and then cache it if possible. |
||
252 | * The provided $computeFunction is only called when the computation needs to happen |
||
253 | * and should return a result value. $args are arguments that will be passed to the |
||
254 | * compute function when called. |
||
255 | * |
||
256 | * @since 1.20 |
||
257 | * |
||
258 | * @param callable $computeFunction |
||
259 | * @param array|mixed $args |
||
260 | * @param string|null $key |
||
261 | * |
||
262 | * @return mixed |
||
263 | */ |
||
264 | public function getCachedValue( $computeFunction, $args = [], $key = null ) { |
||
308 | |||
309 | /** |
||
310 | * Saves the HTML to the cache in case it got recomputed. |
||
311 | * Should be called after the last time anything is added via addCachedHTML. |
||
312 | * |
||
313 | * @since 1.20 |
||
314 | */ |
||
315 | public function saveCache() { |
||
324 | |||
325 | /** |
||
326 | * Sets the time to live for the cache, in seconds or a unix timestamp |
||
327 | * indicating the point of expiry... |
||
328 | * |
||
329 | * @since 1.20 |
||
330 | * |
||
331 | * @param int $cacheExpiry |
||
332 | */ |
||
333 | public function setExpiry( $cacheExpiry ) { |
||
336 | |||
337 | /** |
||
338 | * Returns the cache key to use to cache this page's HTML output. |
||
339 | * Is constructed from the special page name and language code. |
||
340 | * |
||
341 | * @since 1.20 |
||
342 | * |
||
343 | * @return string |
||
344 | * @throws MWException |
||
345 | */ |
||
346 | protected function getCacheKeyString() { |
||
353 | |||
354 | /** |
||
355 | * Sets the cache key that should be used. |
||
356 | * |
||
357 | * @since 1.20 |
||
358 | * |
||
359 | * @param array $cacheKey |
||
360 | */ |
||
361 | public function setCacheKey( array $cacheKey ) { |
||
364 | |||
365 | /** |
||
366 | * Rebuild the content, even if it's already cached. |
||
367 | * This effectively has the same effect as purging the cache, |
||
368 | * since it will be overridden with the new value on the next request. |
||
369 | * |
||
370 | * @since 1.20 |
||
371 | */ |
||
372 | public function rebuildOnDemand() { |
||
375 | |||
376 | /** |
||
377 | * Sets a function that gets called when initialization of the cache is done. |
||
378 | * |
||
379 | * @since 1.20 |
||
380 | * |
||
381 | * @param callable $handlerFunction |
||
382 | */ |
||
383 | public function setOnInitializedHandler( $handlerFunction ) { |
||
386 | } |
||
387 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: