r114180 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114179‎ | r114180 | r114181 >
Date:20:51, 19 March 2012
Author:jeroendedauw
Status:reverted (Comments)
Tags:gerritmigration 
Comment:
make more generic: do not assume we want to add html to the output. Also get rid of action=purge from key args
Modified paths:
  • /trunk/phase3/includes/specials/SpecialCachedPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialCachedPage.php
@@ -164,15 +164,33 @@
165165 *
166166 * @since 1.20
167167 *
168 - * @param {function} $callback
 168+ * @param {function} $computeFunction
169169 * @param array $args
170170 * @param string|null $key
171171 */
172 - public function addCachedHTML( $callback, $args = array(), $key = null ) {
 172+ public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
 173+ $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) );
 174+ }
 175+
 176+ /**
 177+ * Get a cached value if available or compute it if not and then cache it if possible.
 178+ * The provided $computeFunction is only called when the computation needs to happen
 179+ * and should return a result value. $args are arguments that will be passed to the
 180+ * compute function when called.
 181+ *
 182+ * @since 1.20
 183+ *
 184+ * @param {function} $computeFunction
 185+ * @param array|mixed $args
 186+ * @param string|null $key
 187+ *
 188+ * @return mixed
 189+ */
 190+ protected function getCachedValue( $computeFunction, $args = array(), $key = null ) {
173191 $this->initCaching();
174192
175193 if ( $this->cacheEnabled && $this->hasCached ) {
176 - $html = '';
 194+ $value = null;
177195
178196 if ( is_null( $key ) ) {
179197 $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) );
@@ -185,12 +203,12 @@
186204 wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
187205 }
188206 else {
189 - $html = array_shift( $this->cachedChunks );
 207+ $value = array_shift( $this->cachedChunks );
190208 }
191209 }
192210 else {
193211 if ( array_key_exists( $key, $this->cachedChunks ) ) {
194 - $html = $this->cachedChunks[$key];
 212+ $value = $this->cachedChunks[$key];
195213 unset( $this->cachedChunks[$key] );
196214 }
197215 else {
@@ -199,19 +217,23 @@
200218 }
201219 }
202220 else {
203 - $html = call_user_func_array( $callback, $args );
 221+ if ( !is_array( $args ) ) {
 222+ $args = array( $args );
 223+ }
204224
 225+ $value = call_user_func_array( $computeFunction, $args );
 226+
205227 if ( $this->cacheEnabled ) {
206228 if ( is_null( $key ) ) {
207 - $this->cachedChunks[] = $html;
 229+ $this->cachedChunks[] = $value;
208230 }
209231 else {
210 - $this->cachedChunks[$key] = $html;
 232+ $this->cachedChunks[$key] = $value;
211233 }
212234 }
213235 }
214236
215 - $this->getOutput()->addHTML( $html );
 237+ return $value;
216238 }
217239
218240 /**
@@ -246,7 +268,13 @@
247269 * @return string
248270 */
249271 protected function getCacheKeyString() {
250 - return call_user_func_array( 'wfMemcKey', $this->getCacheKey() );
 272+ $keyArgs = $this->getCacheKey();
 273+
 274+ if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
 275+ unset( $keyArgs['action'] );
 276+ }
 277+
 278+ return call_user_func_array( 'wfMemcKey', $keyArgs );
251279 }
252280
253281 /**

Follow-up revisions

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

Comments

#Comment by Nikerabbit (talk | contribs)   07:27, 20 March 2012

I don't like this design as much. You lose all the niceties like ->addWikiText ->addModules and what else if you just have to return string.

#Comment by Jeroen De Dauw (talk | contribs)   14:42, 20 March 2012

Indeed, this is to bad. In my original version you could use these as you would normally. That approach lacked the capability the have multiple blocks of cached HTML (or other values for that matter) though. So it does not work for pages that have non-cacheabale stuff through them. But it was definitely nicer for the simplest use case where you simply want to cache the entire output of a page (which possibly some non-cached stuff before and after).

What I did after that approach was trying to extend OutputPage, to have a addCachedHTML method, which would be ideal. But I did not find a way to make this work. The problem is that you have an instance of OutputPage and want to change it into an instance of CachedOutputPage or whatever. Now since we got comments and checks everywhere to make sure that this object derives from OutputPage, CachedOutputPage better does this. So bye bye decorator pattern. If you know a way to do this without changing all the checks to some interface or whatever, then please let me know, because I think it would be nicer then what I ended up with now. This is what I ended up with after trying to get it to find a way to make it work (and then gave up on it): http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/EducationProgram/specials/SpecialCachedPage.php?revision=114044&view=markup&pathrev=114044

Status & tagging log