Skip to content
Open
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
8 changes: 4 additions & 4 deletions system/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function save(string $key, mixed $value, int $ttl = 60): bool;
* Attempts to get an item from the cache, or executes the callback
* and stores the result on cache miss.
*
* @param string $key Cache item name
* @param int $ttl Time To Live, in seconds
* @param Closure(): mixed $callback Callback executed on cache miss
* @param string $key Cache item name
* @param (callable(mixed): int)|int $ttl Time To Live, in seconds
* @param Closure(): mixed $callback Callback executed on cache miss
*/
public function remember(string $key, int $ttl, Closure $callback): mixed;
public function remember(string $key, callable|int $ttl, Closure $callback): mixed;

/**
* Deletes a specific item from the cache store.
Expand Down
6 changes: 5 additions & 1 deletion system/Cache/Handlers/ApcuHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ public function save(string $key, $value, int $ttl = 60): bool
return apcu_store($key, $value, $ttl);
}

public function remember(string $key, int $ttl, Closure $callback): mixed
public function remember(string $key, callable|int $ttl, Closure $callback): mixed
{
if (is_callable($ttl)) {
return parent::remember($key, $ttl, $callback);
}
Comment thread
patel-vansh marked this conversation as resolved.

$key = static::validateKey($key, $this->prefix);

return apcu_entry($key, $callback, $ttl);
Expand Down
10 changes: 8 additions & 2 deletions system/Cache/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,21 @@ public static function validateKey($key, $prefix = ''): string
return strlen($prefix . $key) > static::MAX_KEY_LENGTH ? $prefix . md5($key) : $prefix . $key;
}

public function remember(string $key, int $ttl, Closure $callback): mixed
public function remember(string $key, callable|int $ttl, Closure $callback): mixed
{
$value = $this->get($key);

if ($value !== null) {
return $value;
}

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

if (is_callable($ttl)) {
$ttl = $ttl($value);
}

Comment thread
patel-vansh marked this conversation as resolved.
$this->save($key, $value, $ttl);

return $value;
}
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/DummyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function get(string $key): mixed
return null;
}

public function remember(string $key, int $ttl, Closure $callback): mixed
public function remember(string $key, callable|int $ttl, Closure $callback): mixed
{
return null;
}
Expand Down
10 changes: 8 additions & 2 deletions system/Test/Mock/MockCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,21 @@ public function get(string $key): mixed
*
* @return bool|null
*/
public function remember(string $key, int $ttl, Closure $callback): mixed
public function remember(string $key, callable|int $ttl, Closure $callback): mixed
{
$value = $this->get($key);

if ($value !== null) {
return $value;
}

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

if (is_callable($ttl)) {
$ttl = $ttl($value);
}

$this->save($key, $value, $ttl);

return $value;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/system/Cache/Handlers/ApcuHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Cache\Handlers;

use ArgumentCountError;
use CodeIgniter\Cache\CacheFactory;
use CodeIgniter\CLI\CLI;
use CodeIgniter\I18n\Time;
Expand Down Expand Up @@ -92,6 +93,48 @@ public function testRemember(): void
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallable(): void
{
$this->handler->remember(self::$key1, static fn (): int => 2, static fn (): string => 'value');

$this->assertSame('value', $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallableAndValuePassed(): void
{
$this->handler->remember(self::$key1, static fn ($value): int => $value[0], static fn (): array => [2, 3]);

$this->assertSame([2, 3], $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

public function testRememberWithTTLCallableAndMultipleParameters(): void
{
$this->expectException(ArgumentCountError::class);

/** @phpstan-ignore argument.type */
$this->handler->remember(self::$key1, static fn ($a, $b): int => 2, static fn (): string => 'value');
}

public function testSave(): void
{
$this->assertTrue($this->handler->save(self::$key1, 'value'));
Expand Down
14 changes: 14 additions & 0 deletions tests/system/Cache/Handlers/DummyHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public function testRemember(): void
$this->assertNull($dummyHandler);
}

public function testRememberWithTTLCallable(): void
{
$dummyHandler = $this->handler->remember('key', static fn (): int => 2, static fn (): string => 'value');

$this->assertNull($dummyHandler);
}

public function testRememberWithTTLCallableAndValuePassed(): void
{
$dummyHandler = $this->handler->remember('key', static fn ($value): int => $value[0], static fn (): array => [2, 3]);

$this->assertNull($dummyHandler);
}

public function testSave(): void
{
$this->assertTrue($this->handler->save('key', 'value'));
Expand Down
43 changes: 43 additions & 0 deletions tests/system/Cache/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Cache\Handlers;

use ArgumentCountError;
use CodeIgniter\Cache\CacheFactory;
use CodeIgniter\Cache\Exceptions\CacheException;
use CodeIgniter\CLI\CLI;
Expand Down Expand Up @@ -144,6 +145,48 @@ public function testRemember(): void
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallable(): void
{
$this->handler->remember(self::$key1, static fn (): int => 2, static fn (): string => 'value');

$this->assertSame('value', $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallableAndValuePassed(): void
{
$this->handler->remember(self::$key1, static fn ($value): int => $value[0], static fn (): array => [2, 3]);

$this->assertSame([2, 3], $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

public function testRememberWithTTLCallableAndMultipleParameters(): void
{
$this->expectException(ArgumentCountError::class);

/** @phpstan-ignore argument.type */
$this->handler->remember(self::$key1, static fn ($a, $b): int => 2, static fn (): string => 'value');
}

/**
* chmod('path', 0444) does not work on Windows
*/
Expand Down
43 changes: 43 additions & 0 deletions tests/system/Cache/Handlers/MemcachedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Cache\Handlers;

use ArgumentCountError;
use CodeIgniter\Cache\CacheFactory;
use CodeIgniter\Cache\LockStoreInterface;
use CodeIgniter\Cache\LockStoreProviderInterface;
Expand Down Expand Up @@ -101,6 +102,48 @@ public function testRemember(): void
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallable(): void
{
$this->handler->remember(self::$key1, static fn (): int => 2, static fn (): string => 'value');

$this->assertSame('value', $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallableAndValuePassed(): void
{
$this->handler->remember(self::$key1, static fn ($value): int => $value[0], static fn (): array => [2, 3]);

$this->assertSame([2, 3], $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

public function testRememberWithTTLCallableAndMultipleParameters(): void
{
$this->expectException(ArgumentCountError::class);

/** @phpstan-ignore argument.type */
$this->handler->remember(self::$key1, static fn ($a, $b): int => 2, static fn (): string => 'value');
}

public function testSave(): void
{
$this->assertTrue($this->handler->save(self::$key1, 'value'));
Expand Down
43 changes: 43 additions & 0 deletions tests/system/Cache/Handlers/PredisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Cache\Handlers;

use ArgumentCountError;
use CodeIgniter\Cache\CacheFactory;
use CodeIgniter\Cache\LockStoreInterface;
use CodeIgniter\Cache\LockStoreProviderInterface;
Expand Down Expand Up @@ -109,6 +110,48 @@ public function testRemember(): void
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallable(): void
{
$this->handler->remember(self::$key1, static fn (): int => 2, static fn (): string => 'value');

$this->assertSame('value', $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallableAndValuePassed(): void
{
$this->handler->remember(self::$key1, static fn ($value): int => $value[0], static fn (): array => [2, 3]);

$this->assertSame([2, 3], $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

public function testRememberWithTTLCallableAndMultipleParameters(): void
{
$this->expectException(ArgumentCountError::class);

/** @phpstan-ignore argument.type */
$this->handler->remember(self::$key1, static fn ($a, $b): int => 2, static fn (): string => 'value');
}

public function testSave(): void
{
$this->assertTrue($this->handler->save(self::$key1, 'value'));
Expand Down
43 changes: 43 additions & 0 deletions tests/system/Cache/Handlers/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Cache\Handlers;

use ArgumentCountError;
use CodeIgniter\Cache\CacheFactory;
use CodeIgniter\Cache\LockStoreInterface;
use CodeIgniter\Cache\LockStoreProviderInterface;
Expand Down Expand Up @@ -110,6 +111,48 @@ public function testRemember(): void
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallable(): void
{
$this->handler->remember(self::$key1, static fn (): int => 2, static fn (): string => 'value');

$this->assertSame('value', $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

/**
* This test waits for 3 seconds before last assertion so this
* is naturally a "slow" test on the perspective of the default limit.
*
* @timeLimit 3.5
*/
public function testRememberWithTTLCallableAndValuePassed(): void
{
$this->handler->remember(self::$key1, static fn ($value): int => $value[0], static fn (): array => [2, 3]);

$this->assertSame([2, 3], $this->handler->get(self::$key1));
$this->assertNull($this->handler->get(self::$dummy));

CLI::wait(3);
$this->assertNull($this->handler->get(self::$key1));
}

public function testRememberWithTTLCallableAndMultipleParameters(): void
{
$this->expectException(ArgumentCountError::class);

/** @phpstan-ignore argument.type */
$this->handler->remember(self::$key1, static fn ($a, $b): int => 2, static fn (): string => 'value');
}

public function testSave(): void
{
$this->assertTrue($this->handler->save(self::$key1, 'value'));
Expand Down
Loading
Loading