Legoktm has uploaded a new change for review.
https://gerrit.wikimedia.org/r/227168
Change subject: Don't let LinkCache grow indefinitely
......................................................................
Don't let LinkCache grow indefinitely
Especially in long running maintenance scripts, this can be problematic.
LinkCache is now LRU-based, and will store a maximum of 500 good titles,
and 500 bad ones.
LinkCache::getGoodLinks() is deprecated since it is problematic to
support in this implementation and is unused.
Bug: T106998
Change-Id: I1328149d65a5e75a5d6e10cb2686a099562a1847
---
M includes/cache/LinkCache.php
1 file changed, 54 insertions(+), 25 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/68/227168/1
diff --git a/includes/cache/LinkCache.php b/includes/cache/LinkCache.php
index e8cc5e5..3081616 100644
--- a/includes/cache/LinkCache.php
+++ b/includes/cache/LinkCache.php
@@ -29,17 +29,33 @@
class LinkCache {
// Increment $mClassVer whenever old serialized versions of this class
// becomes incompatible with the new version.
- private $mClassVer = 4;
+ private $mClassVer = 5;
- private $mGoodLinks = array();
- private $mGoodLinkFields = array();
- private $mBadLinks = array();
+ /**
+ * @var MapCacheLRU
+ */
+ private $mGoodLinks;
+ /**
+ * @var MapCacheLRU
+ */
+ private $mBadLinks;
private $mForUpdate = false;
+
+ /**
+ * How many Titles to store. There are two caches, so the amount
actually
+ * stored in memory can be up to twice this.
+ */
+ const MAX_SIZE = 500;
/**
* @var LinkCache
*/
protected static $instance;
+
+ public function __construct() {
+ $this->mGoodLinks = new MapCacheLRU( self::MAX_SIZE );
+ $this->mBadLinks = new MapCacheLRU( self::MAX_SIZE );
+ }
/**
* Get an instance of this class.
@@ -90,8 +106,9 @@
* @return int
*/
public function getGoodLinkID( $title ) {
- if ( array_key_exists( $title, $this->mGoodLinks ) ) {
- return $this->mGoodLinks[$title];
+ if ( $this->mGoodLinks->has( $title ) ) {
+ $info = $this->mGoodLinks->get( $title );
+ return $info['id'];
} else {
return 0;
}
@@ -106,8 +123,9 @@
*/
public function getGoodLinkFieldObj( $title, $field ) {
$dbkey = $title->getPrefixedDBkey();
- if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
- return $this->mGoodLinkFields[$dbkey][$field];
+ if ( $this->mGoodLinks->has( $dbkey ) ) {
+ $info = $this->mGoodLinks->get( $dbkey );
+ return $info[$field];
} else {
return null;
}
@@ -118,7 +136,8 @@
* @return bool
*/
public function isBadLink( $title ) {
- return array_key_exists( $title, $this->mBadLinks );
+ // We need to use get here since has will not call ping.
+ return $this->mBadLinks->get( $title ) !== null;
}
/**
@@ -135,13 +154,13 @@
$revision = 0, $model = null
) {
$dbkey = $title->getPrefixedDBkey();
- $this->mGoodLinks[$dbkey] = (int)$id;
- $this->mGoodLinkFields[$dbkey] = array(
+ $this->mGoodLinks->set( $dbkey, array(
+ 'id' => (int)$id,
'length' => (int)$len,
'redirect' => (int)$redir,
'revision' => (int)$revision,
'model' => $model ? (string)$model : null,
- );
+ ) );
}
/**
@@ -153,13 +172,13 @@
*/
public function addGoodLinkObjFromRow( $title, $row ) {
$dbkey = $title->getPrefixedDBkey();
- $this->mGoodLinks[$dbkey] = intval( $row->page_id );
- $this->mGoodLinkFields[$dbkey] = array(
+ $this->mGoodLinks->set( $dbkey, array(
+ 'id' => intval( $row->page_id ),
'length' => intval( $row->page_len ),
'redirect' => intval( $row->page_is_redirect ),
'revision' => intval( $row->page_latest ),
'model' => !empty( $row->page_content_model ) ? strval(
$row->page_content_model ) : null,
- );
+ ) );
}
/**
@@ -168,12 +187,12 @@
public function addBadLinkObj( $title ) {
$dbkey = $title->getPrefixedDBkey();
if ( !$this->isBadLink( $dbkey ) ) {
- $this->mBadLinks[$dbkey] = 1;
+ $this->mBadLinks->set( $dbkey, 1 );
}
}
public function clearBadLink( $title ) {
- unset( $this->mBadLinks[$title] );
+ $this->mBadLinks->clear( array( $title ) );
}
/**
@@ -181,17 +200,28 @@
*/
public function clearLink( $title ) {
$dbkey = $title->getPrefixedDBkey();
- unset( $this->mBadLinks[$dbkey] );
- unset( $this->mGoodLinks[$dbkey] );
- unset( $this->mGoodLinkFields[$dbkey] );
+ $this->mBadLinks->clear( array( $dbkey ) );
+ $this->mGoodLinks->clear( array( $dbkey ) );
}
+
+ /**
+ * @deprecated since 1.26
+ * @return array
+ */
public function getGoodLinks() {
- return $this->mGoodLinks;
+ wfDeprecated( __METHOD__, '1.26' );
+ $links = array();
+ foreach ( $this->mGoodLinks->getAllKeys() as $key ) {
+ $info = $this->mGoodLinks->get( $key );
+ $links[$key] = $info['id'];
+ }
+
+ return $links;
}
public function getBadLinks() {
- return array_keys( $this->mBadLinks );
+ return $this->mBadLinks->getAllKeys();
}
/**
@@ -262,8 +292,7 @@
* Clears cache
*/
public function clear() {
- $this->mGoodLinks = array();
- $this->mGoodLinkFields = array();
- $this->mBadLinks = array();
+ $this->mGoodLinks->clear();
+ $this->mBadLinks->clear();
}
}
--
To view, visit https://gerrit.wikimedia.org/r/227168
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I1328149d65a5e75a5d6e10cb2686a099562a1847
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits