Added local blacklist caching

* Also cleaned up the shared blacklist keys
* The cache keys now use getWithSetCallback(), which does
  things like preemptive re-caching to avoid cache slams

Change-Id: Ifda27edaf76770e84b4ece711d3c8bcb8e924674
This commit is contained in:
Aaron Schulz 2015-07-28 15:01:39 -07:00 committed by Ori.livneh
parent 6abf0a1f54
commit 7a02693e9b

View file

@ -212,7 +212,16 @@ abstract class BaseBlacklist {
* @return array Regular expressions * @return array Regular expressions
*/ */
public function getLocalBlacklists() { public function getLocalBlacklists() {
return SpamRegexBatch::regexesFromMessage( "{$this->getBlacklistType()}-blacklist", $this ); $that = $this;
$type = $this->getBlacklistType();
return ObjectCache::getMainWANInstance()->getWithSetCallback(
wfMemcKey( 'spamblacklist', $type, 'blacklist-regex' ),
function () use ( $that, $type ) {
return SpamRegexBatch::regexesFromMessage( "{$type}-blacklist", $that );
},
$this->expiryTime
);
} }
/** /**
@ -221,7 +230,16 @@ abstract class BaseBlacklist {
* @return array Regular expressions * @return array Regular expressions
*/ */
public function getWhitelists() { public function getWhitelists() {
return SpamRegexBatch::regexesFromMessage( "{$this->getBlacklistType()}-whitelist", $this ); $that = $this;
$type = $this->getBlacklistType();
return ObjectCache::getMainWANInstance()->getWithSetCallback(
wfMemcKey( 'spamblacklist', $type, 'whitelist-regex' ),
function () use ( $that, $type ) {
return SpamRegexBatch::regexesFromMessage( "{$type}-whitelist", $that );
},
$this->expiryTime
);
} }
/** /**
@ -229,7 +247,6 @@ abstract class BaseBlacklist {
* @return array * @return array
*/ */
function getSharedBlacklists() { function getSharedBlacklists() {
global $wgDBname;
$listType = $this->getBlacklistType(); $listType = $this->getBlacklistType();
wfDebugLog( 'SpamBlacklist', "Loading $listType regex..." ); wfDebugLog( 'SpamBlacklist', "Loading $listType regex..." );
@ -240,27 +257,40 @@ abstract class BaseBlacklist {
return array(); return array();
} }
$cache = ObjectCache::getMainWANInstance(); $miss = false;
// This used to be cached per-site, but that could be bad on a shared
// server where not all wikis have the same configuration.
$cachedRegexes = $cache->get( "$wgDBname:{$listType}_blacklist_regexes" );
if( is_array( $cachedRegexes ) ) {
wfDebugLog( 'SpamBlacklist', "Got shared spam regexes from cache\n" );
return $cachedRegexes;
}
$regexes = $this->buildSharedBlacklists(); $that = $this;
$cache->set( "$wgDBname:{$listType}_blacklist_regexes", $regexes, $this->expiryTime ); $regexes = ObjectCache::getMainWANInstance()->getWithSetCallback(
// This used to be cached per-site, but that could be bad on a shared
// server where not all wikis have the same configuration.
wfMemcKey( 'spamblacklist', $listType, 'shared-blacklist-regex' ),
function () use ( $that, &$miss ) {
$miss = true;
return $that->buildSharedBlacklists();
},
$this->expiryTime
);
if ( !$miss ) {
wfDebugLog( 'SpamBlacklist', "Got shared spam regexes from cache\n" );
}
return $regexes; return $regexes;
} }
/**
* Clear all primary blacklist cache keys
*
* @note: this method is unused atm
*/
function clearCache() { function clearCache() {
global $wgDBname;
$listType = $this->getBlacklistType(); $listType = $this->getBlacklistType();
$cache = ObjectCache::getMainWANInstance(); $cache = ObjectCache::getMainWANInstance();
$cache->delete( "$wgDBname:{$listType}_blacklist_regexes" ); $cache->delete( wfMemcKey( 'spamblacklist', $listType, 'shared-blacklist-regex' ) );
$cache->delete( wfMemcKey( 'spamblacklist', $listType, 'blacklist-regex' ) );
$cache->delete( wfMemcKey( 'spamblacklist', $listType, 'whitelist-regex' ) );
wfDebugLog( 'SpamBlacklist', "$listType blacklist local cache cleared.\n" ); wfDebugLog( 'SpamBlacklist', "$listType blacklist local cache cleared.\n" );
} }