/**
* Get an item with the given key, regenerating and setting it if not found
*
- * Nothing is stored nor deleted if the callback returns false
+ * The callback can take $ttl as argument by reference and modify it.
+ * Nothing is stored nor deleted if the callback returns false.
*
* @param string $key
* @param int $ttl Time-to-live (seconds)
$value = $this->get( $key, $flags );
if ( $value === false ) {
- if ( !is_callable( $callback ) ) {
- throw new InvalidArgumentException( "Invalid cache miss callback provided." );
- }
- $value = call_user_func( $callback );
- if ( $value !== false ) {
+ $value = $callback( $ttl );
+ if ( $value !== false && $ttl >= 0 ) {
$this->set( $key, $value, $ttl, $flags );
}
}