3 # Object for fast lookup of IP blocks
4 # Represents a memcached value, and in some sense, the entire ipblocks table
8 var $mData = false, $mMemcKey;
10 function BlockCache( $deferLoad = false, $dbName = '' ) {
13 if ( $dbName == '' ) {
17 $this->mMemcKey
= $dbName.':ipblocks';
24 # Load the blocks from the database and save them to memcached
25 function loadFromDB() {
26 global $wgUseMemCached, $wgMemc;
27 $this->mData
= array();
28 # Selecting FOR UPDATE is a convenient way to serialise the memcached and DB operations,
29 # which is necessary even though we don't update the DB
30 if ( $wgUseMemCached ) {
31 Block
::enumBlocks( 'wfBlockCacheInsert', '', EB_FOR_UPDATE
);
32 $wgMemc->set( $this->mMemcKey
, $this->mData
, 0 );
34 Block
::enumBlocks( 'wfBlockCacheInsert', '' );
38 # Load the cache from memcached or, if that's not possible, from the DB
40 global $wgUseMemCached, $wgMemc;
42 if ( $this->mData
=== false) {
44 if ( $wgUseMemCached ) {
45 $this->mData
= $wgMemc->get( $this->mMemcKey
);
48 if ( !is_array( $this->mData
) ) {
54 # Add a block to the cache
55 function insert( &$block ) {
56 if ( $block->mUser
== 0 ) {
57 $nb = $block->getNetworkBits();
58 $ipint = $block->getIntegerAddr();
59 $index = $ipint >> ( 32 - $nb );
61 if ( !array_key_exists( $nb, $this->mData
) ) {
62 $this->mData
[$nb] = array();
65 $this->mData
[$nb][$index] = 1;
69 # Find out if a given IP address is blocked
72 $ipint = ip2long( $ip );
75 foreach ( $this->mData
as $networkBits => $blockInts ) {
76 if ( array_key_exists( $ipint >> ( 32 - $networkBits ), $blockInts ) ) {
82 # Clear low order bits
83 if ( $networkBits != 32 ) {
84 $ip .= '/'.$networkBits;
85 $ip = Block
::normaliseRange( $ip );
96 # Clear the local cache
97 # There was once a clear() to clear memcached too, but I deleted it
98 function clearLocal() {
103 function wfBlockCacheInsert( $block, $tag ) {
104 global $wgBlockCache;
105 $wgBlockCache->insert( $block );