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;
18 * Create a new BlockCache object
20 * @param Boolean $deferLoad specifies whether to immediately load the data from memcached.
21 * @param String $dbName specifies the memcached dbName prefix to be used. Defaults to $wgDBname.
23 function BlockCache( $deferLoad = false, $dbName = '' ) {
26 if ( $dbName == '' ) {
30 $this->mMemcKey
= $dbName.':ipblocks';
38 * Load the blocks from the database and save them to memcached
40 function loadFromDB() {
41 global $wgUseMemCached, $wgMemc;
42 $this->mData
= array();
43 # Selecting FOR UPDATE is a convenient way to serialise the memcached and DB operations,
44 # which is necessary even though we don't update the DB
45 if ( $wgUseMemCached ) {
46 Block
::enumBlocks( 'wfBlockCacheInsert', '', EB_FOR_UPDATE
);
47 $wgMemc->set( $this->mMemcKey
, $this->mData
, 0 );
49 Block
::enumBlocks( 'wfBlockCacheInsert', '' );
54 * Load the cache from memcached or, if that's not possible, from the DB
57 global $wgUseMemCached, $wgMemc;
59 if ( $this->mData
=== false) {
61 if ( $wgUseMemCached ) {
62 $this->mData
= $wgMemc->get( $this->mMemcKey
);
65 if ( !is_array( $this->mData
) ) {
72 * Add a block to the cache
74 * @param Object &$block Reference to a "Block" object.
76 function insert( &$block ) {
77 if ( $block->mUser
== 0 ) {
78 $nb = $block->getNetworkBits();
79 $ipint = $block->getIntegerAddr();
80 $index = $ipint >> ( 32 - $nb );
82 if ( !array_key_exists( $nb, $this->mData
) ) {
83 $this->mData
[$nb] = array();
86 $this->mData
[$nb][$index] = 1;
91 * Find out if a given IP address is blocked
93 * @param String $ip IP address
97 $ipint = ip2long( $ip );
100 foreach ( $this->mData
as $networkBits => $blockInts ) {
101 if ( array_key_exists( $ipint >> ( 32 - $networkBits ), $blockInts ) ) {
107 # Clear low order bits
108 if ( $networkBits != 32 ) {
109 $ip .= '/'.$networkBits;
110 $ip = Block
::normaliseRange( $ip );
112 $block = new Block();
122 * Clear the local cache
123 * There was once a clear() to clear memcached too, but I deleted it
125 function clearLocal() {
126 $this->mData
= false;
131 * Add a block to the global $wgBlockCache
133 * @param Object $block A "Block"-object
134 * @param Any $tag unused
136 function wfBlockCacheInsert( $block, $tag ) {
137 global $wgBlockCache;
138 $wgBlockCache->insert( $block );