Tokenizer caching back to APC

Partial revert of I4dd81a723e2bdb828b90594ad66a3918d8ec5b6c.
Thinking again of it, I think it's not worth it to have this data over
the network. Plus, given that it's not-that-slow to be computed, I think
there can only be a performance gain in using APC (as opposed to e.g.
memcached/redis) for 99.9% of the filters.

Change-Id: I8c6a4a95ec12c18ede8e6419540f7a2ac943457c
This commit is contained in:
Daimona Eaytoy 2019-05-28 10:11:03 +02:00
parent bac286dd16
commit 53f03e5301
2 changed files with 9 additions and 20 deletions

View file

@ -1,7 +1,5 @@
<?php
use MediaWiki\MediaWikiServices;
/**
* Tokenizer for AbuseFilter rules.
*/
@ -69,13 +67,13 @@ class AbuseFilterTokenizer {
/**
* Get a cache key used to store the tokenized code
*
* @param WANObjectCache $cache
* @param BagOStuff $cache
* @param string $code Not yet tokenized
* @return string
* @internal
*/
public static function getCacheKey( WANObjectCache $cache, $code ) {
return $cache->makeGlobalKey( __CLASS__, crc32( $code ) );
public static function getCacheKey( BagOStuff $cache, $code ) {
return $cache->makeGlobalKey( __CLASS__, self::CACHE_VERSION, crc32( $code ) );
}
/**
@ -85,15 +83,14 @@ class AbuseFilterTokenizer {
* @return array[]
*/
public static function getTokens( $code ) {
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$cache = ObjectCache::getLocalServerInstance( 'hash' );
$tokens = $cache->getWithSetCallback(
self::getCacheKey( $cache, $code ),
$cache::TTL_DAY,
function ( $oldValue, &$ttl, array &$setOpts ) use ( $code ) {
function () use ( $code ) {
return self::tokenize( $code );
},
[ 'version' => self::CACHE_VERSION ]
}
);
return $tokens;

View file

@ -117,23 +117,15 @@ class AbuseFilterTokenizerTest extends AbuseFilterParserTestCase {
* @dataProvider provideCode
*/
public function testCaching( $code ) {
$cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
$this->setService( 'MainWANObjectCache', $cache );
$cache = new HashBagOStuff();
$this->setService( 'LocalServerObjectCache', $cache );
$key = AbuseFilterTokenizer::getCacheKey( $cache, $code );
// Other tests may have already cached the same code.
$cache->delete( $key );
AbuseFilterTokenizer::getTokens( $code );
$cached = $cache->getWithSetCallback(
$key,
$cache::TTL_DAY,
function () {
return false;
},
[ 'version' => 1 ]
);
$this->assertNotFalse( $cached );
$this->assertNotFalse( $cache->get( $key ) );
}
/**