Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Cache: allows DateInterface and DateInterval #20034

Merged
merged 1 commit into from
Jul 13, 2017
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
21 changes: 13 additions & 8 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Illuminate\Cache;

use Closure;
use DateTime;
use ArrayAccess;
use DateInterval;
use DateTimeInterface;
use BadMethodCallException;
use Illuminate\Support\Carbon;
use Illuminate\Cache\Events\CacheHit;
Expand Down Expand Up @@ -159,7 +160,7 @@ public function pull($key, $default = null)
*
* @param string $key
* @param mixed $value
* @param \DateTime|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @return void
*/
public function put($key, $value, $minutes = null)
Expand All @@ -179,7 +180,7 @@ public function put($key, $value, $minutes = null)
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @return void
*/
public function putMany(array $values, $minutes)
Expand All @@ -198,7 +199,7 @@ public function putMany(array $values, $minutes)
*
* @param string $key
* @param mixed $value
* @param \DateTime|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @return bool
*/
public function add($key, $value, $minutes)
Expand Down Expand Up @@ -270,7 +271,7 @@ public function forever($key, $value)
* Get an item from the cache, or store the default value.
*
* @param string $key
* @param \DateTime|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @param \Closure $callback
* @return mixed
*/
Expand Down Expand Up @@ -477,13 +478,17 @@ public function offsetUnset($key)
/**
* Calculate the number of minutes with the given duration.
*
* @param \DateTime|float|int $duration
* @param \DateTimeInterface|\DateInterval|float|int $duration
* @return float|int|null
*/
protected function getMinutes($duration)
{
if ($duration instanceof DateTime) {
$duration = Carbon::now()->diffInSeconds(Carbon::instance($duration), false) / 60;
if ($duration instanceof DateInterval) {
$duration = Carbon::now()->add($duration);
}

if ($duration instanceof DateTimeInterface) {
$duration = Carbon::now()->diffInSeconds(Carbon::createFromTimestamp($duration->getTimestamp()), false) / 60;
}

return (int) ($duration * 60) > 0 ? $duration : null;
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Contracts/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function pull($key, $default = null);
*
* @param string $key
* @param mixed $value
* @param \DateTime|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int $minutes
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could probably have mixed there, but I've let this on purpose; this can be modified before 5.5 release if necessary.

* @return void
*/
public function put($key, $value, $minutes);
Expand All @@ -47,7 +47,7 @@ public function put($key, $value, $minutes);
*
* @param string $key
* @param mixed $value
* @param \DateTime|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @return bool
*/
public function add($key, $value, $minutes);
Expand Down Expand Up @@ -83,7 +83,7 @@ public function forever($key, $value);
* Get an item from the cache, or store the default value.
*
* @param string $key
* @param \DateTime|float|int $minutes
* @param \DateTimeInterface|\DateInterval|float|int $minutes
* @param \Closure $callback
* @return mixed
*/
Expand Down
29 changes: 29 additions & 0 deletions tests/Cache/CacheRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Illuminate\Tests\Cache;

use DateTime;
use DateInterval;
use Mockery as m;
use DateTimeImmutable;
use Illuminate\Support\Carbon;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -136,6 +139,32 @@ public function testCacheAddCallsRedisStoreAdd()
$this->assertTrue($repository->add('k', 'v', 60));
}

public function dataProviderTestGetMinutes()
{
return [
[Carbon::now()->addMinutes(5)],
[(new DateTime('2017-07-25 12:13:14 UTC'))->modify('+5 minutes')],
[(new DateTimeImmutable('2017-07-25 12:13:14 UTC'))->modify('+5 minutes')],
[new DateInterval('PT5M')],
[5],
];
}

/**
* @dataProvider dataProviderTestGetMinutes
* @param mixed $duration
*/
public function testGetMinutes($duration)
{
Carbon::setTestNow(Carbon::parse('2017-07-25 12:13:14 UTC'));

$repo = $this->getRepository();
$repo->getStore()->shouldReceive('put')->with($key = 'foo', $value = 'bar', 5);
$repo->put($key, $value, $duration);

Carbon::setTestNow();
}

public function testRegisterMacroWithNonStaticCall()
{
$repo = $this->getRepository();
Expand Down