Merge "Removed unused and poorly supported time argument to BagOStuff::delete"
[lhc/web/wiklou.git] / includes / objectcache / BagOStuff.php
index 1a4514d..c8c3497 100644 (file)
  * @defgroup Cache Cache
  */
 
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
 /**
  * interface is intended to be more or less compatible with
  * the PHP memcached client.
  *
  * @ingroup Cache
  */
-abstract class BagOStuff {
+abstract class BagOStuff implements LoggerAwareInterface {
        private $debugMode = false;
 
        protected $lastError = self::ERR_NONE;
 
+       /**
+        * @var LoggerInterface
+        */
+       protected $logger;
+
        /** Possible values for getLastError() */
        const ERR_NONE = 0; // no error
        const ERR_NO_RESPONSE = 1; // no response
        const ERR_UNREACHABLE = 2; // can't connect
        const ERR_UNEXPECTED = 3; // response gave some error
 
+       public function __construct( array $params = array() ) {
+               if ( isset( $params['logger'] ) ) {
+                       $this->setLogger( $params['logger'] );
+               } else {
+                       $this->setLogger( new NullLogger() );
+               }
+       }
+
+       /**
+        * @param LoggerInterface $logger
+        * @return null
+        */
+       public function setLogger( LoggerInterface $logger ) {
+               $this->logger = $logger;
+       }
+
        /**
         * @param bool $bool
         */
@@ -188,7 +213,7 @@ abstract class BagOStuff {
                $locked = false; // lock acquired
                $attempts = 0; // failed attempts
                do {
-                       if ( ++$attempts >= 3 && $sleep <= 1e6 ) {
+                       if ( ++$attempts >= 3 && $sleep <= 5e5 ) {
                                // Exponentially back off after failed attempts to avoid network spam.
                                // About 2*$uRTT*(2^n-1) us of "sleep" happen for the next n attempts.
                                $sleep *= 2;
@@ -354,8 +379,9 @@ abstract class BagOStuff {
         */
        public function debug( $text ) {
                if ( $this->debugMode ) {
-                       $class = get_class( $this );
-                       wfDebug( "$class debug: $text\n" );
+                       $this->logger->debug( "{class} debug: $text", array(
+                               'class' => get_class( $this ),
+                       ) );
                }
        }