<?php
/**
- * Classes to cache objects in PHP accelerators, SQL database or DBA files
- *
* Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
* https://www.mediawiki.org/
*
const ERR_UNREACHABLE = 2; // can't connect
const ERR_UNEXPECTED = 3; // response gave some error
+ /** Bitfield constants for get()/getMulti() */
+ const READ_LATEST = 1; // use latest data for replicated stores
+
public function __construct( array $params = array() ) {
if ( isset( $params['logger'] ) ) {
$this->setLogger( $params['logger'] );
* Get an item with the given key. Returns false if it does not exist.
* @param string $key
* @param mixed $casToken [optional]
+ * @param integer $flags Bitfield; supports READ_LATEST [optional]
* @return mixed Returns false on failure
*/
- abstract public function get( $key, &$casToken = null );
+ abstract public function get( $key, &$casToken = null, $flags = 0 );
/**
* Set an item.
*/
protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) {
do {
+ $this->clearLastError();
$casToken = null; // passed by reference
$currentValue = $this->get( $key, $casToken );
+ if ( $this->getLastError() ) {
+ return false; // don't spam retries (retry only on races)
+ }
+
// Derive the new value from the old value
$value = call_user_func( $callback, $this, $key, $currentValue );
+ $this->clearLastError();
if ( $value === false ) {
$success = true; // do nothing
} elseif ( $currentValue === false ) {
// Try to update the key, failing if it gets changed in the meantime
$success = $this->cas( $casToken, $key, $value, $exptime );
}
+ if ( $this->getLastError() ) {
+ return false; // IO error; don't spam retries
+ }
} while ( !$success && --$attempts );
return $success;
return false;
}
+ $this->clearLastError();
$currentValue = $this->get( $key );
- // Derive the new value from the old value
- $value = call_user_func( $callback, $this, $key, $currentValue );
+ if ( !$this->getLastError() ) {
+ // Derive the new value from the old value
+ $value = call_user_func( $callback, $this, $key, $currentValue );
- if ( $value === false ) {
- $success = true; // do nothing
- } else {
- $success = $this->set( $key, $value, $exptime ); // set the new value
+ if ( $value === false ) {
+ $success = true; // do nothing
+ } else {
+ $success = $this->set( $key, $value, $exptime ); // set the new value
+ }
}
if ( !$this->unlock( $key ) ) {
return false;
}
- /* *** Emulated functions *** */
-
/**
* Get an associative array containing the item for each of the keys that have items.
* @param array $keys List of strings
+ * @param integer $flags Bitfield; supports READ_LATEST [optional]
* @return array
*/
- public function getMulti( array $keys ) {
+ public function getMulti( array $keys, $flags = 0 ) {
$res = array();
foreach ( $keys as $key ) {
$val = $this->get( $key );