<?php
-#
-# Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
-# http://www.mediawiki.org/
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# http://www.gnu.org/copyleft/gpl.html
-
/**
- * @defgroup Cache Cache
+ * Classes to cache objects in PHP accelerators, SQL database or DBA files
+ *
+ * Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
+ * http://www.mediawiki.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Cache
*/
+/**
+ * @defgroup Cache Cache
+ */
+
/**
* interface is intended to be more or less compatible with
* the PHP memcached client.
}
public function add( $key, $value, $exptime = 0 ) {
- if ( $this->get( $key ) == false ) {
+ if ( !$this->get( $key ) ) {
$this->set( $key, $value, $exptime );
+
return true;
}
}
}
}
+ /**
+ * @param $key String: Key to increase
+ * @param $value Integer: Value to add to $key (Default 1)
+ * @return null if lock is not possible else $key value increased by $value
+ */
public function incr( $key, $value = 1 ) {
if ( !$this->lock( $key ) ) {
- return false;
+ return null;
}
+
$value = intval( $value );
- $n = false;
if ( ( $n = $this->get( $key ) ) !== false ) {
$n += $value;
$this->set( $key, $n ); // exptime?
}
$this->unlock( $key );
+
return $n;
}
}
public function debug( $text ) {
- if ( $this->debugMode )
+ if ( $this->debugMode ) {
wfDebug( "BagOStuff debug: $text\n" );
+ }
}
/**
* Convert an optionally relative time to an absolute time
*/
protected function convertExpiry( $exptime ) {
- if ( ( $exptime != 0 ) && ( $exptime < 3600 * 24 * 30 ) ) {
+ if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
return time() + $exptime;
} else {
return $exptime;
protected function expire( $key ) {
$et = $this->bag[$key][1];
+
if ( ( $et == 0 ) || ( $et > time() ) ) {
return false;
}
+
$this->delete( $key );
+
return true;
}
if ( !isset( $this->bag[$key] ) ) {
return false;
}
+
if ( $this->expire( $key ) ) {
return false;
}
+
return $this->bag[$key][0];
}
if ( !isset( $this->bag[$key] ) ) {
return false;
}
+
unset( $this->bag[$key] );
+
return true;
}
var $lastExpireAll = 0;
protected function getDB() {
- global $wgDBtype;
if ( !isset( $this->db ) ) {
/* We must keep a separate connection to MySQL in order to avoid deadlocks
* However, SQLite has an opposite behaviour.
* @todo Investigate behaviour for other databases
*/
- if ( $wgDBtype == 'sqlite' ) {
+ if ( wfGetDB( DB_MASTER )->getType() == 'sqlite' ) {
$this->db = wfGetDB( DB_MASTER );
} else {
$this->lb = wfGetLBFactory()->newMainLB();
$this->db->clearFlag( DBO_TRX );
}
}
+
return $this->db;
}
$db = $this->getDB();
$row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
array( 'keyname' => $key ), __METHOD__ );
+
if ( !$row ) {
$this->debug( 'get: no matching rows' );
return false;
}
$this->debug( "get: retrieved data; expiry time is " . $row->exptime );
+
if ( $this->isExpired( $row->exptime ) ) {
$this->debug( "get: key has expired, deleting" );
try {
} catch ( DBQueryError $e ) {
$this->handleWriteError( $e );
}
+
return false;
}
+
return $this->unserialize( $db->decodeBlob( $row->value ) );
}
public function set( $key, $value, $exptime = 0 ) {
$db = $this->getDB();
$exptime = intval( $exptime );
- if ( $exptime < 0 ) $exptime = 0;
+
+ if ( $exptime < 0 ) {
+ $exptime = 0;
+ }
+
if ( $exptime == 0 ) {
$encExpiry = $this->getMaxDateTime();
} else {
- if ( $exptime < 3.16e8 ) # ~10 years
+ if ( $exptime < 3.16e8 ) { # ~10 years
$exptime += time();
+ }
+
$encExpiry = $db->timestamp( $exptime );
}
try {
$db->begin();
- $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
- $db->insert( 'objectcache',
+ // (bug 24425) use a replace if the db supports it instead of
+ // delete/insert to avoid clashes with conflicting keynames
+ $db->replace( 'objectcache', array( 'keyname' ),
array(
'keyname' => $key,
'value' => $db->encodeBlob( $this->serialize( $value ) ),
$db->commit();
} catch ( DBQueryError $e ) {
$this->handleWriteError( $e );
+
return false;
}
+
return true;
}
public function delete( $key, $time = 0 ) {
$db = $this->getDB();
+
try {
$db->begin();
$db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
$db->commit();
} catch ( DBQueryError $e ) {
$this->handleWriteError( $e );
+
return false;
}
+
return true;
}
if ( $row === false ) {
// Missing
$db->commit();
- return false;
+
+ return null;
}
$db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
if ( $this->isExpired( $row->exptime ) ) {
// Expired, do not reinsert
$db->commit();
- return false;
+
+ return null;
}
$oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
$db->commit();
} catch ( DBQueryError $e ) {
$this->handleWriteError( $e );
- return false;
+
+ return null;
}
+
return $newValue;
}
$db = $this->getDB();
$res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__ );
$result = array();
+
foreach ( $res as $row ) {
$result[] = $row->keyname;
}
+
return $result;
}
public function expireAll() {
$db = $this->getDB();
$now = $db->timestamp();
+
try {
$db->begin();
$db->delete( 'objectcache', array( 'exptime < ' . $db->addQuotes( $now ) ), __METHOD__ );
public function deleteAll() {
$db = $this->getDB();
+
try {
$db->begin();
$db->delete( 'objectcache', '*', __METHOD__ );
*/
protected function serialize( &$data ) {
$serial = serialize( $data );
+
if ( function_exists( 'gzdeflate' ) ) {
return gzdeflate( $serial );
} else {
protected function unserialize( $serial ) {
if ( function_exists( 'gzinflate' ) ) {
$decomp = @gzinflate( $serial );
+
if ( false !== $decomp ) {
$serial = $decomp;
}
}
+
$ret = unserialize( $serial );
+
return $ret;
}
*/
protected function handleWriteError( $exception ) {
$db = $this->getDB();
+
if ( !$db->wasReadOnlyError() ) {
throw $exception;
}
+
try {
$db->rollback();
} catch ( DBQueryError $e ) {
}
+
wfDebug( __METHOD__ . ": ignoring query error\n" );
$db->ignoreErrors( false );
}
class APCBagOStuff extends BagOStuff {
public function get( $key ) {
$val = apc_fetch( $key );
+
if ( is_string( $val ) ) {
$val = unserialize( $val );
}
+
return $val;
}
public function set( $key, $value, $exptime = 0 ) {
apc_store( $key, serialize( $value ), $exptime );
+
return true;
}
public function delete( $key, $time = 0 ) {
apc_delete( $key );
+
return true;
}
$info = apc_cache_info( 'user' );
$list = $info['cache_list'];
$keys = array();
+
foreach ( $list as $entry ) {
$keys[] = $entry['info'];
}
+
return $keys;
}
}
class eAccelBagOStuff extends BagOStuff {
public function get( $key ) {
$val = eaccelerator_get( $key );
+
if ( is_string( $val ) ) {
$val = unserialize( $val );
}
+
return $val;
}
public function set( $key, $value, $exptime = 0 ) {
eaccelerator_put( $key, serialize( $value ), $exptime );
+
return true;
}
public function delete( $key, $time = 0 ) {
eaccelerator_rm( $key );
+
return true;
}
public function lock( $key, $waitTimeout = 0 ) {
eaccelerator_lock( $key );
+
return true;
}
public function unlock( $key ) {
eaccelerator_unlock( $key );
+
return true;
}
}
* @ingroup Cache
*/
class XCacheBagOStuff extends BagOStuff {
-
/**
* Get a value from the XCache object cache
*
*/
public function get( $key ) {
$val = xcache_get( $key );
- if ( is_string( $val ) )
+
+ if ( is_string( $val ) ) {
$val = unserialize( $val );
+ }
+
return $val;
}
*/
public function set( $key, $value, $expire = 0 ) {
xcache_set( $key, serialize( $value ), $expire );
+
return true;
}
*/
public function delete( $key, $time = 0 ) {
xcache_unset( $key );
+
return true;
}
}
public function __construct( $dir = false ) {
global $wgDBAhandler;
+
if ( $dir === false ) {
global $wgTmpDirectory;
$dir = $wgTmpDirectory;
}
+
$this->mFile = "$dir/mw-cache-" . wfWikiID();
$this->mFile .= '.db';
wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
function encode( $value, $expiry ) {
# Convert to absolute time
$expiry = $this->convertExpiry( $expiry );
+
return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
}
} else {
$handle = $this->getWriter();
}
+
if ( !$handle ) {
wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
}
+
return $handle;
}
function getWriter() {
$handle = dba_open( $this->mFile, 'cl', $this->mHandler );
+
if ( !$handle ) {
wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
}
+
return $handle;
}
function get( $key ) {
wfProfileIn( __METHOD__ );
wfDebug( __METHOD__ . "($key)\n" );
+
$handle = $this->getReader();
if ( !$handle ) {
return null;
}
+
$val = dba_fetch( $key, $handle );
list( $val, $expiry ) = $this->decode( $val );
+
# Must close ASAP because locks are held
dba_close( $handle );
wfDebug( __METHOD__ . ": $key expired\n" );
$val = null;
}
+
wfProfileOut( __METHOD__ );
return $val;
}
function set( $key, $value, $exptime = 0 ) {
wfProfileIn( __METHOD__ );
wfDebug( __METHOD__ . "($key)\n" );
+
$blob = $this->encode( $value, $exptime );
+
$handle = $this->getWriter();
if ( !$handle ) {
return false;
}
+
$ret = dba_replace( $key, $blob, $handle );
dba_close( $handle );
+
wfProfileOut( __METHOD__ );
return $ret;
}
function delete( $key, $time = 0 ) {
wfProfileIn( __METHOD__ );
wfDebug( __METHOD__ . "($key)\n" );
+
$handle = $this->getWriter();
if ( !$handle ) {
return false;
}
+
$ret = dba_delete( $key, $handle );
dba_close( $handle );
+
wfProfileOut( __METHOD__ );
return $ret;
}
function add( $key, $value, $exptime = 0 ) {
wfProfileIn( __METHOD__ );
+
$blob = $this->encode( $value, $exptime );
+
$handle = $this->getWriter();
+
if ( !$handle ) {
return false;
}
+
$ret = dba_insert( $key, $blob, $handle );
+
# Insert failed, check to see if it failed due to an expired key
if ( !$ret ) {
list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
+
if ( $expiry < time() ) {
# Yes expired, delete and try again
dba_delete( $key, $handle );
}
dba_close( $handle );
+
wfProfileOut( __METHOD__ );
return $ret;
}
function keys() {
$reader = $this->getReader();
$k1 = dba_firstkey( $reader );
+
if ( !$k1 ) {
return array();
}
+
$result[] = $k1;
+
while ( $key = dba_nextkey( $reader ) ) {
$result[] = $key;
}
+
return $result;
}
}
*/
public function get( $key ) {
$val = wincache_ucache_get( $key );
- if ( is_string( $val ) )
+
+ if ( is_string( $val ) ) {
$val = unserialize( $val );
+ }
+
return $val;
}
* @return bool
*/
public function set( $key, $value, $expire = 0 ) {
- wincache_ucache_set( $key, serialize( $value ), $expire );
- return true;
+ $result = wincache_ucache_set( $key, serialize( $value ), $expire );
+
+ /* wincache_ucache_set returns an empty array on success if $value
+ was an array, bool otherwise */
+ return ( is_array( $result ) && $result === array() ) || $result;
}
/**
*/
public function delete( $key, $time = 0 ) {
wincache_ucache_delete( $key );
+
return true;
}
$info = wincache_ucache_info();
$list = $info['ucache_entries'];
$keys = array();
+
+ if ( is_null( $list ) ) {
+ return array();
+ }
+
foreach ( $list as $entry ) {
$keys[] = $entry['key_name'];
}
+
return $keys;
}
}