Merge "Handle edge case in WikiPage::lock()"
[lhc/web/wiklou.git] / includes / libs / objectcache / WANObjectCache.php
index b1d3ec2..fefecdf 100644 (file)
@@ -82,7 +82,7 @@ class WANObjectCache {
        /** Seconds to keep lock keys around */
        const LOCK_TTL = 5;
        /** Default remaining TTL at which to consider pre-emptive regeneration */
-       const LOW_TTL = 10;
+       const LOW_TTL = 30;
        /** Default time-since-expiry on a miss that makes a key "hot" */
        const LOCK_TSE = 1;
 
@@ -269,10 +269,12 @@ class WANObjectCache {
         *   - d) T1 reads the row and calls set() due to a cache miss
         *   - e) Stale value is stuck in cache
         *
+        * Setting 'lag' helps avoids keys getting stuck in long-term stale states.
+        *
         * Example usage:
         * @code
         *     $dbr = wfGetDB( DB_SLAVE );
-        *     $setOpts = DatabaseBase::getCacheSetOptions( $dbr );
+        *     $setOpts = Database::getCacheSetOptions( $dbr );
         *     // Fetch the row from the DB
         *     $row = $dbr->selectRow( ... );
         *     $key = wfMemcKey( 'building', $buildingId );
@@ -505,6 +507,7 @@ class WANObjectCache {
         * can be set dynamically by altering $ttl in the callback (by reference).
         * The $setOpts array can be altered and is given to set() when called;
         * it is recommended to set the 'since' field to avoid race conditions.
+        * Setting 'lag' helps avoids keys getting stuck in long-term stale states.
         *
         * Usually, callbacks ignore the current value, but it can be used
         * to maintain "most recent X" values that come from time or sequence
@@ -529,7 +532,7 @@ class WANObjectCache {
         *         function ( $oldValue, &$ttl, array &$setOpts ) {
         *             $dbr = wfGetDB( DB_SLAVE );
         *             // Account for any snapshot/slave lag
-        *             $setOpts += DatabaseBase::getCacheSetOptions( $dbr );
+        *             $setOpts += Database::getCacheSetOptions( $dbr );
         *
         *             return $dbr->selectRow( ... );
         *        },
@@ -547,7 +550,7 @@ class WANObjectCache {
         *         function ( $oldValue, &$ttl, array &$setOpts ) {
         *             $dbr = wfGetDB( DB_SLAVE );
         *             // Account for any snapshot/slave lag
-        *             $setOpts += DatabaseBase::getCacheSetOptions( $dbr );
+        *             $setOpts += Database::getCacheSetOptions( $dbr );
         *
         *             return CatConfig::newFromRow( $dbr->selectRow( ... ) );
         *        },
@@ -570,7 +573,7 @@ class WANObjectCache {
         *             // Determine new value from the DB
         *             $dbr = wfGetDB( DB_SLAVE );
         *             // Account for any snapshot/slave lag
-        *             $setOpts += DatabaseBase::getCacheSetOptions( $dbr );
+        *             $setOpts += Database::getCacheSetOptions( $dbr );
         *
         *             return CatState::newFromResults( $dbr->select( ... ) );
         *        },
@@ -595,7 +598,7 @@ class WANObjectCache {
         *         function ( $oldValue, &$ttl, array &$setOpts ) {
         *             $dbr = wfGetDB( DB_SLAVE );
         *             // Account for any snapshot/slave lag
-        *             $setOpts += DatabaseBase::getCacheSetOptions( $dbr );
+        *             $setOpts += Database::getCacheSetOptions( $dbr );
         *
         *             // Start off with the last cached list
         *             $list = $oldValue ?: array();
@@ -618,31 +621,41 @@ class WANObjectCache {
         * @see WANObjectCache::set()
         *
         * @param string $key Cache key
-        * @param callable $callback Value generation function
         * @param integer $ttl Seconds to live for key updates. Special values are:
-        *   - WANObjectCache::TTL_NONE        : cache forever
-        *   - WANObjectCache::TTL_UNCACHEABLE : do not cache at all
-        * @param array $checkKeys List of "check" keys
+        *   - WANObjectCache::TTL_NONE : Cache forever
+        *   - WANObjectCache::TTL_UNCACHEABLE: Do not cache at all
+        * @param callable $callback Value generation function
         * @param array $opts Options map:
-        *   - lowTTL  : consider pre-emptive updates when the current TTL (sec)
-        *               of the key is less than this. It becomes more likely
-        *               over time, becoming a certainty once the key is expired.
-        *               [Default: WANObjectCache::LOW_TTL seconds]
-        *   - lockTSE : if the key is tombstoned or expired (by $checkKeys) less
-        *               than this many seconds ago, then try to have a single
-        *               thread handle cache regeneration at any given time.
-        *               Other threads will try to use stale values if possible.
-        *               If, on miss, the time since expiration is low, the assumption
-        *               is that the key is hot and that a stampede is worth avoiding.
-        *               Setting this above WANObjectCache::HOLDOFF_TTL makes no difference.
-        *               The higher this is set, the higher the worst-case staleness can be.
-        *               Use WANObjectCache::TSE_NONE to disable this logic.
-        *               [Default: WANObjectCache::TSE_NONE]
+        *   - checkKeys: List of "check" keys.
+        *   - lowTTL: Consider pre-emptive updates when the current TTL (sec) of the key is less than
+        *      this. It becomes more likely over time, becoming a certainty once the key is expired.
+        *      Default: WANObjectCache::LOW_TTL seconds.
+        *   - lockTSE: If the key is tombstoned or expired (by checkKeys) less than this many seconds
+        *      ago, then try to have a single thread handle cache regeneration at any given time.
+        *      Other threads will try to use stale values if possible. If, on miss, the time since
+        *      expiration is low, the assumption is that the key is hot and that a stampede is worth
+        *      avoiding. Setting this above WANObjectCache::HOLDOFF_TTL makes no difference. The
+        *      higher this is set, the higher the worst-case staleness can be.
+        *      Use WANObjectCache::TSE_NONE to disable this logic. Default: WANObjectCache::TSE_NONE.
         * @return mixed Value to use for the key
         */
        final public function getWithSetCallback(
-               $key, $callback, $ttl, array $checkKeys = array(), array $opts = array()
+               $key, $ttl, $callback, array $opts = array(), $oldOpts = array()
        ) {
+               // Back-compat with 1.26: Swap $ttl and $callback
+               if ( is_int( $callback ) ) {
+                       $temp = $ttl;
+                       $ttl = $callback;
+                       $callback = $temp;
+               }
+               // Back-compat with 1.26: $checkKeys as separate parameter
+               if ( $oldOpts || ( is_array( $opts ) && isset( $opts[0] ) ) ) {
+                       $checkKeys = $opts;
+                       $opts = $oldOpts;
+               } else {
+                       $checkKeys = isset( $opts['checkKeys'] ) ? $opts['checkKeys'] : array();
+               }
+
                $lowTTL = isset( $opts['lowTTL'] ) ? $opts['lowTTL'] : min( self::LOW_TTL, $ttl );
                $lockTSE = isset( $opts['lockTSE'] ) ? $opts['lockTSE'] : self::TSE_NONE;