4 * Class to store objects in the database
8 class SqlBagOStuff
extends BagOStuff
{
9 var $lb, $db, $serverInfo;
10 var $lastExpireAll = 0;
13 * Constructor. Parameters are:
14 * - server: A server info structure in the format required by each
15 * element in $wgDBServers.
17 public function __construct( $params ) {
18 if ( isset( $params['server'] ) ) {
19 $this->serverInfo
= $params['server'];
20 $this->serverInfo
['load'] = 1;
24 protected function getDB() {
25 if ( !isset( $this->db
) ) {
26 # If server connection info was given, use that
27 if ( $this->serverInfo
) {
28 $this->lb
= new LoadBalancer( array(
29 'servers' => array( $this->serverInfo
) ) );
30 $this->db
= $this->lb
->getConnection( DB_MASTER
);
31 $this->db
->clearFlag( DBO_TRX
);
33 # We must keep a separate connection to MySQL in order to avoid deadlocks
34 # However, SQLite has an opposite behaviour.
35 # @todo Investigate behaviour for other databases
36 if ( wfGetDB( DB_MASTER
)->getType() == 'sqlite' ) {
37 $this->db
= wfGetDB( DB_MASTER
);
39 $this->lb
= wfGetLBFactory()->newMainLB();
40 $this->db
= $this->lb
->getConnection( DB_MASTER
);
41 $this->db
->clearFlag( DBO_TRX
);
49 public function get( $key ) {
50 # expire old entries if any
51 $this->garbageCollect();
53 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
54 array( 'keyname' => $key ), __METHOD__
);
57 $this->debug( 'get: no matching rows' );
61 $this->debug( "get: retrieved data; expiry time is " . $row->exptime
);
63 if ( $this->isExpired( $row->exptime
) ) {
64 $this->debug( "get: key has expired, deleting" );
67 # Put the expiry time in the WHERE condition to avoid deleting a
68 # newly-inserted value
69 $db->delete( 'objectcache',
72 'exptime' => $row->exptime
75 } catch ( DBQueryError
$e ) {
76 $this->handleWriteError( $e );
82 return $this->unserialize( $db->decodeBlob( $row->value
) );
85 public function set( $key, $value, $exptime = 0 ) {
87 $exptime = intval( $exptime );
93 if ( $exptime == 0 ) {
94 $encExpiry = $this->getMaxDateTime();
96 if ( $exptime < 3.16e8
) { # ~10 years
100 $encExpiry = $db->timestamp( $exptime );
104 // (bug 24425) use a replace if the db supports it instead of
105 // delete/insert to avoid clashes with conflicting keynames
106 $db->replace( 'objectcache', array( 'keyname' ),
109 'value' => $db->encodeBlob( $this->serialize( $value ) ),
110 'exptime' => $encExpiry
113 } catch ( DBQueryError
$e ) {
114 $this->handleWriteError( $e );
122 public function delete( $key, $time = 0 ) {
123 $db = $this->getDB();
127 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__
);
129 } catch ( DBQueryError
$e ) {
130 $this->handleWriteError( $e );
138 public function incr( $key, $step = 1 ) {
139 $db = $this->getDB();
140 $step = intval( $step );
144 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
145 array( 'keyname' => $key ), __METHOD__
, array( 'FOR UPDATE' ) );
146 if ( $row === false ) {
152 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__
);
153 if ( $this->isExpired( $row->exptime
) ) {
154 // Expired, do not reinsert
160 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value
) ) );
161 $newValue = $oldValue +
$step;
162 $db->insert( 'objectcache',
165 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
166 'exptime' => $row->exptime
169 } catch ( DBQueryError
$e ) {
170 $this->handleWriteError( $e );
178 public function keys() {
179 $db = $this->getDB();
180 $res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__
);
183 foreach ( $res as $row ) {
184 $result[] = $row->keyname
;
190 protected function isExpired( $exptime ) {
191 return $exptime != $this->getMaxDateTime() && wfTimestamp( TS_UNIX
, $exptime ) < time();
194 protected function getMaxDateTime() {
195 if ( time() > 0x7fffffff ) {
196 return $this->getDB()->timestamp( 1 << 62 );
198 return $this->getDB()->timestamp( 0x7fffffff );
202 protected function garbageCollect() {
203 /* Ignore 99% of requests */
204 if ( !mt_rand( 0, 100 ) ) {
206 /* Avoid repeating the delete within a few seconds */
207 if ( $now > ( $this->lastExpireAll +
1 ) ) {
208 $this->lastExpireAll
= $now;
214 public function expireAll() {
215 $db = $this->getDB();
216 $now = $db->timestamp();
220 $db->delete( 'objectcache', array( 'exptime < ' . $db->addQuotes( $now ) ), __METHOD__
);
222 } catch ( DBQueryError
$e ) {
223 $this->handleWriteError( $e );
227 public function deleteAll() {
228 $db = $this->getDB();
232 $db->delete( 'objectcache', '*', __METHOD__
);
234 } catch ( DBQueryError
$e ) {
235 $this->handleWriteError( $e );
240 * Serialize an object and, if possible, compress the representation.
241 * On typical message and page data, this can provide a 3X decrease
242 * in storage requirements.
247 protected function serialize( &$data ) {
248 $serial = serialize( $data );
250 if ( function_exists( 'gzdeflate' ) ) {
251 return gzdeflate( $serial );
258 * Unserialize and, if necessary, decompress an object.
259 * @param $serial string
262 protected function unserialize( $serial ) {
263 if ( function_exists( 'gzinflate' ) ) {
264 $decomp = @gzinflate
( $serial );
266 if ( false !== $decomp ) {
271 $ret = unserialize( $serial );
277 * Handle a DBQueryError which occurred during a write operation.
278 * Ignore errors which are due to a read-only database, rethrow others.
280 protected function handleWriteError( $exception ) {
281 $db = $this->getDB();
283 if ( !$db->wasReadOnlyError() ) {
289 } catch ( DBQueryError
$e ) {
292 wfDebug( __METHOD__
. ": ignoring query error\n" );
293 $db->ignoreErrors( false );
298 * Backwards compatibility alias
300 class MediaWikiBagOStuff
extends SqlBagOStuff
{ }