diff options
Diffstat (limited to 'includes/objectcache/BagOStuff.php')
-rw-r--r-- | includes/objectcache/BagOStuff.php | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/includes/objectcache/BagOStuff.php b/includes/objectcache/BagOStuff.php new file mode 100644 index 00000000..97b6cb2c --- /dev/null +++ b/includes/objectcache/BagOStuff.php @@ -0,0 +1,164 @@ +<?php +/** + * 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. + * + * backends for local hash array and SQL table included: + * <code> + * $bag = new HashBagOStuff(); + * $bag = new SqlBagOStuff(); # connect to db first + * </code> + * + * @ingroup Cache + */ +abstract class BagOStuff { + private $debugMode = false; + + /** + * @param $bool bool + */ + public function setDebug( $bool ) { + $this->debugMode = $bool; + } + + /* *** THE GUTS OF THE OPERATION *** */ + /* Override these with functional things in subclasses */ + + /** + * Get an item with the given key. Returns false if it does not exist. + * @param $key string + * + * @return bool|Object + */ + abstract public function get( $key ); + + /** + * Set an item. + * @param $key string + * @param $value mixed + * @param $exptime int Either an interval in seconds or a unix timestamp for expiry + */ + abstract public function set( $key, $value, $exptime = 0 ); + + /** + * Delete an item. + * @param $key string + * @param $time int Amount of time to delay the operation (mostly memcached-specific) + */ + abstract public function delete( $key, $time = 0 ); + + public function lock( $key, $timeout = 0 ) { + /* stub */ + return true; + } + + public function unlock( $key ) { + /* stub */ + return true; + } + + public function keys() { + /* stub */ + return array(); + } + + /** + * Delete all objects expiring before a certain date. + * + * @return true on success, false if unimplemented + */ + public function deleteObjectsExpiringBefore( $date ) { + // stub + return false; + } + + /* *** Emulated functions *** */ + + public function add( $key, $value, $exptime = 0 ) { + if ( !$this->get( $key ) ) { + $this->set( $key, $value, $exptime ); + + return true; + } + } + + public function replace( $key, $value, $exptime = 0 ) { + if ( $this->get( $key ) !== false ) { + $this->set( $key, $value, $exptime ); + } + } + + /** + * @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 null; + } + + $value = intval( $value ); + + if ( ( $n = $this->get( $key ) ) !== false ) { + $n += $value; + $this->set( $key, $n ); // exptime? + } + $this->unlock( $key ); + + return $n; + } + + public function decr( $key, $value = 1 ) { + return $this->incr( $key, - $value ); + } + + public function debug( $text ) { + if ( $this->debugMode ) { + $class = get_class( $this ); + wfDebug( "$class debug: $text\n" ); + } + } + + /** + * Convert an optionally relative time to an absolute time + */ + protected function convertExpiry( $exptime ) { + if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) { + return time() + $exptime; + } else { + return $exptime; + } + } +} + + |