Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,10 @@ public function rememberForever($key, Closure $callback)
* @param array{ 0: \DateTimeInterface|\DateInterval|int, 1: \DateTimeInterface|\DateInterval|int } $ttl
* @param (callable(): TCacheValue) $callback
* @param array{ seconds?: int, owner?: string }|null $lock
* @param bool $alwaysDefer
* @return TCacheValue
*/
public function flexible($key, $ttl, $callback, $lock = null)
public function flexible($key, $ttl, $callback, $lock = null, $alwaysDefer = false)
{
[
$key => $value,
Expand Down Expand Up @@ -520,7 +521,7 @@ public function flexible($key, $ttl, $callback, $lock = null)
});
};

defer($refresh, "illuminate:cache:flexible:{$key}");
defer($refresh, "illuminate:cache:flexible:{$key}", $alwaysDefer);

return $value;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/Cache/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,31 @@ public function testItImplicitlyClearsTtlKeysFromFileDriver()
$this->assertTrue($cache->missing('illuminate:cache:flexible:created:count'));
}

public function testItCanAlwaysDefer()
{
$this->freezeTime();
$cache = Cache::driver('array');
$count = 0;

// Cache is empty. The value should be populated...
$cache->flexible('foo', [10, 20], function () use (&$count) {
return ++$count;
}, alwaysDefer: true);

// First call to flexible() should not defer
$this->assertCount(0, defer());

Carbon::setTestNow(now()->addSeconds(11));

// Second callback should defer with always now true
$cache->flexible('foo', [10, 20], function () use (&$count) {
return ++$count;
}, alwaysDefer: true);

$this->assertCount(1, defer());
$this->assertTrue(defer()->first()->always);
}

public function testItRoundsDateTimeValuesToAccountForTimePassedDuringScriptExecution()
{
// do not freeze time as this test depends on time progressing duration execution.
Expand Down