$this->debugMode = $bool;
}
+ /**
+ * Get an item with the given key, regenerating and setting it if not found
+ *
+ * If the callback returns false, then nothing is stored.
+ *
+ * @param string $key
+ * @param int $ttl Time-to-live (seconds)
+ * @param callable $callback Callback that derives the new value
+ * @return mixed The cached value if found or the result of $callback otherwise
+ * @since 1.27
+ */
+ final public function getWithSetCallback( $key, $ttl, $callback ) {
+ $value = $this->get( $key );
+
+ if ( $value === false ) {
+ if ( !is_callable( $callback ) ) {
+ throw new InvalidArgumentException( "Invalid cache miss callback provided." );
+ }
+ $value = call_user_func( $callback );
+ if ( $value !== false ) {
+ $this->set( $key, $value, $ttl );
+ }
+ }
+
+ return $value;
+ }
+
/**
* Get an item with the given key
*
$this->assertEquals( $this->cache->get( $key ), $value );
}
+ /**
+ * @covers BagOStuff::getWithSetCallback
+ */
+ public function testGetWithSetCallback() {
+ $key = wfMemcKey( 'test' );
+ $value = $this->cache->getWithSetCallback(
+ $key,
+ 30,
+ function () {
+ return 'hello kitty';
+ }
+ );
+
+ $this->assertEquals( 'hello kitty', $value );
+ $this->assertEquals( $value, $this->cache->get( $key ) );
+ }
+
/**
* @covers BagOStuff::incr
*/