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 = '' )
14 if ( $dbName == '' ) {
18 $this->mMemcKey
= $dbName.':ipblocks';
27 global $wgUseMemCached, $wgMemc;
29 if ( $this->mData
=== false) {
32 if ( $wgUseMemCached ) {
33 $this->mData
= $wgMemc->get( $this->mMemcKey
);
34 if ( !$this->mData
) {
39 if ( !is_array( $this->mData
) ) {
41 $this->mData
= array();
42 Block
::enumBlocks( 'wfBlockCacheInsert', '' ); # Calls $this->insert()
46 $wgMemc->set( $this->mMemcKey
, $this->mData
, 0 );
51 function insert( &$block )
53 if ( $block->mUser
== 0 ) {
54 $nb = $block->getNetworkBits();
55 $ipint = $block->getIntegerAddr();
56 $index = $ipint >> ( 32 - $nb );
58 if ( !array_key_exists( $nb, $this->mData
) ) {
59 $this->mData
[$nb] = array();
62 $this->mData
[$nb][$index] = 1;
69 $ipint = ip2long( $ip );
72 foreach ( $this->mData
as $networkBits => $blockInts ) {
73 if ( array_key_exists( $ipint >> ( 32 - $networkBits ), $blockInts ) ) {
79 # Clear low order bits
80 if ( $networkBits != 32 ) {
81 $ip .= '/'.$networkBits;
82 $ip = Block
::normaliseRange( $ip );
95 global $wgUseMemCached, $wgMemc;
98 if ( $wgUseMemCached ) {
99 $wgMemc->delete( $this->mMemcKey
);
103 function clearLocal()
105 $this->mData
= false;
109 function wfBlockCacheInsert( $block, $tag )
111 global $wgBlockCache;
112 $wgBlockCache->insert( $block );