X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FBlockCache.php;h=43b77a7db7b3a9115ea81a8d16f3b1c60203fd44;hb=8ffff3e2e073ee4ce13e35799cbec94997b65a20;hp=e18182cfbbd8a37a82210d5c885cb119fb949517;hpb=0332f4c59809e5a89f626e8969cd76261819f432;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/BlockCache.php b/includes/BlockCache.php index e18182cfbb..43b77a7db7 100644 --- a/includes/BlockCache.php +++ b/includes/BlockCache.php @@ -1,57 +1,64 @@ mMemcKey = "$dbName:ipblocks"; + $this->mMemcKey = $dbName.':ipblocks'; if ( !$deferLoad ) { $this->load(); } } - function load() - { + # Load the blocks from the database and save them to memcached + function loadFromDB() { + global $wgUseMemCached, $wgMemc; + $this->mData = array(); + # Selecting FOR UPDATE is a convenient way to serialise the memcached and DB operations, + # which is necessary even though we don't update the DB + if ( $wgUseMemCached ) { + Block::enumBlocks( 'wfBlockCacheInsert', '', EB_FOR_UPDATE ); + $wgMemc->set( $this->mMemcKey, $this->mData, 0 ); + } else { + Block::enumBlocks( 'wfBlockCacheInsert', '' ); + } + } + + # Load the cache from memcached or, if that's not possible, from the DB + function load() { global $wgUseMemCached, $wgMemc; if ( $this->mData === false) { - $this->mData = array(); - - $saveMemc = false; # Try memcached if ( $wgUseMemCached ) { $this->mData = $wgMemc->get( $this->mMemcKey ); - if ( !$this->mData ) { - $saveMemc = true; - } } - if ( $this->mData === false || is_null( $this->mData ) ) { - # Load from DB - $this->mData = array(); - Block::enumBlocks( "wfBlockCacheInsert", "" ); # Calls $this->insert() - } - - if ( $saveMemc ) { - $wgMemc->set( $this->mMemcKey, $this->mData, 0 ); + if ( !is_array( $this->mData ) ) { + $this->loadFromDB(); } } } - function insert( &$block ) - { + # Add a block to the cache + function insert( &$block ) { if ( $block->mUser == 0 ) { $nb = $block->getNetworkBits(); $ipint = $block->getIntegerAddr(); @@ -64,9 +71,9 @@ class BlockCache $this->mData[$nb][$index] = 1; } } - - function get( $ip ) - { + + # Find out if a given IP address is blocked + function get( $ip ) { $this->load(); $ipint = ip2long( $ip ); $blocked = false; @@ -80,7 +87,7 @@ class BlockCache if ( $blocked ) { # Clear low order bits if ( $networkBits != 32 ) { - $ip .= "/$networkBits"; + $ip .= '/'.$networkBits; $ip = Block::normaliseRange( $ip ); } $block = new Block(); @@ -92,24 +99,14 @@ class BlockCache return $block; } - function clear() - { - global $wgUseMemCached, $wgMemc; - - $this->mData = false; - if ( $wgUseMemCached ) { - $wgMemc->delete( $this->mMemcKey ); - } - } - - function clearLocal() - { + # Clear the local cache + # There was once a clear() to clear memcached too, but I deleted it + function clearLocal() { $this->mData = false; } } -function wfBlockCacheInsert( $block, $tag ) -{ +function wfBlockCacheInsert( $block, $tag ) { global $wgBlockCache; $wgBlockCache->insert( $block ); }