00001 <?php
00002 #
00003 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
00004 # http://www.mediawiki.org/
00005 #
00006 # This program is free software; you can redistribute it and/or modify
00007 # it under the terms of the GNU General Public License as published by
00008 # the Free Software Foundation; either version 2 of the License, or
00009 # (at your option) any later version.
00010 #
00011 # This program is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014 # GNU General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU General Public License along
00017 # with this program; if not, write to the Free Software Foundation, Inc.,
00018 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 # http://www.gnu.org/copyleft/gpl.html
00020
00040 abstract class BagOStuff {
00041 var $debugMode = false;
00042
00043 public function set_debug( $bool ) {
00044 $this->debugMode = $bool;
00045 }
00046
00047
00048
00049
00054 abstract public function get( $key );
00055
00062 abstract public function set( $key, $value, $exptime = 0 );
00063
00064
00065
00066
00067
00068
00069 abstract public function delete( $key, $time = 0 );
00070
00071 public function lock( $key, $timeout = 0 ) {
00072
00073 return true;
00074 }
00075
00076 public function unlock( $key ) {
00077
00078 return true;
00079 }
00080
00081 public function keys() {
00082
00083 return array();
00084 }
00085
00086
00087
00088 public function get_multi( $keys ) {
00089 $out = array();
00090
00091 foreach ( $keys as $key ) {
00092 $out[$key] = $this->get( $key );
00093 }
00094
00095 return $out;
00096 }
00097
00098 public function set_multi( $hash, $exptime = 0 ) {
00099 foreach ( $hash as $key => $value ) {
00100 $this->set( $key, $value, $exptime );
00101 }
00102 }
00103
00104 public function add( $key, $value, $exptime = 0 ) {
00105 if ( $this->get( $key ) == false ) {
00106 $this->set( $key, $value, $exptime );
00107 return true;
00108 }
00109 }
00110
00111 public function add_multi( $hash, $exptime = 0 ) {
00112 foreach ( $hash as $key => $value ) {
00113 $this->add( $key, $value, $exptime );
00114 }
00115 }
00116
00117 public function delete_multi( $keys, $time = 0 ) {
00118 foreach ( $keys as $key ) {
00119 $this->delete( $key, $time );
00120 }
00121 }
00122
00123 public function replace( $key, $value, $exptime = 0 ) {
00124 if ( $this->get( $key ) !== false ) {
00125 $this->set( $key, $value, $exptime );
00126 }
00127 }
00128
00129 public function incr( $key, $value = 1 ) {
00130 if ( !$this->lock( $key ) ) {
00131 return false;
00132 }
00133 $value = intval( $value );
00134
00135 $n = false;
00136 if ( ( $n = $this->get( $key ) ) !== false ) {
00137 $n += $value;
00138 $this->set( $key, $n );
00139 }
00140 $this->unlock( $key );
00141 return $n;
00142 }
00143
00144 public function decr( $key, $value = 1 ) {
00145 return $this->incr( $key, - $value );
00146 }
00147
00148 public function debug( $text ) {
00149 if ( $this->debugMode )
00150 wfDebug( "BagOStuff debug: $text\n" );
00151 }
00152
00156 protected function convertExpiry( $exptime ) {
00157 if ( ( $exptime != 0 ) && ( $exptime < 3600 * 24 * 30 ) ) {
00158 return time() + $exptime;
00159 } else {
00160 return $exptime;
00161 }
00162 }
00163 }
00164
00172 class HashBagOStuff extends BagOStuff {
00173 var $bag;
00174
00175 function __construct() {
00176 $this->bag = array();
00177 }
00178
00179 protected function expire( $key ) {
00180 $et = $this->bag[$key][1];
00181 if ( ( $et == 0 ) || ( $et > time() ) ) {
00182 return false;
00183 }
00184 $this->delete( $key );
00185 return true;
00186 }
00187
00188 function get( $key ) {
00189 if ( !isset( $this->bag[$key] ) ) {
00190 return false;
00191 }
00192 if ( $this->expire( $key ) ) {
00193 return false;
00194 }
00195 return $this->bag[$key][0];
00196 }
00197
00198 function set( $key, $value, $exptime = 0 ) {
00199 $this->bag[$key] = array( $value, $this->convertExpiry( $exptime ) );
00200 }
00201
00202 function delete( $key, $time = 0 ) {
00203 if ( !isset( $this->bag[$key] ) ) {
00204 return false;
00205 }
00206 unset( $this->bag[$key] );
00207 return true;
00208 }
00209
00210 function keys() {
00211 return array_keys( $this->bag );
00212 }
00213 }
00214
00220 class SqlBagOStuff extends BagOStuff {
00221 var $lb, $db;
00222 var $lastExpireAll = 0;
00223
00224 protected function getDB() {
00225 global $wgDBtype;
00226 if ( !isset( $this->db ) ) {
00227
00228
00229
00230
00231 if ( $wgDBtype == 'sqlite' ) {
00232 $this->db = wfGetDB( DB_MASTER );
00233 } else {
00234 $this->lb = wfGetLBFactory()->newMainLB();
00235 $this->db = $this->lb->getConnection( DB_MASTER );
00236 $this->db->clearFlag( DBO_TRX );
00237 }
00238 }
00239 return $this->db;
00240 }
00241
00242 public function get( $key ) {
00243 # expire old entries if any
00244 $this->garbageCollect();
00245 $db = $this->getDB();
00246 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
00247 array( 'keyname' => $key ), __METHOD__ );
00248 if ( !$row ) {
00249 $this->debug( 'get: no matching rows' );
00250 return false;
00251 }
00252
00253 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
00254 if ( $this->isExpired( $row->exptime ) ) {
00255 $this->debug( "get: key has expired, deleting" );
00256 try {
00257 $db->begin();
00258 # Put the expiry time in the WHERE condition to avoid deleting a
00259 # newly-inserted value
00260 $db->delete( 'objectcache',
00261 array(
00262 'keyname' => $key,
00263 'exptime' => $row->exptime
00264 ), __METHOD__ );
00265 $db->commit();
00266 } catch ( DBQueryError $e ) {
00267 $this->handleWriteError( $e );
00268 }
00269 return false;
00270 }
00271 return $this->unserialize( $db->decodeBlob( $row->value ) );
00272 }
00273
00274 public function set( $key, $value, $exptime = 0 ) {
00275 $db = $this->getDB();
00276 $exptime = intval( $exptime );
00277 if ( $exptime < 0 ) $exptime = 0;
00278 if ( $exptime == 0 ) {
00279 $encExpiry = $this->getMaxDateTime();
00280 } else {
00281 if ( $exptime < 3.16e8 ) # ~10 years
00282 $exptime += time();
00283 $encExpiry = $db->timestamp( $exptime );
00284 }
00285 try {
00286 $db->begin();
00287 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
00288 $db->insert( 'objectcache',
00289 array(
00290 'keyname' => $key,
00291 'value' => $db->encodeBlob( $this->serialize( $value ) ),
00292 'exptime' => $encExpiry
00293 ), __METHOD__ );
00294 $db->commit();
00295 } catch ( DBQueryError $e ) {
00296 $this->handleWriteError( $e );
00297 return false;
00298 }
00299 return true;
00300 }
00301
00302 public function delete( $key, $time = 0 ) {
00303 $db = $this->getDB();
00304 try {
00305 $db->begin();
00306 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
00307 $db->commit();
00308 } catch ( DBQueryError $e ) {
00309 $this->handleWriteError( $e );
00310 return false;
00311 }
00312 return true;
00313 }
00314
00315 public function incr( $key, $step = 1 ) {
00316 $db = $this->getDB();
00317 $step = intval( $step );
00318
00319 try {
00320 $db->begin();
00321 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
00322 array( 'keyname' => $key ), __METHOD__, array( 'FOR UPDATE' ) );
00323 if ( $row === false ) {
00324
00325 $db->commit();
00326 return false;
00327 }
00328 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
00329 if ( $this->isExpired( $row->exptime ) ) {
00330
00331 $db->commit();
00332 return false;
00333 }
00334
00335 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
00336 $newValue = $oldValue + $step;
00337 $db->insert( 'objectcache',
00338 array(
00339 'keyname' => $key,
00340 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
00341 'exptime' => $row->exptime
00342 ), __METHOD__ );
00343 $db->commit();
00344 } catch ( DBQueryError $e ) {
00345 $this->handleWriteError( $e );
00346 return false;
00347 }
00348 return $newValue;
00349 }
00350
00351 public function keys() {
00352 $db = $this->getDB();
00353 $res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__ );
00354 $result = array();
00355 foreach ( $res as $row ) {
00356 $result[] = $row->keyname;
00357 }
00358 return $result;
00359 }
00360
00361 protected function isExpired( $exptime ) {
00362 return $exptime != $this->getMaxDateTime() && wfTimestamp( TS_UNIX, $exptime ) < time();
00363 }
00364
00365 protected function getMaxDateTime() {
00366 if ( time() > 0x7fffffff ) {
00367 return $this->getDB()->timestamp( 1 << 62 );
00368 } else {
00369 return $this->getDB()->timestamp( 0x7fffffff );
00370 }
00371 }
00372
00373 protected function garbageCollect() {
00374
00375 if ( !mt_rand( 0, 100 ) ) {
00376 $now = time();
00377
00378 if ( $now > ( $this->lastExpireAll + 1 ) ) {
00379 $this->lastExpireAll = $now;
00380 $this->expireAll();
00381 }
00382 }
00383 }
00384
00385 public function expireAll() {
00386 $db = $this->getDB();
00387 $now = $db->timestamp();
00388 try {
00389 $db->begin();
00390 $db->delete( 'objectcache', array( 'exptime < ' . $db->addQuotes( $now ) ), __METHOD__ );
00391 $db->commit();
00392 } catch ( DBQueryError $e ) {
00393 $this->handleWriteError( $e );
00394 }
00395 }
00396
00397 public function deleteAll() {
00398 $db = $this->getDB();
00399 try {
00400 $db->begin();
00401 $db->delete( 'objectcache', '*', __METHOD__ );
00402 $db->commit();
00403 } catch ( DBQueryError $e ) {
00404 $this->handleWriteError( $e );
00405 }
00406 }
00407
00416 protected function serialize( &$data ) {
00417 $serial = serialize( $data );
00418 if ( function_exists( 'gzdeflate' ) ) {
00419 return gzdeflate( $serial );
00420 } else {
00421 return $serial;
00422 }
00423 }
00424
00430 protected function unserialize( $serial ) {
00431 if ( function_exists( 'gzinflate' ) ) {
00432 $decomp = @gzinflate( $serial );
00433 if ( false !== $decomp ) {
00434 $serial = $decomp;
00435 }
00436 }
00437 $ret = unserialize( $serial );
00438 return $ret;
00439 }
00440
00445 protected function handleWriteError( $exception ) {
00446 $db = $this->getDB();
00447 if ( !$db->wasReadOnlyError() ) {
00448 throw $exception;
00449 }
00450 try {
00451 $db->rollback();
00452 } catch ( DBQueryError $e ) {
00453 }
00454 wfDebug( __METHOD__ . ": ignoring query error\n" );
00455 $db->ignoreErrors( false );
00456 }
00457 }
00458
00462 class MediaWikiBagOStuff extends SqlBagOStuff { }
00463
00469 class APCBagOStuff extends BagOStuff {
00470 public function get( $key ) {
00471 $val = apc_fetch( $key );
00472 if ( is_string( $val ) ) {
00473 $val = unserialize( $val );
00474 }
00475 return $val;
00476 }
00477
00478 public function set( $key, $value, $exptime = 0 ) {
00479 apc_store( $key, serialize( $value ), $exptime );
00480 return true;
00481 }
00482
00483 public function delete( $key, $time = 0 ) {
00484 apc_delete( $key );
00485 return true;
00486 }
00487
00488 public function keys() {
00489 $info = apc_cache_info( 'user' );
00490 $list = $info['cache_list'];
00491 $keys = array();
00492 foreach ( $list as $entry ) {
00493 $keys[] = $entry['info'];
00494 }
00495 return $keys;
00496 }
00497 }
00498
00507 class eAccelBagOStuff extends BagOStuff {
00508 public function get( $key ) {
00509 $val = eaccelerator_get( $key );
00510 if ( is_string( $val ) ) {
00511 $val = unserialize( $val );
00512 }
00513 return $val;
00514 }
00515
00516 public function set( $key, $value, $exptime = 0 ) {
00517 eaccelerator_put( $key, serialize( $value ), $exptime );
00518 return true;
00519 }
00520
00521 public function delete( $key, $time = 0 ) {
00522 eaccelerator_rm( $key );
00523 return true;
00524 }
00525
00526 public function lock( $key, $waitTimeout = 0 ) {
00527 eaccelerator_lock( $key );
00528 return true;
00529 }
00530
00531 public function unlock( $key ) {
00532 eaccelerator_unlock( $key );
00533 return true;
00534 }
00535 }
00536
00543 class XCacheBagOStuff extends BagOStuff {
00544
00551 public function get( $key ) {
00552 $val = xcache_get( $key );
00553 if ( is_string( $val ) )
00554 $val = unserialize( $val );
00555 return $val;
00556 }
00557
00566 public function set( $key, $value, $expire = 0 ) {
00567 xcache_set( $key, serialize( $value ), $expire );
00568 return true;
00569 }
00570
00578 public function delete( $key, $time = 0 ) {
00579 xcache_unset( $key );
00580 return true;
00581 }
00582 }
00583
00592 class DBABagOStuff extends BagOStuff {
00593 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
00594
00595 public function __construct( $dir = false ) {
00596 global $wgDBAhandler;
00597 if ( $dir === false ) {
00598 global $wgTmpDirectory;
00599 $dir = $wgTmpDirectory;
00600 }
00601 $this->mFile = "$dir/mw-cache-" . wfWikiID();
00602 $this->mFile .= '.db';
00603 wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
00604 $this->mHandler = $wgDBAhandler;
00605 }
00606
00610 function encode( $value, $expiry ) {
00611 # Convert to absolute time
00612 $expiry = $this->convertExpiry( $expiry );
00613 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
00614 }
00615
00619 function decode( $blob ) {
00620 if ( !is_string( $blob ) ) {
00621 return array( null, 0 );
00622 } else {
00623 return array(
00624 unserialize( substr( $blob, 11 ) ),
00625 intval( substr( $blob, 0, 10 ) )
00626 );
00627 }
00628 }
00629
00630 function getReader() {
00631 if ( file_exists( $this->mFile ) ) {
00632 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
00633 } else {
00634 $handle = $this->getWriter();
00635 }
00636 if ( !$handle ) {
00637 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
00638 }
00639 return $handle;
00640 }
00641
00642 function getWriter() {
00643 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
00644 if ( !$handle ) {
00645 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
00646 }
00647 return $handle;
00648 }
00649
00650 function get( $key ) {
00651 wfProfileIn( __METHOD__ );
00652 wfDebug( __METHOD__ . "($key)\n" );
00653 $handle = $this->getReader();
00654 if ( !$handle ) {
00655 return null;
00656 }
00657 $val = dba_fetch( $key, $handle );
00658 list( $val, $expiry ) = $this->decode( $val );
00659 # Must close ASAP because locks are held
00660 dba_close( $handle );
00661
00662 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
00663 # Key is expired, delete it
00664 $handle = $this->getWriter();
00665 dba_delete( $key, $handle );
00666 dba_close( $handle );
00667 wfDebug( __METHOD__ . ": $key expired\n" );
00668 $val = null;
00669 }
00670 wfProfileOut( __METHOD__ );
00671 return $val;
00672 }
00673
00674 function set( $key, $value, $exptime = 0 ) {
00675 wfProfileIn( __METHOD__ );
00676 wfDebug( __METHOD__ . "($key)\n" );
00677 $blob = $this->encode( $value, $exptime );
00678 $handle = $this->getWriter();
00679 if ( !$handle ) {
00680 return false;
00681 }
00682 $ret = dba_replace( $key, $blob, $handle );
00683 dba_close( $handle );
00684 wfProfileOut( __METHOD__ );
00685 return $ret;
00686 }
00687
00688 function delete( $key, $time = 0 ) {
00689 wfProfileIn( __METHOD__ );
00690 wfDebug( __METHOD__ . "($key)\n" );
00691 $handle = $this->getWriter();
00692 if ( !$handle ) {
00693 return false;
00694 }
00695 $ret = dba_delete( $key, $handle );
00696 dba_close( $handle );
00697 wfProfileOut( __METHOD__ );
00698 return $ret;
00699 }
00700
00701 function add( $key, $value, $exptime = 0 ) {
00702 wfProfileIn( __METHOD__ );
00703 $blob = $this->encode( $value, $exptime );
00704 $handle = $this->getWriter();
00705 if ( !$handle ) {
00706 return false;
00707 }
00708 $ret = dba_insert( $key, $blob, $handle );
00709 # Insert failed, check to see if it failed due to an expired key
00710 if ( !$ret ) {
00711 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
00712 if ( $expiry < time() ) {
00713 # Yes expired, delete and try again
00714 dba_delete( $key, $handle );
00715 $ret = dba_insert( $key, $blob, $handle );
00716 # This time if it failed then it will be handled by the caller like any other race
00717 }
00718 }
00719
00720 dba_close( $handle );
00721 wfProfileOut( __METHOD__ );
00722 return $ret;
00723 }
00724
00725 function keys() {
00726 $reader = $this->getReader();
00727 $k1 = dba_firstkey( $reader );
00728 if ( !$k1 ) {
00729 return array();
00730 }
00731 $result[] = $k1;
00732 while ( $key = dba_nextkey( $reader ) ) {
00733 $result[] = $key;
00734 }
00735 return $result;
00736 }
00737 }