r114164 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114163‎ | r114164 | r114165 >
Date:18:40, 19 March 2012
Author:jeroendedauw
Status:reverted
Tags:gerritmigration 
Comment:
some refactoring to allow for nicer usage in deriving classes
Modified paths:
  • /trunk/phase3/includes/specials/SpecialCachedPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialCachedPage.php
@@ -4,11 +4,12 @@
55 * Abstract special page class with scaffolding for caching the HTML output.
66 *
77 * To enable the caching functionality, the cacheExpiry field should be set
8 - * in the constructor.
 8+ * before parent::execute is called (and it should be called from execute).
99 *
1010 * To add HTML that should be cached, use addCachedHTML like this:
1111 * $this->addCachedHTML( array( $this, 'displayCachedContent' ) );
1212 *
 13+ * Before the first addCachedHTML call, you should call $this->startCache();
1314 * After adding the last HTML that should be cached, call $this->saveCache();
1415 *
1516 * @since 1.20
@@ -25,9 +26,9 @@
2627 * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
2728 *
2829 * @since 1.20
29 - * @var integer|null
 30+ * @var integer
3031 */
31 - protected $cacheExpiry = null;
 32+ protected $cacheExpiry = 3600;
3233
3334 /**
3435 * List of HTML chunks to be cached (if !hasCached) or that where cashed (of hasCached).
@@ -49,24 +50,46 @@
5051 protected $hasCached = null;
5152
5253 /**
53 - * Main method.
 54+ * If the cache is enabled or not.
5455 *
5556 * @since 1.20
 57+ * @var boolean
 58+ */
 59+ protected $cacheEnabled = true;
 60+
 61+ /**
 62+ * Sets if the cache should be enabled or not.
5663 *
57 - * @param string|null $subPage
 64+ * @since 1.20
 65+ * @param boolean $cacheEnabled
5866 */
59 - public function execute( $subPage ) {
 67+ public function setCacheEnabled( $cacheEnabled ) {
 68+ $this->cacheEnabled = $cacheEnabled;
 69+ }
 70+
 71+ /**
 72+ * Initializes the caching.
 73+ * Should be called before the first time anything is added via addCachedHTML.
 74+ *
 75+ * @since 1.20
 76+ *
 77+ * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
 78+ * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
 79+ */
 80+ public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
 81+ if ( !is_null( $cacheExpiry ) ) {
 82+ $this->cacheExpiry = $cacheExpiry;
 83+ }
 84+
 85+ if ( !is_null( $cacheEnabled ) ) {
 86+ $this->setCacheEnabled( $cacheEnabled );
 87+ }
 88+
6089 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
6190 $this->hasCached = false;
6291 }
6392
64 - if ( !is_null( $this->cacheExpiry ) ) {
65 - $this->initCaching();
66 -
67 - if ( $this->hasCached === true ) {
68 - $this->getOutput()->setSubtitle( $this->getCachedNotice( $subPage ) );
69 - }
70 - }
 93+ $this->initCaching();
7194 }
7295
7396 /**
@@ -75,17 +98,15 @@
7699 *
77100 * @since 1.20
78101 *
79 - * @param string|null $subPage
80 - *
81102 * @return string
82103 */
83 - protected function getCachedNotice( $subPage ) {
 104+ protected function getCachedNotice() {
84105 $refreshArgs = $this->getRequest()->getQueryValues();
85106 unset( $refreshArgs['title'] );
86107 $refreshArgs['action'] = 'purge';
87108
88109 $refreshLink = Linker::link(
89 - $this->getTitle( $subPage ),
 110+ $this->getTitle( $this->getTitle()->getSubpageText() ),
90111 $this->msg( 'cachedspecial-refresh-now' )->escaped(),
91112 array(),
92113 $refreshArgs
@@ -114,13 +135,21 @@
115136 */
116137 protected function initCaching() {
117138 if ( is_null( $this->hasCached ) ) {
118 - $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKey() );
 139+ $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKeyString() );
119140
120141 $this->hasCached = is_array( $cachedChunks );
121142 $this->cachedChunks = $this->hasCached ? $cachedChunks : array();
 143+
 144+ $this->onCacheInitialized();
122145 }
123146 }
124147
 148+ protected function onCacheInitialized() {
 149+ if ( $this->hasCached ) {
 150+ $this->getOutput()->setSubtitle( $this->getCachedNotice() );
 151+ }
 152+ }
 153+
125154 /**
126155 * Add some HTML to be cached.
127156 * This is done by providing a callback function that should
@@ -136,7 +165,7 @@
137166 public function addCachedHTML( $callback, $args = array(), $key = null ) {
138167 $this->initCaching();
139168
140 - if ( $this->hasCached ) {
 169+ if ( $this->cacheEnabled && $this->hasCached ) {
141170 $html = '';
142171
143172 if ( is_null( $key ) ) {
@@ -166,12 +195,14 @@
167196 else {
168197 $html = call_user_func_array( $callback, $args );
169198
170 - if ( is_null( $key ) ) {
171 - $this->cachedChunks[] = $html;
 199+ if ( $this->cacheEnabled ) {
 200+ if ( is_null( $key ) ) {
 201+ $this->cachedChunks[] = $html;
 202+ }
 203+ else {
 204+ $this->cachedChunks[$key] = $html;
 205+ }
172206 }
173 - else {
174 - $this->cachedChunks[$key] = $html;
175 - }
176207 }
177208
178209 $this->getOutput()->addHTML( $html );
@@ -184,8 +215,8 @@
185216 * @since 1.20
186217 */
187218 public function saveCache() {
188 - if ( $this->hasCached === false && !empty( $this->cachedChunks ) ) {
189 - wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKey(), $this->cachedChunks, $this->cacheExpiry );
 219+ if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
 220+ wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry );
190221 }
191222 }
192223
@@ -208,8 +239,22 @@
209240 *
210241 * @return string
211242 */
 243+ protected function getCacheKeyString() {
 244+ return call_user_func_array( 'wfMemcKey', $this->getCacheKey() );
 245+ }
 246+
 247+ /**
 248+ * Returns the variables used to constructed the cache key in an array.
 249+ *
 250+ * @since 1.20
 251+ *
 252+ * @return array
 253+ */
212254 protected function getCacheKey() {
213 - return wfMemcKey( $this->mName, $this->getLanguage()->getCode() );
 255+ return array(
 256+ $this->mName,
 257+ $this->getLanguage()->getCode()
 258+ );
214259 }
215260
216261 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r114175fix for r114164jeroendedauw20:05, 19 March 2012
r114326Revert r114067, r114071, r114075, r114079, r114081, r114082, r114084, r114086......catrope23:03, 20 March 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r114081adding special page with scaffolding for caching chunks of HTMLjeroendedauw22:26, 17 March 2012

Status & tagging log