r114219 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114218‎ | r114219 | r114220 >
Date:00:04, 20 March 2012
Author:jeroendedauw
Status:reverted
Tags:gerritmigration 
Comment:
follow up to r114215, fix some fails and added CachedAction implementing the same stuff as SpecialCachedPage
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/CacheHelper.php (modified) (history)
  • /trunk/phase3/includes/actions/CachedAction.php (added) (history)
  • /trunk/phase3/includes/specials/SpecialCachedPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialCachedPage.php
@@ -1,7 +1,8 @@
22 <?php
33
44 /**
5 - * Abstract special page class with scaffolding for caching the HTML output.
 5+ * Abstract special page class with scaffolding for caching HTML and other values
 6+ * in a single blob.
67 *
78 * Before using any of the cahing functionality, call startCache.
89 * After the last call to either getCachedValue or addCachedHTML, call saveCache.
@@ -75,6 +76,24 @@
7677 }
7778
7879 /**
 80+ * Get a cached value if available or compute it if not and then cache it if possible.
 81+ * The provided $computeFunction is only called when the computation needs to happen
 82+ * and should return a result value. $args are arguments that will be passed to the
 83+ * compute function when called.
 84+ *
 85+ * @since 1.20
 86+ *
 87+ * @param {function} $computeFunction
 88+ * @param array|mixed $args
 89+ * @param string|null $key
 90+ *
 91+ * @return mixed
 92+ */
 93+ public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
 94+ return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
 95+ }
 96+
 97+ /**
7998 * Add some HTML to be cached.
8099 * This is done by providing a callback function that should
81100 * return the HTML to be added. It will only be called if the
Index: trunk/phase3/includes/AutoLoader.php
@@ -259,6 +259,7 @@
260260 'ZipDirectoryReader' => 'includes/ZipDirectoryReader.php',
261261
262262 # includes/actions
 263+ 'CachedAction' => 'includes/actions/CachedAction.php',
263264 'CreditsAction' => 'includes/actions/CreditsAction.php',
264265 'DeleteAction' => 'includes/actions/DeleteAction.php',
265266 'EditAction' => 'includes/actions/EditAction.php',
Index: trunk/phase3/includes/CacheHelper.php
@@ -1,5 +1,13 @@
22 <?php
33
 4+/**
 5+ * Interface for all classes implementing CacheHelper functionality.
 6+ *
 7+ * @since 1.20
 8+ *
 9+ * @licence GNU GPL v2 or later
 10+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 11+ */
412 interface ICacheHelper {
513
614 /**
@@ -22,18 +30,20 @@
2331 function startCache( $cacheExpiry = null, $cacheEnabled = null );
2432
2533 /**
26 - * Add some HTML to be cached.
27 - * This is done by providing a callback function that should
28 - * return the HTML to be added. It will only be called if the
29 - * item is not in the cache yet or when the cache has been invalidated.
 34+ * Get a cached value if available or compute it if not and then cache it if possible.
 35+ * The provided $computeFunction is only called when the computation needs to happen
 36+ * and should return a result value. $args are arguments that will be passed to the
 37+ * compute function when called.
3038 *
3139 * @since 1.20
3240 *
3341 * @param {function} $computeFunction
34 - * @param array $args
 42+ * @param array|mixed $args
3543 * @param string|null $key
 44+ *
 45+ * @return mixed
3646 */
37 - function addCachedHTML( $computeFunction, $args = array(), $key = null );
 47+ function getCachedValue( $computeFunction, $args = array(), $key = null );
3848
3949 /**
4050 * Saves the HTML to the cache in case it got recomputed.
@@ -73,7 +83,6 @@
7484 * @since 1.20
7585 *
7686 * @file CacheHelper.php
77 - * @ingroup SpecialPage
7887 *
7988 * @licence GNU GPL v2 or later
8089 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
@@ -216,25 +225,7 @@
217226 }
218227 }
219228
220 -
221 -
222229 /**
223 - * Add some HTML to be cached.
224 - * This is done by providing a callback function that should
225 - * return the HTML to be added. It will only be called if the
226 - * item is not in the cache yet or when the cache has been invalidated.
227 - *
228 - * @since 1.20
229 - *
230 - * @param {function} $computeFunction
231 - * @param array $args
232 - * @param string|null $key
233 - */
234 - public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
235 - $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) );
236 - }
237 -
238 - /**
239230 * Get a cached value if available or compute it if not and then cache it if possible.
240231 * The provided $computeFunction is only called when the computation needs to happen
241232 * and should return a result value. $args are arguments that will be passed to the
Index: trunk/phase3/includes/actions/CachedAction.php
@@ -0,0 +1,161 @@
 2+<?php
 3+
 4+/**
 5+ * Abstract action class with scaffolding for caching HTML and other values
 6+ * in a single blob.
 7+ *
 8+ * Before using any of the cahing functionality, call startCache.
 9+ * After the last call to either getCachedValue or addCachedHTML, call saveCache.
 10+ *
 11+ * To get a cached value or compute it, use getCachedValue like this:
 12+ * $this->getCachedValue( $callback );
 13+ *
 14+ * To add HTML that should be cached, use addCachedHTML like this:
 15+ * $this->addCachedHTML( $callback );
 16+ *
 17+ * The callback function is only called when needed, so do all your expensive
 18+ * computations here. This function should returns the HTML to be cached.
 19+ * It should not add anything to the PageOutput object!
 20+ *
 21+ * @since 1.20
 22+ *
 23+ * @file CachedAction.php
 24+ * @ingroup Action
 25+ *
 26+ * @licence GNU GPL v2 or later
 27+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 28+ */
 29+abstract class CachedAction extends FormlessAction implements ICacheHelper {
 30+
 31+ /**
 32+ * CacheHelper object to which we foreward the non-SpecialPage specific caching work.
 33+ * Initialized in startCache.
 34+ *
 35+ * @since 1.20
 36+ * @var CacheHelper
 37+ */
 38+ protected $cacheHelper;
 39+
 40+ /**
 41+ * Sets if the cache should be enabled or not.
 42+ *
 43+ * @since 1.20
 44+ * @param boolean $cacheEnabled
 45+ */
 46+ public function setCacheEnabled( $cacheEnabled ) {
 47+ $this->cacheHelper->setCacheEnabled( $cacheEnabled );
 48+ }
 49+
 50+ /**
 51+ * Initializes the caching.
 52+ * Should be called before the first time anything is added via addCachedHTML.
 53+ *
 54+ * @since 1.20
 55+ *
 56+ * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
 57+ * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
 58+ */
 59+ public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
 60+ $this->cacheHelper = new CacheHelper( $this->get );
 61+
 62+ $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
 63+
 64+ $keyArgs = $this->getCacheKey();
 65+
 66+ if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
 67+ unset( $keyArgs['action'] );
 68+ }
 69+
 70+ $this->cacheHelper->setCacheKey( $keyArgs );
 71+
 72+ if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
 73+ $this->cacheHelper->purge();
 74+ }
 75+
 76+ $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
 77+ }
 78+
 79+ /**
 80+ * Get a cached value if available or compute it if not and then cache it if possible.
 81+ * The provided $computeFunction is only called when the computation needs to happen
 82+ * and should return a result value. $args are arguments that will be passed to the
 83+ * compute function when called.
 84+ *
 85+ * @since 1.20
 86+ *
 87+ * @param {function} $computeFunction
 88+ * @param array|mixed $args
 89+ * @param string|null $key
 90+ *
 91+ * @return mixed
 92+ */
 93+ public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
 94+ return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
 95+ }
 96+
 97+ /**
 98+ * Add some HTML to be cached.
 99+ * This is done by providing a callback function that should
 100+ * return the HTML to be added. It will only be called if the
 101+ * item is not in the cache yet or when the cache has been invalidated.
 102+ *
 103+ * @since 1.20
 104+ *
 105+ * @param {function} $computeFunction
 106+ * @param array $args
 107+ * @param string|null $key
 108+ */
 109+ public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
 110+ $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) );
 111+ }
 112+
 113+ /**
 114+ * Saves the HTML to the cache in case it got recomputed.
 115+ * Should be called after the last time anything is added via addCachedHTML.
 116+ *
 117+ * @since 1.20
 118+ */
 119+ public function saveCache() {
 120+ $this->cacheHelper->saveCache();
 121+ }
 122+
 123+ /**
 124+ * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
 125+ *
 126+ * @since 1.20
 127+ *
 128+ * @param integer $cacheExpiry
 129+ */
 130+ public function setExpirey( $cacheExpiry ) {
 131+ $this->cacheHelper->setExpirey( $cacheExpiry );
 132+ }
 133+
 134+ /**
 135+ * Returns the variables used to constructed the cache key in an array.
 136+ *
 137+ * @since 1.20
 138+ *
 139+ * @return array
 140+ */
 141+ protected function getCacheKey() {
 142+ return array(
 143+ get_class( $this->page ),
 144+ $this->getName(),
 145+ $this->getLanguage()->getCode()
 146+ );
 147+ }
 148+
 149+ /**
 150+ * Gets called after the cache got initialized.
 151+ *
 152+ * @since 1.20
 153+ *
 154+ * @param boolean $hasCached
 155+ */
 156+ public function onCacheInitialized( $hasCached ) {
 157+ if ( $hasCached ) {
 158+ $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
 159+ }
 160+ }
 161+
 162+}
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r114326Revert r114067, r114071, r114075, r114079, r114081, r114082, r114084, r114086......catrope23:03, 20 March 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r114215split general cache helper functionality to its own class, so we can also eas...jeroendedauw23:33, 19 March 2012

Status & tagging log