-
-/**
- * Simple generic object store
- *
- * interface is intended to be more or less compatible with
- * the PHP memcached client.
- *
- * backends for local hash array and SQL table included:
- * $bag = new HashBagOStuff();
- * $bag = new MysqlBagOStuff($tablename); # connect to db first
- *
- * @abstract
- */
-class BagOStuff {
- var $debugmode;
-
- function BagOStuff() {
- $this->set_debug( false );
- }
-
- function set_debug($bool) {
- $this->debugmode = $bool;
- }
-
- /* *** THE GUTS OF THE OPERATION *** */
- /* Override these with functional things in subclasses */
-
- function get($key) {
- /* stub */
- return false;
- }
-
- function set($key, $value, $exptime=0) {
- /* stub */
- return false;
- }
-
- function delete($key, $time=0) {
- /* stub */
- return false;
- }
-
- function lock($key, $timeout = 0) {
- /* stub */
- return true;
- }
-
- function unlock($key) {
- /* stub */
- return true;
- }
-
- /* *** Emulated functions *** */
- /* Better performance can likely be got with custom written versions */
- function get_multi($keys) {
- $out = array();
- foreach($keys as $key)
- $out[$key] = $this->get($key);
- return $out;
- }
-
- function set_multi($hash, $exptime=0) {
- foreach($hash as $key => $value)
- $this->set($key, $value, $exptime);
- }
-
- function add($key, $value, $exptime=0) {
- if( $this->get($key) == false ) {
- $this->set($key, $value, $exptime);
- return true;
- }
- }
-
- function add_multi($hash, $exptime=0) {
- foreach($hash as $key => $value)
- $this->add($key, $value, $exptime);
- }
-
- function delete_multi($keys, $time=0) {
- foreach($keys as $key)
- $this->delete($key, $time);
- }
-
- function replace($key, $value, $exptime=0) {
- if( $this->get($key) !== false )
- $this->set($key, $value, $exptime);
- }
-
- function incr($key, $value=1) {
- if ( !$this->lock($key) ) {
- return false;
- }
- $value = intval($value);
- if($value < 0) $value = 0;
-
- $n = false;
- if( ($n = $this->get($key)) !== false ) {
- $n += $value;
- $this->set($key, $n); // exptime?
- }
- $this->unlock($key);
- return $n;
- }
-
- function decr($key, $value=1) {
- if ( !$this->lock($key) ) {
- return false;
- }
- $value = intval($value);
- if($value < 0) $value = 0;
-
- $m = false;
- if( ($n = $this->get($key)) !== false ) {
- $m = $n - $value;
- if($m < 0) $m = 0;
- $this->set($key, $m); // exptime?
- }
- $this->unlock($key);
- return $m;
- }
-
- function _debug($text) {
- if($this->debugmode)
- wfDebug("BagOStuff debug: $text\n");
- }
-}
-