r70765 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70764‎ | r70765 | r70766 >
Date:14:58, 9 August 2010
Author:platonides
Status:ok (Comments)
Tags:
Comment:
Move pieces related to ParserOutput expiry into a new CacheTime class.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/parser/ParserOutput.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/parser/ParserOutput.php
@@ -3,16 +3,102 @@
44 * @todo document
55 * @ingroup Parser
66 */
7 -class ParserOutput
 7+
 8+class CacheTime {
 9+ var $mVersion = Parser::VERSION, # Compatibility check
 10+ $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
 11+ $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
 12+ $mContainsOldMagic; # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
 13+
 14+ function getCacheTime() { return $this->mCacheTime; }
 15+
 16+ function containsOldMagic() { return $this->mContainsOldMagic; }
 17+ function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
 18+
 19+ /**
 20+ * setCacheTime() sets the timestamp expressing when the page has been rendered.
 21+ * This doesn not control expiry, see updateCacheExpiry() for that!
 22+ */
 23+ function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
 24+
 25+
 26+ /**
 27+ * Sets the number of seconds after which this object should expire.
 28+ * This value is used with the ParserCache.
 29+ * If called with a value greater than the value provided at any previous call,
 30+ * the new call has no effect. The value returned by getCacheExpiry is smaller
 31+ * or equal to the smallest number that was provided as an argument to
 32+ * updateCacheExpiry().
 33+ */
 34+ function updateCacheExpiry( $seconds ) {
 35+ $seconds = (int)$seconds;
 36+
 37+ if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds )
 38+ $this->mCacheExpiry = $seconds;
 39+
 40+ // hack: set old-style marker for uncacheable entries.
 41+ if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 )
 42+ $this->mCacheTime = -1;
 43+ }
 44+
 45+ /**
 46+ * Returns the number of seconds after which this object should expire.
 47+ * This method is used by ParserCache to determine how long the ParserOutput can be cached.
 48+ * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
 49+ * The value returned by getCacheExpiry is smaller or equal to the smallest number
 50+ * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
 51+ * value of $wgParserCacheExpireTime.
 52+ */
 53+ function getCacheExpiry() {
 54+ global $wgParserCacheExpireTime;
 55+
 56+ if ( $this->mCacheTime < 0 ) return 0; // old-style marker for "not cachable"
 57+
 58+ $expire = $this->mCacheExpiry;
 59+
 60+ if ( $expire === null )
 61+ $expire = $wgParserCacheExpireTime;
 62+ else
 63+ $expire = min( $expire, $wgParserCacheExpireTime );
 64+
 65+ if( $this->containsOldMagic() ) { //compatibility hack
 66+ $expire = min( $expire, 3600 ); # 1 hour
 67+ }
 68+
 69+ if ( $expire <= 0 ) return 0; // not cachable
 70+ else return $expire;
 71+ }
 72+
 73+
 74+ function isCacheable() {
 75+ return $this->getCacheExpiry() > 0;
 76+ }
 77+
 78+ /**
 79+ * Return true if this cached output object predates the global or
 80+ * per-article cache invalidation timestamps, or if it comes from
 81+ * an incompatible older version.
 82+ *
 83+ * @param $touched String: the affected article's last touched timestamp
 84+ * @return Boolean
 85+ */
 86+ public function expired( $touched ) {
 87+ global $wgCacheEpoch;
 88+ return !$this->isCacheable() || // parser says it's uncacheable
 89+ $this->getCacheTime() < $touched ||
 90+ $this->getCacheTime() <= $wgCacheEpoch ||
 91+ $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
 92+ !isset( $this->mVersion ) ||
 93+ version_compare( $this->mVersion, Parser::VERSION, "lt" );
 94+ }
 95+}
 96+
 97+class ParserOutput extends CacheTime
898 {
999 var $mText, # The output text
10100 $mLanguageLinks, # List of the full text of language links, in the order they appear
11101 $mCategories, # Map of category names to sort keys
12 - $mContainsOldMagic, # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
13102 $mTitleText, # title text of the chosen language variant
14 - $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
15 - $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
16 - $mVersion = Parser::VERSION, # Compatibility check
17103 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
18104 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
19105 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
@@ -45,7 +131,6 @@
46132 function getInterwikiLinks() { return $this->mInterwikiLinks; }
47133 function getCategoryLinks() { return array_keys( $this->mCategories ); }
48134 function &getCategories() { return $this->mCategories; }
49 - function getCacheTime() { return $this->mCacheTime; }
50135 function getTitleText() { return $this->mTitleText; }
51136 function getSections() { return $this->mSections; }
52137 function &getLinks() { return $this->mLinks; }
@@ -60,71 +145,15 @@
61146 function getIndexPolicy() { return $this->mIndexPolicy; }
62147 function getTOCHTML() { return $this->mTOCHTML; }
63148
64 - function containsOldMagic() { return $this->mContainsOldMagic; }
65149 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
66150 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
67151 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
68 - function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
69152
70 - /** setCacheTime() sets the timestamp expressing when the page has been rendered.
71 - * This doesn not control expiry, see updateCacheExpiry() for that!
72 - */
73 - function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
74153 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
75154 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
76155 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
77156 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
78157
79 -
80 - /** Sets the number of seconds after which this object should expire.
81 - * This value is used with the ParserCache.
82 - * If called with a value greater than the value provided at any previous call,
83 - * the new call has no effect. The value returned by getCacheExpiry is smaller
84 - * or equal to the smallest number that was provided as an argument to
85 - * updateCacheExpiry().
86 - */
87 - function updateCacheExpiry( $seconds ) {
88 - $seconds = (int)$seconds;
89 -
90 - if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds )
91 - $this->mCacheExpiry = $seconds;
92 -
93 - // hack: set old-style marker for uncacheable entries.
94 - if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 )
95 - $this->mCacheTime = -1;
96 - }
97 -
98 - /** Returns the number of seconds after which this object should expire.
99 - * This method is used by ParserCache to determine how long the ParserOutput can be cached.
100 - * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
101 - * The value returned by getCacheExpiry is smaller or equal to the smallest number
102 - * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
103 - * value of $wgParserCacheExpireTime.
104 - */
105 - function getCacheExpiry() {
106 - global $wgParserCacheExpireTime;
107 -
108 - if ( $this->mCacheTime < 0 ) return 0; // old-style marker for "not cachable"
109 -
110 - $expire = $this->mCacheExpiry;
111 -
112 - if ( $expire === null )
113 - $expire = $wgParserCacheExpireTime;
114 - else
115 - $expire = min( $expire, $wgParserCacheExpireTime );
116 -
117 - if( $this->containsOldMagic() ) { //compatibility hack
118 - $expire = min( $expire, 3600 ); # 1 hour
119 - }
120 -
121 - if ( $expire <= 0 ) return 0; // not cachable
122 - else return $expire;
123 - }
124 -
125 - function isCacheable() {
126 - return $this->getCacheExpiry() > 0;
127 - }
128 -
129158 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
130159 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
131160 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
@@ -220,24 +249,6 @@
221250 }
222251
223252 /**
224 - * Return true if this cached output object predates the global or
225 - * per-article cache invalidation timestamps, or if it comes from
226 - * an incompatible older version.
227 - *
228 - * @param $touched String: the affected article's last touched timestamp
229 - * @return Boolean
230 - */
231 - public function expired( $touched ) {
232 - global $wgCacheEpoch;
233 - return !$this->isCacheable() || // parser says it's uncacheable
234 - $this->getCacheTime() < $touched ||
235 - $this->getCacheTime() <= $wgCacheEpoch ||
236 - $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
237 - !isset( $this->mVersion ) ||
238 - version_compare( $this->mVersion, Parser::VERSION, "lt" );
239 - }
240 -
241 - /**
242253 * Add some text to the <head>.
243254 * If $tag is set, the section with that tag will only be included once
244255 * in a given page.
Index: trunk/phase3/includes/AutoLoader.php
@@ -27,6 +27,7 @@
2828 'BagOStuff' => 'includes/BagOStuff.php',
2929 'Block' => 'includes/Block.php',
3030 'CacheDependency' => 'includes/CacheDependency.php',
 31+ 'CacheTime' => 'includes/parser/ParserOutput.php',
3132 'Category' => 'includes/Category.php',
3233 'Categoryfinder' => 'includes/Categoryfinder.php',
3334 'CategoryPage' => 'includes/CategoryPage.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r70783Use only the page relevant pieces in the parser cache key. Eg. two users with...platonides21:53, 9 August 2010

Comments

#Comment by Platonides (talk | contribs)   15:06, 9 August 2010

Part of making this a separate commit was to give the lengthy explanation in the commit sumamry, and I forget. :/

The serialized structure changes as the affected fields appear in a different order, but they unserialize fine from one to the other. Advantage of interpreted languages.

Thus, existing ParserCache entries continue working after the change.

Status & tagging log