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
4 changes: 3 additions & 1 deletion src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,9 @@ protected function getSeconds($ttl)
$duration = $this->parseDateInterval($ttl);

if ($duration instanceof DateTimeInterface) {
$duration = Carbon::now()->diffInSeconds($duration, false);
$duration = (int) ceil(
Carbon::now()->diffInMilliseconds($duration, false) / 1000
);
Comment on lines +627 to +629
Copy link
Member Author

@timacdonald timacdonald Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed this from diffInSeconds to diffInMilliseconds / 1000 because Carbon 2 would always return an int while carbon 3 returns a float.

This allows us to ensure we get the correct value on both versions.

}

return (int) ($duration > 0 ? $duration : 0);
Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/Cache/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Illuminate\Tests\Integration\Cache;

use Illuminate\Cache\Events\KeyWritten;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\TestCase;

Expand Down Expand Up @@ -235,4 +237,21 @@ public function testItImplicitlyClearsTtlKeysFromFileDriver()
$this->assertFalse($cache->getFilesystem()->exists($cache->path('illuminate:cache:flexible:created:count')));
$this->assertTrue($cache->missing('illuminate:cache:flexible:created:count'));
}

public function testItRoundsDateTimeValuesToAccountForTimePassedDuringScriptExecution()
{
// do not freeze time as this test depends on time progressing duration execution.
$cache = Cache::driver('array');
$events = [];
Event::listen(function (KeyWritten $event) use (&$events) {
$events[] = $event;
});

$result = $cache->put('foo', 'bar', now()->addSecond());

$this->assertTrue($result);
$this->assertCount(1, $events);
$this->assertSame('foo', $events[0]->key);
$this->assertSame(1, $events[0]->seconds);
}
}