Skip to content

Commit d46604a

Browse files
authored
Added option to always defer for flexible cache (#55802)
1 parent 318cad4 commit d46604a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Illuminate/Cache/Repository.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,10 @@ public function rememberForever($key, Closure $callback)
483483
* @param array{ 0: \DateTimeInterface|\DateInterval|int, 1: \DateTimeInterface|\DateInterval|int } $ttl
484484
* @param (callable(): TCacheValue) $callback
485485
* @param array{ seconds?: int, owner?: string }|null $lock
486+
* @param bool $alwaysDefer
486487
* @return TCacheValue
487488
*/
488-
public function flexible($key, $ttl, $callback, $lock = null)
489+
public function flexible($key, $ttl, $callback, $lock = null, $alwaysDefer = false)
489490
{
490491
[
491492
$key => $value,
@@ -520,7 +521,7 @@ public function flexible($key, $ttl, $callback, $lock = null)
520521
});
521522
};
522523

523-
defer($refresh, "illuminate:cache:flexible:{$key}");
524+
defer($refresh, "illuminate:cache:flexible:{$key}", $alwaysDefer);
524525

525526
return $value;
526527
}

tests/Integration/Cache/RepositoryTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,31 @@ public function testItImplicitlyClearsTtlKeysFromFileDriver()
238238
$this->assertTrue($cache->missing('illuminate:cache:flexible:created:count'));
239239
}
240240

241+
public function testItCanAlwaysDefer()
242+
{
243+
$this->freezeTime();
244+
$cache = Cache::driver('array');
245+
$count = 0;
246+
247+
// Cache is empty. The value should be populated...
248+
$cache->flexible('foo', [10, 20], function () use (&$count) {
249+
return ++$count;
250+
}, alwaysDefer: true);
251+
252+
// First call to flexible() should not defer
253+
$this->assertCount(0, defer());
254+
255+
Carbon::setTestNow(now()->addSeconds(11));
256+
257+
// Second callback should defer with always now true
258+
$cache->flexible('foo', [10, 20], function () use (&$count) {
259+
return ++$count;
260+
}, alwaysDefer: true);
261+
262+
$this->assertCount(1, defer());
263+
$this->assertTrue(defer()->first()->always);
264+
}
265+
241266
public function testItRoundsDateTimeValuesToAccountForTimePassedDuringScriptExecution()
242267
{
243268
// do not freeze time as this test depends on time progressing duration execution.

0 commit comments

Comments
 (0)