Merge "Allow for dynamic TTLs in getWithSetCallback()"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 14 May 2015 03:23:36 +0000 (03:23 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 14 May 2015 03:23:36 +0000 (03:23 +0000)
1  2 
includes/libs/objectcache/WANObjectCache.php

@@@ -74,9 -74,6 +74,9 @@@ class WANObjectCache 
        /** Seconds to keep lock keys around */
        const LOCK_TTL = 5;
  
 +      /** Idiom for set()/getWithSetCallback() TTL */
 +      const TTL_NONE = 0;
 +
        /** Cache format version number */
        const VERSION = 1;
  
        /**
         * Fetch the value of a timestamp "check" key
         *
 +       * Note that "check" keys won't collide with other regular keys
 +       *
         * @param string $key
         * @return float|bool TS_UNIX timestamp of the key; false if not present
         */
         * avoid race conditions where dependent keys get updated with a
         * stale value (e.g. from a DB slave).
         *
 +       * Note that "check" keys won't collide with other regular keys
 +       *
         * @see WANObjectCache::get()
         *
         * @param string $key Cache key
        /**
         * Method to fetch/regenerate cache keys
         *
-        * On cache miss, the key will be set to the callback result.
+        * On cache miss, the key will be set to the callback result,
+        * unless the callback returns false. The arguments supplied are:
+        *     (current value or false, &$ttl)
         * The callback function returns the new value given the current
-        * value (false if not present). If false is returned, then nothing
-        * will be saved to cache.
+        * value (false if not present). Preemptive re-caching and $checkKeys
+        * can result in a non-false current value. The TTL of the new value
+        * can be set dynamically by altering $ttl in the callback (by reference).
         *
         * Usually, callbacks ignore the current value, but it can be used
         * to maintain "most recent X" values that come from time or sequence
         * @code
         *     $key = wfMemcKey( 'cat-recent-actions', $catId );
         *     // Function that derives the new key value given the old value
-        *     $callback = function( $cValue ) { ... };
+        *     $callback = function( $cValue, &$ttl ) { ... };
         *     // Get the key value from cache or from source on cache miss;
         *     // try to only let one cluster thread manage doing cache updates
         *     $opts = array( 'lockTSE' => 5, 'lowTTL' => 10 );
                }
  
                // Generate the new value from the callback...
-               $value = call_user_func( $callback, $cValue );
+               $value = call_user_func_array( $callback, array( $cValue, &$ttl ) );
                // When delete() is called, writes are write-holed by the tombstone,
                // so use a special stash key to pass the new value around threads.
                if ( $value !== false && ( $isHot || $isTombstone ) ) {