Skip to content

Commit 784cfbe

Browse files
Allow setting default_ttl (SpartnerNL#3980)
* allow setting default_ttl * style * allow defaultTTL to be callable * add defaultTTL to BatchCacheDeprecated * remove PHP8 union type from BatchCacheDeprecated * use DateInterval instead of DateTimeInterface * adds defaultTTL tests * fix Cache TTL sub-section * finalize tests * update CHANGELOG.md * make func_num_args less messy * style * only 2 arguments * default_ttl to 3 hours --------- Co-authored-by: Patrick Brouwers <[email protected]>
1 parent 769209f commit 784cfbe

File tree

5 files changed

+145
-12
lines changed

5 files changed

+145
-12
lines changed

config/excel.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,20 @@
259259
'illuminate' => [
260260
'store' => null,
261261
],
262+
263+
/*
264+
|--------------------------------------------------------------------------
265+
| Cache Time-to-live (TTL)
266+
|--------------------------------------------------------------------------
267+
|
268+
| The TTL of items written to cache. If you want to keep the items cached
269+
| indefinitely, set this to null. Otherwise, set a number of seconds,
270+
| a \DateInterval, or a callable.
271+
|
272+
| Allowable types: callable|\DateInterval|int|null
273+
|
274+
*/
275+
'default_ttl' => 10800,
262276
],
263277

264278
/*

src/Cache/BatchCache.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@ class BatchCache implements CacheInterface
1717
*/
1818
protected $memory;
1919

20+
/**
21+
* @var null|int|\DateInterval|callable
22+
*/
23+
protected $defaultTTL = null;
24+
2025
/**
2126
* @param CacheInterface $cache
2227
* @param MemoryCache $memory
28+
* @param null|int|\DateInterval|callable $defaultTTL
2329
*/
24-
public function __construct(CacheInterface $cache, MemoryCache $memory)
25-
{
26-
$this->cache = $cache;
27-
$this->memory = $memory;
30+
public function __construct(
31+
CacheInterface $cache,
32+
MemoryCache $memory,
33+
null|int|\DateInterval|callable $defaultTTL = null
34+
) {
35+
$this->cache = $cache;
36+
$this->memory = $memory;
37+
$this->defaultTTL = $defaultTTL;
2838
}
2939

3040
public function __sleep()
@@ -56,6 +66,10 @@ public function get(string $key, mixed $default = null): mixed
5666
*/
5767
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
5868
{
69+
if (func_num_args() === 2) {
70+
$ttl = value($this->defaultTTL);
71+
}
72+
5973
$this->memory->set($key, $value, $ttl);
6074

6175
if ($this->memory->reachedMemoryLimit()) {
@@ -120,6 +134,10 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
120134
*/
121135
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
122136
{
137+
if (func_num_args() === 1) {
138+
$ttl = value($this->defaultTTL);
139+
}
140+
123141
$this->memory->setMultiple($values, $ttl);
124142

125143
if ($this->memory->reachedMemoryLimit()) {

src/Cache/BatchCacheDeprecated.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@ class BatchCacheDeprecated implements CacheInterface
1717
*/
1818
protected $memory;
1919

20+
/**
21+
* @var null|int|\DateTimeInterface|callable
22+
*/
23+
protected $defaultTTL = null;
24+
2025
/**
2126
* @param CacheInterface $cache
2227
* @param MemoryCacheDeprecated $memory
28+
* @param int|\DateTimeInterface|callable|null $defaultTTL
2329
*/
24-
public function __construct(CacheInterface $cache, MemoryCacheDeprecated $memory)
25-
{
26-
$this->cache = $cache;
27-
$this->memory = $memory;
30+
public function __construct(
31+
CacheInterface $cache,
32+
MemoryCacheDeprecated $memory,
33+
$defaultTTL = null
34+
) {
35+
$this->cache = $cache;
36+
$this->memory = $memory;
37+
$this->defaultTTL = $defaultTTL;
2838
}
2939

3040
public function __sleep()
@@ -56,6 +66,10 @@ public function get($key, $default = null)
5666
*/
5767
public function set($key, $value, $ttl = null)
5868
{
69+
if (func_num_args() === 2) {
70+
$ttl = value($this->defaultTTL);
71+
}
72+
5973
$this->memory->set($key, $value, $ttl);
6074

6175
if ($this->memory->reachedMemoryLimit()) {
@@ -120,6 +134,10 @@ public function getMultiple($keys, $default = null)
120134
*/
121135
public function setMultiple($values, $ttl = null)
122136
{
137+
if (func_num_args() === 1) {
138+
$ttl = value($this->defaultTTL);
139+
}
140+
123141
$this->memory->setMultiple($values, $ttl);
124142

125143
if ($this->memory->reachedMemoryLimit()) {

src/Cache/CacheManager.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ public function createBatchDriver(): CacheInterface
5959
if (!InstalledVersions::satisfies(new VersionParser, 'psr/simple-cache', '^3.0')) {
6060
return new BatchCacheDeprecated(
6161
$this->createIlluminateDriver(),
62-
$this->createMemoryDriver()
62+
$this->createMemoryDriver(),
63+
config('excel.cache.ttl')
6364
);
6465
}
6566

6667
return new BatchCache(
6768
$this->createIlluminateDriver(),
68-
$this->createMemoryDriver()
69+
$this->createMemoryDriver(),
70+
config('excel.cache.ttl')
6971
);
7072
}
7173

tests/Cache/BatchCacheTest.php

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use Composer\InstalledVersions;
66
use Composer\Semver\VersionParser;
7+
use DateInterval;
78
use Illuminate\Cache\ArrayStore;
9+
use Illuminate\Cache\Events\KeyWritten;
810
use Illuminate\Cache\Repository;
11+
use Illuminate\Support\Facades\Event;
912
use Maatwebsite\Excel\Cache\BatchCache;
1013
use Maatwebsite\Excel\Cache\BatchCacheDeprecated;
1114
use Maatwebsite\Excel\Cache\CacheManager;
@@ -176,6 +179,82 @@ public function it_persists_to_cache_when_memory_limit_reached_on_setting_multip
176179
], $cache->getMultiple(['A1', 'A2', 'A3', 'A4', 'A5']));
177180
}
178181

182+
/**
183+
* @test
184+
*
185+
* @dataProvider defaultTTLDataProvider
186+
*/
187+
public function it_writes_to_cache_with_default_ttl($defaultTTL, $receivedAs)
188+
{
189+
config()->set('excel.cache.default_ttl', $defaultTTL);
190+
191+
$cache = $this->givenCache(['A1' => 'A1-value'], [], 1);
192+
$this->cache->setEventDispatcher(Event::fake());
193+
$cache->set('A2', 'A2-value');
194+
195+
$expectedTTL = value($receivedAs);
196+
197+
$dispatchedCollection = Event::dispatched(
198+
KeyWritten::class,
199+
function (KeyWritten $event) use ($expectedTTL) {
200+
return $event->seconds === $expectedTTL;
201+
});
202+
203+
$this->assertCount(2, $dispatchedCollection);
204+
}
205+
206+
/**
207+
* @test
208+
*/
209+
public function it_writes_to_cache_with_a_dateinterval_ttl()
210+
{
211+
// DateInterval is 1 minute
212+
config()->set('excel.cache.default_ttl', new DateInterval('PT1M'));
213+
214+
$cache = $this->givenCache(['A1' => 'A1-value'], [], 1);
215+
$this->cache->setEventDispatcher(Event::fake());
216+
$cache->set('A2', 'A2-value');
217+
218+
$dispatchedCollection = Event::dispatched(
219+
KeyWritten::class,
220+
function (KeyWritten $event) {
221+
return $event->seconds === 60;
222+
});
223+
224+
$this->assertCount(2, $dispatchedCollection);
225+
}
226+
227+
/**
228+
* @test
229+
*/
230+
public function it_can_override_default_ttl()
231+
{
232+
config()->set('excel.cache.default_ttl', 1);
233+
234+
$cache = $this->givenCache(['A1' => 'A1-value'], [], 1);
235+
$this->cache->setEventDispatcher(Event::fake());
236+
$cache->set('A2', 'A2-value', null);
237+
238+
$dispatchedCollection = Event::dispatched(
239+
KeyWritten::class,
240+
function (KeyWritten $event) {
241+
return $event->seconds === null;
242+
});
243+
244+
$this->assertCount(2, $dispatchedCollection);
245+
}
246+
247+
public static function defaultTTLDataProvider(): array
248+
{
249+
return [
250+
'null (forever)' => [null, null],
251+
'int value' => [$value = rand(1, 100), $value],
252+
'callable' => [$closure = function () {
253+
return 199;
254+
}, $closure],
255+
];
256+
}
257+
179258
/**
180259
* Construct a BatchCache with a in memory store
181260
* and an array cache, pretending to be a persistence store.
@@ -200,13 +279,15 @@ private function givenCache(array $memory = [], array $persisted = [], int $memo
200279
if (!InstalledVersions::satisfies(new VersionParser, 'psr/simple-cache', '^3.0')) {
201280
return new BatchCacheDeprecated(
202281
$this->cache,
203-
$this->memory
282+
$this->memory,
283+
config('excel.cache.default_ttl')
204284
);
205285
}
206286

207287
return new BatchCache(
208288
$this->cache,
209-
$this->memory
289+
$this->memory,
290+
config('excel.cache.default_ttl')
210291
);
211292
}
212293
}

0 commit comments

Comments
 (0)