objectcache: add WANObjectCacheReaper for assuring purges
[mediawiki.git] / includes / cache / LinkCache.php
blobb720decb869a43b1ff2f8b0af81d2bcdc1b7ab2d
1 <?php
2 /**
3 * Page existence cache.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Cache
23 use MediaWiki\Linker\LinkTarget;
24 use MediaWiki\MediaWikiServices;
26 /**
27 * Cache for article titles (prefixed DB keys) and ids linked from one source
29 * @ingroup Cache
31 class LinkCache {
32 /** @var HashBagOStuff */
33 private $mGoodLinks;
34 /** @var HashBagOStuff */
35 private $mBadLinks;
36 /** @var WANObjectCache */
37 private $wanCache;
39 /** @var bool */
40 private $mForUpdate = false;
42 /** @var TitleFormatter */
43 private $titleFormatter;
45 /**
46 * How many Titles to store. There are two caches, so the amount actually
47 * stored in memory can be up to twice this.
49 const MAX_SIZE = 10000;
51 public function __construct( TitleFormatter $titleFormatter, WANObjectCache $cache ) {
52 $this->mGoodLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
53 $this->mBadLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
54 $this->wanCache = $cache;
55 $this->titleFormatter = $titleFormatter;
58 /**
59 * Get an instance of this class.
61 * @return LinkCache
62 * @deprecated since 1.28, use MediaWikiServices instead
64 public static function singleton() {
65 return MediaWikiServices::getInstance()->getLinkCache();
68 /**
69 * General accessor to get/set whether the master DB should be used
71 * This used to also set the FOR UPDATE option (locking the rows read
72 * in order to avoid link table inconsistency), which was later removed
73 * for performance on wikis with a high edit rate.
75 * @param bool $update
76 * @return bool
78 public function forUpdate( $update = null ) {
79 return wfSetVar( $this->mForUpdate, $update );
82 /**
83 * @param string $title Prefixed DB key
84 * @return int Page ID or zero
86 public function getGoodLinkID( $title ) {
87 $info = $this->mGoodLinks->get( $title );
88 if ( !$info ) {
89 return 0;
91 return $info['id'];
94 /**
95 * Get a field of a title object from cache.
96 * If this link is not a cached good title, it will return NULL.
97 * @param LinkTarget $target
98 * @param string $field ('length','redirect','revision','model')
99 * @return string|int|null
101 public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
102 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
103 $info = $this->mGoodLinks->get( $dbkey );
104 if ( !$info ) {
105 return null;
107 return $info[$field];
111 * @param string $title Prefixed DB key
112 * @return bool
114 public function isBadLink( $title ) {
115 // Use get() to ensure it records as used for LRU.
116 return $this->mBadLinks->get( $title ) !== false;
120 * Add a link for the title to the link cache
122 * @param int $id Page's ID
123 * @param LinkTarget $target
124 * @param int $len Text's length
125 * @param int $redir Whether the page is a redirect
126 * @param int $revision Latest revision's ID
127 * @param string|null $model Latest revision's content model ID
128 * @param string|null $lang Language code of the page, if not the content language
130 public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
131 $revision = 0, $model = null, $lang = null
133 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
134 $this->mGoodLinks->set( $dbkey, [
135 'id' => (int)$id,
136 'length' => (int)$len,
137 'redirect' => (int)$redir,
138 'revision' => (int)$revision,
139 'model' => $model ? (string)$model : null,
140 'lang' => $lang ? (string)$lang : null,
141 ] );
145 * Same as above with better interface.
146 * @since 1.19
147 * @param LinkTarget $target
148 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
149 * page_latest and page_content_model
151 public function addGoodLinkObjFromRow( LinkTarget $target, $row ) {
152 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
153 $this->mGoodLinks->set( $dbkey, [
154 'id' => intval( $row->page_id ),
155 'length' => intval( $row->page_len ),
156 'redirect' => intval( $row->page_is_redirect ),
157 'revision' => intval( $row->page_latest ),
158 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
159 'lang' => !empty( $row->page_lang ) ? strval( $row->page_lang ) : null,
160 ] );
164 * @param LinkTarget $target
166 public function addBadLinkObj( LinkTarget $target ) {
167 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
168 if ( !$this->isBadLink( $dbkey ) ) {
169 $this->mBadLinks->set( $dbkey, 1 );
174 * @param string $title Prefixed DB key
176 public function clearBadLink( $title ) {
177 $this->mBadLinks->delete( $title );
181 * @param LinkTarget $target
183 public function clearLink( LinkTarget $target ) {
184 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
185 $this->mBadLinks->delete( $dbkey );
186 $this->mGoodLinks->delete( $dbkey );
190 * Add a title to the link cache, return the page_id or zero if non-existent
192 * @deprecated since 1.27, unused
193 * @param string $title Prefixed DB key
194 * @return int Page ID or zero
196 public function addLink( $title ) {
197 $nt = Title::newFromDBkey( $title );
198 if ( !$nt ) {
199 return 0;
201 return $this->addLinkObj( $nt );
205 * Fields that LinkCache needs to select
207 * @since 1.28
208 * @return array
210 public static function getSelectFields() {
211 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
213 $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
214 if ( $wgContentHandlerUseDB ) {
215 $fields[] = 'page_content_model';
217 if ( $wgPageLanguageUseDB ) {
218 $fields[] = 'page_lang';
221 return $fields;
225 * Add a title to the link cache, return the page_id or zero if non-existent
227 * @param LinkTarget $nt LinkTarget object to add
228 * @return int Page ID or zero
230 public function addLinkObj( LinkTarget $nt ) {
231 $key = $this->titleFormatter->getPrefixedDBkey( $nt );
232 if ( $this->isBadLink( $key ) || $nt->isExternal()
233 || $nt->inNamespace( NS_SPECIAL )
235 return 0;
237 $id = $this->getGoodLinkID( $key );
238 if ( $id != 0 ) {
239 return $id;
242 if ( $key === '' ) {
243 return 0;
246 // Cache template/file pages as they are less often viewed but heavily used
247 if ( $this->mForUpdate ) {
248 $row = $this->fetchPageRow( wfGetDB( DB_MASTER ), $nt );
249 } elseif ( $this->isCacheable( $nt ) ) {
250 // These pages are often transcluded heavily, so cache them
251 $cache = $this->wanCache;
252 $row = $cache->getWithSetCallback(
253 $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
254 $cache::TTL_DAY,
255 function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
256 $dbr = wfGetDB( DB_REPLICA );
257 $setOpts += Database::getCacheSetOptions( $dbr );
259 $row = $this->fetchPageRow( $dbr, $nt );
260 $mtime = $row ? wfTimestamp( TS_UNIX, $row->page_touched ) : false;
261 $ttl = $cache->adaptiveTTL( $mtime, $ttl );
263 return $row;
266 } else {
267 $row = $this->fetchPageRow( wfGetDB( DB_REPLICA ), $nt );
270 if ( $row ) {
271 $this->addGoodLinkObjFromRow( $nt, $row );
272 $id = intval( $row->page_id );
273 } else {
274 $this->addBadLinkObj( $nt );
275 $id = 0;
278 return $id;
282 * @param WANObjectCache $cache
283 * @param TitleValue $t
284 * @return string[]
285 * @since 1.28
287 public function getMutableCacheKeys( WANObjectCache $cache, TitleValue $t ) {
288 if ( $this->isCacheable( $t ) ) {
289 return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
292 return [];
295 private function isCacheable( LinkTarget $title ) {
296 return ( $title->inNamespace( NS_TEMPLATE ) || $title->inNamespace( NS_FILE ) );
299 private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {
300 $fields = self::getSelectFields();
301 if ( $this->isCacheable( $nt ) ) {
302 $fields[] = 'page_touched';
305 return $db->selectRow(
306 'page',
307 $fields,
308 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
309 __METHOD__
314 * Purge the link cache for a title
316 * @param LinkTarget $title
317 * @since 1.28
319 public function invalidateTitle( LinkTarget $title ) {
320 if ( $this->isCacheable( $title ) ) {
321 $cache = ObjectCache::getMainWANInstance();
322 $cache->delete(
323 $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
329 * Clears cache
331 public function clear() {
332 $this->mGoodLinks->clear();
333 $this->mBadLinks->clear();