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

Allow setting default_ttl #3980

Merged
14 changes: 14 additions & 0 deletions config/excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@
'illuminate' => [
'store' => null,
],

/*
|--------------------------------------------------------------------------
| Cache Time-to-live (TTL)
|--------------------------------------------------------------------------
|
| The TTL of items written to cache. If you want to keep the items cached
| indefinitely, set this to null. Otherwise, set a number of seconds,
| a \DateInterval, or a callable.
|
| Allowable types: callable|\DateInterval|int|null
|
*/
'default_ttl' => 10800,
],

/*
Expand Down
26 changes: 22 additions & 4 deletions src/Cache/BatchCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ class BatchCache implements CacheInterface
*/
protected $memory;

/**
* @var null|int|\DateInterval|callable
*/
protected $defaultTTL = null;

/**
* @param CacheInterface $cache
* @param MemoryCache $memory
* @param null|int|\DateInterval|callable $defaultTTL
*/
public function __construct(CacheInterface $cache, MemoryCache $memory)
{
$this->cache = $cache;
$this->memory = $memory;
public function __construct(
CacheInterface $cache,
MemoryCache $memory,
null|int|\DateInterval|callable $defaultTTL = null
) {
$this->cache = $cache;
$this->memory = $memory;
$this->defaultTTL = $defaultTTL;
}

public function __sleep()
Expand Down Expand Up @@ -56,6 +66,10 @@ public function get(string $key, mixed $default = null): mixed
*/
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
if (func_num_args() === 2) {
$ttl = value($this->defaultTTL);
}

$this->memory->set($key, $value, $ttl);

if ($this->memory->reachedMemoryLimit()) {
Expand Down Expand Up @@ -120,6 +134,10 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
*/
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
{
if (func_num_args() === 1) {
$ttl = value($this->defaultTTL);
}

$this->memory->setMultiple($values, $ttl);

if ($this->memory->reachedMemoryLimit()) {
Expand Down
26 changes: 22 additions & 4 deletions src/Cache/BatchCacheDeprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ class BatchCacheDeprecated implements CacheInterface
*/
protected $memory;

/**
* @var null|int|\DateTimeInterface|callable
*/
protected $defaultTTL = null;

/**
* @param CacheInterface $cache
* @param MemoryCacheDeprecated $memory
* @param int|\DateTimeInterface|callable|null $defaultTTL
*/
public function __construct(CacheInterface $cache, MemoryCacheDeprecated $memory)
{
$this->cache = $cache;
$this->memory = $memory;
public function __construct(
CacheInterface $cache,
MemoryCacheDeprecated $memory,
$defaultTTL = null
) {
$this->cache = $cache;
$this->memory = $memory;
$this->defaultTTL = $defaultTTL;
}

public function __sleep()
Expand Down Expand Up @@ -56,6 +66,10 @@ public function get($key, $default = null)
*/
public function set($key, $value, $ttl = null)
{
if (func_num_args() === 2) {
$ttl = value($this->defaultTTL);
}

$this->memory->set($key, $value, $ttl);

if ($this->memory->reachedMemoryLimit()) {
Expand Down Expand Up @@ -120,6 +134,10 @@ public function getMultiple($keys, $default = null)
*/
public function setMultiple($values, $ttl = null)
{
if (func_num_args() === 1) {
$ttl = value($this->defaultTTL);
}

$this->memory->setMultiple($values, $ttl);

if ($this->memory->reachedMemoryLimit()) {
Expand Down
6 changes: 4 additions & 2 deletions src/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ public function createBatchDriver(): CacheInterface
if (!InstalledVersions::satisfies(new VersionParser, 'psr/simple-cache', '^3.0')) {
return new BatchCacheDeprecated(
$this->createIlluminateDriver(),
$this->createMemoryDriver()
$this->createMemoryDriver(),
config('excel.cache.ttl')
);
}

return new BatchCache(
$this->createIlluminateDriver(),
$this->createMemoryDriver()
$this->createMemoryDriver(),
config('excel.cache.ttl')
);
}

Expand Down
85 changes: 83 additions & 2 deletions tests/Cache/BatchCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use DateInterval;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Events\KeyWritten;
use Illuminate\Cache\Repository;
use Illuminate\Support\Facades\Event;
use Maatwebsite\Excel\Cache\BatchCache;
use Maatwebsite\Excel\Cache\BatchCacheDeprecated;
use Maatwebsite\Excel\Cache\CacheManager;
Expand Down Expand Up @@ -176,6 +179,82 @@ public function it_persists_to_cache_when_memory_limit_reached_on_setting_multip
], $cache->getMultiple(['A1', 'A2', 'A3', 'A4', 'A5']));
}

/**
* @test
*
* @dataProvider defaultTTLDataProvider
*/
public function it_writes_to_cache_with_default_ttl($defaultTTL, $receivedAs)
{
config()->set('excel.cache.default_ttl', $defaultTTL);

$cache = $this->givenCache(['A1' => 'A1-value'], [], 1);
$this->cache->setEventDispatcher(Event::fake());
$cache->set('A2', 'A2-value');

$expectedTTL = value($receivedAs);

$dispatchedCollection = Event::dispatched(
KeyWritten::class,
function (KeyWritten $event) use ($expectedTTL) {
return $event->seconds === $expectedTTL;
});

$this->assertCount(2, $dispatchedCollection);
}

/**
* @test
*/
public function it_writes_to_cache_with_a_dateinterval_ttl()
{
// DateInterval is 1 minute
config()->set('excel.cache.default_ttl', new DateInterval('PT1M'));

$cache = $this->givenCache(['A1' => 'A1-value'], [], 1);
$this->cache->setEventDispatcher(Event::fake());
$cache->set('A2', 'A2-value');

$dispatchedCollection = Event::dispatched(
KeyWritten::class,
function (KeyWritten $event) {
return $event->seconds === 60;
});

$this->assertCount(2, $dispatchedCollection);
}

/**
* @test
*/
public function it_can_override_default_ttl()
{
config()->set('excel.cache.default_ttl', 1);

$cache = $this->givenCache(['A1' => 'A1-value'], [], 1);
$this->cache->setEventDispatcher(Event::fake());
$cache->set('A2', 'A2-value', null);

$dispatchedCollection = Event::dispatched(
KeyWritten::class,
function (KeyWritten $event) {
return $event->seconds === null;
});

$this->assertCount(2, $dispatchedCollection);
}

public static function defaultTTLDataProvider(): array
{
return [
'null (forever)' => [null, null],
'int value' => [$value = rand(1, 100), $value],
'callable' => [$closure = function () {
return 199;
}, $closure],
];
}

/**
* Construct a BatchCache with a in memory store
* and an array cache, pretending to be a persistence store.
Expand All @@ -200,13 +279,15 @@ private function givenCache(array $memory = [], array $persisted = [], int $memo
if (!InstalledVersions::satisfies(new VersionParser, 'psr/simple-cache', '^3.0')) {
return new BatchCacheDeprecated(
$this->cache,
$this->memory
$this->memory,
config('excel.cache.default_ttl')
);
}

return new BatchCache(
$this->cache,
$this->memory
$this->memory,
config('excel.cache.default_ttl')
);
}
}