diff options
Diffstat (limited to 'includes/objectcache/DBABagOStuff.php')
-rw-r--r-- | includes/objectcache/DBABagOStuff.php | 127 |
1 files changed, 107 insertions, 20 deletions
diff --git a/includes/objectcache/DBABagOStuff.php b/includes/objectcache/DBABagOStuff.php index ade8c0a9..36ced496 100644 --- a/includes/objectcache/DBABagOStuff.php +++ b/includes/objectcache/DBABagOStuff.php @@ -1,4 +1,25 @@ <?php +/** + * Object caching using DBA backend. + * + * 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 + */ /** * Cache that uses DBA as a backend. @@ -7,23 +28,24 @@ * for systems that don't have it. * * On construction you can pass array( 'dir' => '/some/path' ); as a parameter - * to override the default DBA files directory (wgTmpDirectory). + * to override the default DBA files directory (wfTempDir()). * * @ingroup Cache */ class DBABagOStuff extends BagOStuff { var $mHandler, $mFile, $mReader, $mWriter, $mDisabled; + /** + * @param $params array + */ public function __construct( $params ) { global $wgDBAhandler; if ( !isset( $params['dir'] ) ) { - global $wgTmpDirectory; - $params['dir'] = $wgTmpDirectory; + $params['dir'] = wfTempDir(); } - $this->mFile = $params['dir']."/mw-cache-" . wfWikiID(); - $this->mFile .= '.db'; + $this->mFile = $params['dir'] . '/mw-cache-' . wfWikiID() . '.db'; wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" ); $this->mHandler = $wgDBAhandler; } @@ -35,7 +57,7 @@ class DBABagOStuff extends BagOStuff { * * @return string */ - function encode( $value, $expiry ) { + protected function encode( $value, $expiry ) { # Convert to absolute time $expiry = $this->convertExpiry( $expiry ); @@ -43,11 +65,12 @@ class DBABagOStuff extends BagOStuff { } /** + * @param $blob string * @return array list containing value first and expiry second */ - function decode( $blob ) { + protected function decode( $blob ) { if ( !is_string( $blob ) ) { - return array( null, 0 ); + return array( false, 0 ); } else { return array( unserialize( substr( $blob, 11 ) ), @@ -56,7 +79,10 @@ class DBABagOStuff extends BagOStuff { } } - function getReader() { + /** + * @return resource + */ + protected function getReader() { if ( file_exists( $this->mFile ) ) { $handle = dba_open( $this->mFile, 'rl', $this->mHandler ); } else { @@ -70,7 +96,10 @@ class DBABagOStuff extends BagOStuff { return $handle; } - function getWriter() { + /** + * @return resource + */ + protected function getWriter() { $handle = dba_open( $this->mFile, 'cl', $this->mHandler ); if ( !$handle ) { @@ -80,14 +109,18 @@ class DBABagOStuff extends BagOStuff { return $handle; } - function get( $key ) { + /** + * @param $key string + * @return mixed + */ + public function get( $key ) { wfProfileIn( __METHOD__ ); wfDebug( __METHOD__ . "($key)\n" ); $handle = $this->getReader(); if ( !$handle ) { wfProfileOut( __METHOD__ ); - return null; + return false; } $val = dba_fetch( $key, $handle ); @@ -96,20 +129,26 @@ class DBABagOStuff extends BagOStuff { # Must close ASAP because locks are held dba_close( $handle ); - if ( !is_null( $val ) && $expiry && $expiry < time() ) { + if ( $val !== false && $expiry && $expiry < time() ) { # Key is expired, delete it $handle = $this->getWriter(); dba_delete( $key, $handle ); dba_close( $handle ); wfDebug( __METHOD__ . ": $key expired\n" ); - $val = null; + $val = false; } wfProfileOut( __METHOD__ ); return $val; } - function set( $key, $value, $exptime = 0 ) { + /** + * @param $key string + * @param $value mixed + * @param $exptime int + * @return bool + */ + public function set( $key, $value, $exptime = 0 ) { wfProfileIn( __METHOD__ ); wfDebug( __METHOD__ . "($key)\n" ); @@ -128,7 +167,12 @@ class DBABagOStuff extends BagOStuff { return $ret; } - function delete( $key, $time = 0 ) { + /** + * @param $key string + * @param $time int + * @return bool + */ + public function delete( $key, $time = 0 ) { wfProfileIn( __METHOD__ ); wfDebug( __METHOD__ . "($key)\n" ); @@ -138,14 +182,20 @@ class DBABagOStuff extends BagOStuff { return false; } - $ret = dba_delete( $key, $handle ); + $ret = !dba_exists( $key, $handle ) || dba_delete( $key, $handle ); dba_close( $handle ); wfProfileOut( __METHOD__ ); return $ret; } - function add( $key, $value, $exptime = 0 ) { + /** + * @param $key string + * @param $value mixed + * @param $exptime int + * @return bool + */ + public function add( $key, $value, $exptime = 0 ) { wfProfileIn( __METHOD__ ); $blob = $this->encode( $value, $exptime ); @@ -163,7 +213,7 @@ class DBABagOStuff extends BagOStuff { if ( !$ret ) { list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) ); - if ( $expiry < time() ) { + if ( $expiry && $expiry < time() ) { # Yes expired, delete and try again dba_delete( $key, $handle ); $ret = dba_insert( $key, $blob, $handle ); @@ -177,6 +227,44 @@ class DBABagOStuff extends BagOStuff { return $ret; } + /** + * @param $key string + * @param $step integer + * @return integer|bool + */ + public function incr( $key, $step = 1 ) { + wfProfileIn( __METHOD__ ); + + $handle = $this->getWriter(); + + if ( !$handle ) { + wfProfileOut( __METHOD__ ); + return false; + } + + list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) ); + if ( $value !== false ) { + if ( $expiry && $expiry < time() ) { + # Key is expired, delete it + dba_delete( $key, $handle ); + wfDebug( __METHOD__ . ": $key expired\n" ); + $value = false; + } else { + $value += $step; + $blob = $this->encode( $value, $expiry ); + + $ret = dba_replace( $key, $blob, $handle ); + $value = $ret ? $value : false; + } + } + + dba_close( $handle ); + + wfProfileOut( __METHOD__ ); + + return ( $value === false ) ? false : (int)$value; + } + function keys() { $reader = $this->getReader(); $k1 = dba_firstkey( $reader ); @@ -196,4 +284,3 @@ class DBABagOStuff extends BagOStuff { return $result; } } - |