<?php
-
-# Object for fast lookup of IP blocks
-# Represents a memcached value, and in some sense, the entire ipblocks table
-
+/**
+ * Contain the blockcache class
+ * @package MediaWiki
+ */
+
+/**
+ * Object for fast lookup of IP blocks
+ * Represents a memcached value, and in some sense, the entire ipblocks table
+ * @package MediaWiki
+ */
class BlockCache
{
var $mData = false, $mMemcKey;
- function BlockCache( $deferLoad = false, $dbName = '' )
- {
+ function BlockCache( $deferLoad = false, $dbName = '' ) {
global $wgDBname;
if ( $dbName == '' ) {
}
}
- 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) {
- $saveMemc = false;
# Try memcached
if ( $wgUseMemCached ) {
$this->mData = $wgMemc->get( $this->mMemcKey );
- if ( !$this->mData ) {
- $saveMemc = true;
- }
}
if ( !is_array( $this->mData ) ) {
- # Load from DB
- $this->mData = array();
- Block::enumBlocks( 'wfBlockCacheInsert', '' ); # Calls $this->insert()
- }
-
- if ( $saveMemc ) {
- $wgMemc->set( $this->mMemcKey, $this->mData, 0 );
+ $this->loadFromDB();
}
}
}
- function insert( &$block )
- {
+ # Add a block to the cache
+ function insert( &$block ) {
if ( $block->mUser == 0 ) {
$nb = $block->getNetworkBits();
$ipint = $block->getIntegerAddr();
$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;
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 );
}