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
37 changes: 19 additions & 18 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php namespace Illuminate\Cache;

use Closure;
use DateTime;
use DateInterval;
use ArrayAccess;
use Carbon\Carbon;
use DateTimeInterface;
use Illuminate\Support\Traits\MacroableTrait;

class Repository implements ArrayAccess {
Expand Down Expand Up @@ -82,12 +83,12 @@ public function pull($key, $default = null)
*
* @param string $key
* @param mixed $value
* @param \DateTime|int $minutes
* @param \DateTimeInterface|\DateInterval $ttl
* @return void
*/
public function put($key, $value, $minutes)
public function put($key, $value, DateTimeInterface|DateInterval $ttl)
{
$minutes = $this->getMinutes($minutes);
$minutes = $this->getMinutes($ttl);

if ( ! is_null($minutes))
{
Expand All @@ -100,14 +101,14 @@ public function put($key, $value, $minutes)
*
* @param string $key
* @param mixed $value
* @param \DateTime|int $minutes
* @param \DateTimeInterface|\DateInterval $ttl
* @return bool
*/
public function add($key, $value, $minutes)
public function add($key, $value, DateTimeInterface|DateInterval $ttl)
{
if (is_null($this->get($key)))
{
$this->put($key, $value, $minutes); return true;
$this->put($key, $value, $ttl); return true;
}

return false;
Expand All @@ -117,11 +118,11 @@ public function add($key, $value, $minutes)
* Get an item from the cache, or store the default value.
*
* @param string $key
* @param \DateTime|int $minutes
* @param \DateTimeInterface|\DateInterval $ttl
* @param \Closure $callback
* @return mixed
*/
public function remember($key, $minutes, Closure $callback)
public function remember($key, DateTimeInterface|DateInterval $ttl, Closure $callback)
{
// If the item exists in the cache we will just return this immediately
// otherwise we will execute the given Closure and cache the result
Expand All @@ -131,7 +132,7 @@ public function remember($key, $minutes, Closure $callback)
return $value;
}

$this->put($key, $value = $callback(), $minutes);
$this->put($key, $value = $callback(), $ttl);

return $value;
}
Expand Down Expand Up @@ -232,7 +233,7 @@ public function offsetGet($key): mixed
*/
public function offsetSet($key, $value): void
{
$this->put($key, $value, $this->default);
$this->put($key, $value, Carbon::now()->addMinutes($this->default));
}

/**
Expand All @@ -247,21 +248,21 @@ public function offsetUnset($key): void
}

/**
* Calculate the number of minutes with the given duration.
* Calculate the number of minutes until the given TTL.
*
* @param \DateTime|int $duration
* @param \DateTimeInterface|\DateInterval $duration
* @return int|null
*/
protected function getMinutes($duration)
{
if ($duration instanceof DateTime)
if ($duration instanceof DateInterval)
{
$fromNow = Carbon::instance($duration)->diffInMinutes();

return $fromNow > 0 ? $fromNow : null;
$duration = Carbon::now()->add($duration);
}

return is_string($duration) ? (int) $duration : $duration;
$fromNow = Carbon::instance($duration)->diffInMinutes();

return $fromNow > 0 ? $fromNow : null;
}

/**
Expand Down
35 changes: 18 additions & 17 deletions src/Illuminate/Cache/TaggedCache.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php namespace Illuminate\Cache;

use Closure;
use DateTime;
use DateInterval;
use Carbon\Carbon;
use DateTimeInterface;

class TaggedCache implements StoreInterface {

Expand Down Expand Up @@ -63,12 +64,12 @@ public function get($key, $default = null)
*
* @param string $key
* @param mixed $value
* @param \DateTime|int $minutes
* @param \DateTimeInterface|\DateInterval $ttl
* @return void
*/
public function put($key, $value, $minutes)
public function put($key, $value, $ttl)
{
$minutes = $this->getMinutes($minutes);
$minutes = $this->getMinutes($ttl);

if ( ! is_null($minutes))
{
Expand All @@ -81,14 +82,14 @@ public function put($key, $value, $minutes)
*
* @param string $key
* @param mixed $value
* @param \DateTime|int $minutes
* @param \DateTimeInterface|\DateInterval $ttl
* @return bool
*/
public function add($key, $value, $minutes)
public function add($key, $value, DateTimeInterface|DateInterval $ttl)
{
if (is_null($this->get($key)))
{
$this->put($key, $value, $minutes); return true;
$this->put($key, $value, $ttl); return true;
}

return false;
Expand Down Expand Up @@ -155,18 +156,18 @@ public function flush()
* Get an item from the cache, or store the default value.
*
* @param string $key
* @param \DateTime|int $minutes
* @param \DateTimeInterface|\DateInterval $ttl
* @param \Closure $callback
* @return mixed
*/
public function remember($key, $minutes, Closure $callback)
public function remember($key, DateTimeInterface|DateInterval $ttl, Closure $callback)
{
// If the item exists in the cache we will just return this immediately
// otherwise we will execute the given Closure and cache the result
// of that execution for the given number of minutes in storage.
if ( ! is_null($value = $this->get($key))) return $value;

$this->put($key, $value = $callback(), $minutes);
$this->put($key, $value = $callback(), $ttl);

return $value;
}
Expand Down Expand Up @@ -224,21 +225,21 @@ public function getPrefix()
}

/**
* Calculate the number of minutes with the given duration.
* Calculate the number of minutes until the given TTL.
*
* @param \DateTime|int $duration
* @param \DateTimeInterface|\DateInterval $duration
* @return int|null
*/
protected function getMinutes($duration)
{
if ($duration instanceof DateTime)
if ($duration instanceof DateInterval)
{
$fromNow = Carbon::instance($duration)->diffInMinutes();

return $fromNow > 0 ? $fromNow : null;
$duration = Carbon::now()->add($duration);
}

return is_string($duration) ? (int) $duration : $duration;
$fromNow = Carbon::instance($duration)->diffInMinutes();

return $fromNow > 0 ? $fromNow : null;
}

}
3 changes: 2 additions & 1 deletion src/Illuminate/CachedRouting/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/

use Closure;
use Carbon\Carbon;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Routing\Router as LaravelRouter;
Expand Down Expand Up @@ -114,7 +115,7 @@ public function cache($filename, Closure $callback, $cacheMinutes = 1440): ?stri
// Persist the routes, ignoring failures so a broken cache store
// never propagates out of boot (routes stay defined in memory).
try {
$cacher->put($cacheKey, $this->routes->getCacheableRoutes(), $cacheMinutes);
$cacher->put($cacheKey, $this->routes->getCacheableRoutes(), Carbon::now()->addMinutes($cacheMinutes));
} catch (\Throwable $e) {
// Best-effort cache; a write failure is non-fatal.
}
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Illuminate\Database\Query;

use Closure;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Grammars\Grammar;
Expand Down Expand Up @@ -1440,7 +1441,7 @@ public function getCached($columns = array('*'))
return $cache->rememberForever($key, $callback);
}

return $cache->remember($key, $minutes, $callback);
return $cache->remember($key, Carbon::now()->addMinutes($minutes), $callback);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Session/CacheBasedSessionHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Illuminate\Session;

use Carbon\Carbon;
use Illuminate\Cache\Repository;

class CacheBasedSessionHandler implements \SessionHandlerInterface {
Expand Down Expand Up @@ -57,7 +58,7 @@ public function read(string $id): string|false
public function write(string $id, string $data): bool
{
try {
$this->cache->put($id, $data, $this->minutes);
$this->cache->put($id, $data, Carbon::now()->addMinutes($this->minutes));
} catch (\Throwable) {
return false;
}
Expand Down
20 changes: 10 additions & 10 deletions tests/Cache/CacheRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Carbon\Carbon;
use Illuminate\Cache\StoreInterface;
use L4\Tests\BackwardCompatibleTestCase;
use Mockery as m;
Expand Down Expand Up @@ -53,18 +54,17 @@ public function testRememberMethodCallsPutAndReturnsDefault()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('get')->andReturn(null);
$repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', 10);
$result = $repo->remember('foo', 10, function() { return 'bar'; });
$repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', m::type('int'));
$result = $repo->remember('foo', Carbon::now()->addMinutes(10), function() { return 'bar'; });
$this->assertEquals('bar', $result);
}


/**
* Use Carbon object...
*/
// $repo = $this->getRepository();
// $repo->getStore()->shouldReceive('get')->andReturn(null);
// $repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', 9);
// $result = $repo->remember('foo', Carbon::now()->addMinutes(10), function() { return 'bar'; });
// $this->assertEquals('bar', $result);
public function testPutAcceptsDateIntervalTtl()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', m::type('int'));
$repo->put('foo', 'bar', new DateInterval('PT10M'));
}


Expand Down
13 changes: 7 additions & 6 deletions tests/Cache/CacheTaggedCacheTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Carbon\Carbon;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\StoreInterface;
use Illuminate\Cache\TagSet;
Expand All @@ -18,8 +19,8 @@ protected function tearDown(): void
public function testSectionCanBeFlushed()
{
$store = new ArrayStore;
$store->section('bop')->put('foo', 'bar', 10);
$store->section('zap')->put('baz', 'boom', 10);
$store->section('bop')->put('foo', 'bar', Carbon::now()->addMinutes(10));
$store->section('zap')->put('baz', 'boom', Carbon::now()->addMinutes(10));
$store->section('bop')->flush();
$this->assertNull($store->section('bop')->get('foo'));
$this->assertEquals('boom', $store->section('zap')->get('baz'));
Expand All @@ -30,7 +31,7 @@ public function testCacheCanBeSavedWithMultipleTags()
{
$store = new ArrayStore;
$tags = ['bop', 'zap'];
$store->tags($tags)->put('foo', 'bar', 10);
$store->tags($tags)->put('foo', 'bar', Carbon::now()->addMinutes(10));
$this->assertEquals('bar', $store->tags($tags)->get('foo'));
}

Expand All @@ -50,9 +51,9 @@ public function testCacheSavedWithMultipleTagsCanBeFlushed()
{
$store = new ArrayStore;
$tags1 = ['bop', 'zap'];
$store->tags($tags1)->put('foo', 'bar', 10);
$store->tags($tags1)->put('foo', 'bar', Carbon::now()->addMinutes(10));
$tags2 = ['bam', 'pow'];
$store->tags($tags2)->put('foo', 'bar', 10);
$store->tags($tags2)->put('foo', 'bar', Carbon::now()->addMinutes(10));
$store->tags('zap')->flush();
$this->assertNull($store->tags($tags1)->get('foo'));
$this->assertEquals('bar', $store->tags($tags2)->get('foo'));
Expand All @@ -62,7 +63,7 @@ public function testCacheSavedWithMultipleTagsCanBeFlushed()
public function testTagsWithStringArgument()
{
$store = new ArrayStore;
$store->tags('bop')->put('foo', 'bar', 10);
$store->tags('bop')->put('foo', 'bar', Carbon::now()->addMinutes(10));
$this->assertEquals('bar', $store->tags('bop')->get('foo'));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function testSelectWithCaching(): void

$driver->shouldReceive('remember')
->once()
->with($query->getCacheKey(), 5, m::type('Closure'))
->with($query->getCacheKey(), m::type(\DateTimeInterface::class), m::type('Closure'))
->andReturnUsing(function($key, $minutes, $callback) { return $callback(); });


Expand Down Expand Up @@ -134,7 +134,7 @@ public function testSelectWithCachingAndTags(): void

$taggedCache->shouldReceive('remember')
->once()
->with($query->getCacheKey(), 5, m::type('Closure'))
->with($query->getCacheKey(), m::type(\DateTimeInterface::class), m::type('Closure'))
->andReturnUsing(function($key, $minutes, $callback) { return $callback(); });

$this->assertEquals($query->get(), ['results']);
Expand Down
Loading