* @ingroup Cache
*/
class APCBagOStuff extends BagOStuff {
-
/**
* @param $key string
* @return mixed
$val = apc_fetch( $key );
if ( is_string( $val ) ) {
- $val = unserialize( $val );
+ if ( $this->isInteger( $val ) ) {
+ $val = intval( $val );
+ } else {
+ $val = unserialize( $val );
+ }
}
return $val;
* @return bool
*/
public function set( $key, $value, $exptime = 0 ) {
- apc_store( $key, serialize( $value ), $exptime );
+ if ( !$this->isInteger( $value ) ) {
+ $value = serialize( $value );
+ }
+
+ apc_store( $key, $value, $exptime );
return true;
}
return true;
}
+ public function incr( $key, $value = 1 ) {
+ return apc_inc( $key, $value );
+ }
+
+ public function decr( $key, $value = 1 ) {
+ return apc_dec( $key, $value );
+ }
+
/**
* @return Array
*/
return $keys;
}
}
-
}
/**
+ * Increase stored value of $key by $value while preserving its TTL
* @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
- * @return bool success
+ * @return integer
*/
public function incr( $key, $value = 1 ) {
if ( !$this->lock( $key ) ) {
}
/**
+ * Decrease stored value of $key by $value while preserving its TTL
* @param $key String
* @param $value Integer
- * @return bool success
+ * @return integer
*/
public function decr( $key, $value = 1 ) {
return $this->incr( $key, - $value );
return $exptime;
}
}
+
+ /**
+ * Check if a value is an integer
+ *
+ * @param $value mixed
+ * @return bool
+ */
+ protected function isInteger( $value ) {
+ return ( is_int( $value ) || ctype_digit( $value ) );
+ }
}
$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;
}
*
* @return string
*/
- function encode( $value, $expiry ) {
+ protected function encode( $value, $expiry ) {
# Convert to absolute time
$expiry = $this->convertExpiry( $expiry );
* @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 );
} else {
/**
* @return resource
*/
- function getReader() {
+ protected function getReader() {
if ( file_exists( $this->mFile ) ) {
$handle = dba_open( $this->mFile, 'rl', $this->mHandler );
} else {
/**
* @return resource
*/
- function getWriter() {
+ protected function getWriter() {
$handle = dba_open( $this->mFile, 'cl', $this->mHandler );
if ( !$handle ) {
* @param $key string
* @return mixed|null|string
*/
- function get( $key ) {
+ public function get( $key ) {
wfProfileIn( __METHOD__ );
wfDebug( __METHOD__ . "($key)\n" );
* @param $exptime int
* @return bool
*/
- function set( $key, $value, $exptime = 0 ) {
+ public function set( $key, $value, $exptime = 0 ) {
wfProfileIn( __METHOD__ );
wfDebug( __METHOD__ . "($key)\n" );
* @param $time int
* @return bool
*/
- function delete( $key, $time = 0 ) {
+ public function delete( $key, $time = 0 ) {
wfProfileIn( __METHOD__ );
wfDebug( __METHOD__ . "($key)\n" );
* @param $exptime int
* @return bool
*/
- function add( $key, $value, $exptime = 0 ) {
+ public function add( $key, $value, $exptime = 0 ) {
wfProfileIn( __METHOD__ );
$blob = $this->encode( $value, $exptime );
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 );
}
/**
- * @return Array
+ * @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 ( !is_null( $value ) ) {
+ if ( $expiry && $expiry < time() ) {
+ # Key is expired, delete it
+ dba_delete( $key, $handle );
+ wfDebug( __METHOD__ . ": $key expired\n" );
+ $value = null;
+ } else {
+ $value += $step;
+ $blob = $this->encode( $value, $expiry );
+
+ $ret = dba_replace( $key, $blob, $handle );
+ $value = $ret ? $value : null;
+ }
+ }
+
+ dba_close( $handle );
+
+ wfProfileOut( __METHOD__ );
+
+ return is_null( $value ) ? false : (int)$value;
+ }
+
function keys() {
$reader = $this->getReader();
$k1 = dba_firstkey( $reader );
return $result;
}
}
-
$val = xcache_get( $key );
if ( is_string( $val ) ) {
- $val = unserialize( $val );
+ if ( $this->isInteger( $val ) ) {
+ $val = intval( $val );
+ } else {
+ $val = unserialize( $val );
+ }
}
return $val;
* @return bool
*/
public function set( $key, $value, $expire = 0 ) {
- xcache_set( $key, serialize( $value ), $expire );
+ if ( !$this->isInteger( $value ) ) {
+ $value = serialize( $value );
+ }
+
+ xcache_set( $key, $value, $expire );
return true;
}
xcache_unset( $key );
return true;
}
-}
+ public function incr( $key, $value = 1 ) {
+ return xcache_inc( $key, $value );
+ }
+
+ public function decr( $key, $value = 1 ) {
+ return xcache_dec( $key, $value );
+ }
+}