3 * Contain the blockcache class
8 * Object for fast lookup of IP blocks
9 * Represents a memcached value, and in some sense, the entire ipblocks table
14 var $mData = false, $mMemcKey;
16 function BlockCache( $deferLoad = false, $dbName = '' ) {
19 if ( $dbName == '' ) {
23 $this->mMemcKey
= $dbName.':ipblocks';
30 # Load the blocks from the database and save them to memcached
31 function loadFromDB() {
32 global $wgUseMemCached, $wgMemc;
33 $this->mData
= array();
34 # Selecting FOR UPDATE is a convenient way to serialise the memcached and DB operations,
35 # which is necessary even though we don't update the DB
36 if ( $wgUseMemCached ) {
37 Block
::enumBlocks( 'wfBlockCacheInsert', '', EB_FOR_UPDATE
);
38 $wgMemc->set( $this->mMemcKey
, $this->mData
, 0 );
40 Block
::enumBlocks( 'wfBlockCacheInsert', '' );
44 # Load the cache from memcached or, if that's not possible, from the DB
46 global $wgUseMemCached, $wgMemc;
48 if ( $this->mData
=== false) {
50 if ( $wgUseMemCached ) {
51 $this->mData
= $wgMemc->get( $this->mMemcKey
);
54 if ( !is_array( $this->mData
) ) {
60 # Add a block to the cache
61 function insert( &$block ) {
62 if ( $block->mUser
== 0 ) {
63 $nb = $block->getNetworkBits();
64 $ipint = $block->getIntegerAddr();
65 $index = $ipint >> ( 32 - $nb );
67 if ( !array_key_exists( $nb, $this->mData
) ) {
68 $this->mData
[$nb] = array();
71 $this->mData
[$nb][$index] = 1;
75 # Find out if a given IP address is blocked
78 $ipint = ip2long( $ip );
81 foreach ( $this->mData
as $networkBits => $blockInts ) {
82 if ( array_key_exists( $ipint >> ( 32 - $networkBits ), $blockInts ) ) {
88 # Clear low order bits
89 if ( $networkBits != 32 ) {
90 $ip .= '/'.$networkBits;
91 $ip = Block
::normaliseRange( $ip );
102 # Clear the local cache
103 # There was once a clear() to clear memcached too, but I deleted it
104 function clearLocal() {
105 $this->mData
= false;
109 function wfBlockCacheInsert( $block, $tag ) {
110 global $wgBlockCache;
111 $wgBlockCache->insert( $block );