-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
317 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Tracy (https://tracy.nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\Bridges\Psr; | ||
|
||
use DateInterval; | ||
use DateTimeImmutable; | ||
use Generator; | ||
use Nette; | ||
use Psr; | ||
|
||
class PsrCacheAdapter implements Psr\SimpleCache\CacheInterface | ||
{ | ||
public function __construct( | ||
private Nette\Caching\Storage $storage, | ||
) { | ||
} | ||
|
||
|
||
public function get(string $key, mixed $default = null): mixed | ||
{ | ||
return $this->storage->read($key) ?? $default; | ||
} | ||
|
||
|
||
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool | ||
{ | ||
$dependencies = []; | ||
if ($ttl !== null) { | ||
$dependencies[Nette\Caching\Cache::Expire] = self::ttlToSeconds($ttl); | ||
} | ||
|
||
$this->storage->write($key, $value, $dependencies); | ||
|
||
return true; | ||
} | ||
|
||
|
||
public function delete(string $key): bool | ||
{ | ||
$this->storage->remove($key); | ||
|
||
return true; | ||
} | ||
|
||
|
||
public function clear(): bool | ||
{ | ||
$this->storage->clean([Nette\Caching\Cache::All => true]); | ||
|
||
return true; | ||
} | ||
|
||
|
||
/** | ||
* @return Generator<string, mixed> | ||
*/ | ||
public function getMultiple(iterable $keys, mixed $default = null): iterable | ||
{ | ||
foreach ($keys as $name) { | ||
yield $name => $this->get($name, $default); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @param iterable<string|int, mixed> $values | ||
*/ | ||
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool | ||
{ | ||
$ttl = self::ttlToSeconds($ttl); | ||
|
||
foreach ($values as $key => $value) { | ||
$this->set((string) $key, $value, $ttl); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
public function deleteMultiple(iterable $keys): bool | ||
{ | ||
foreach ($keys as $value) { | ||
$this->delete($value); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
public function has(string $key): bool | ||
{ | ||
return $this->storage->read($key) !== null; | ||
} | ||
|
||
|
||
private static function ttlToSeconds(null|int|DateInterval $ttl = null): ?int | ||
{ | ||
if ($ttl instanceof DateInterval) { | ||
return self::dateIntervalToSeconds($ttl); | ||
} | ||
|
||
return $ttl; | ||
} | ||
|
||
|
||
private static function dateIntervalToSeconds(DateInterval $dateInterval): int | ||
{ | ||
$now = new DateTimeImmutable(); | ||
$expiresAt = $now->add($dateInterval); | ||
|
||
return $expiresAt->getTimestamp() - $now->getTimestamp(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Bridges\Psr\PsrCacheAdapter; | ||
use Nette\Caching\Storages\DevNullStorage; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
test('get', function () { | ||
$cache = new PsrCacheAdapter(new DevNullStorage()); | ||
Assert::null($cache->get('test')); | ||
}); | ||
|
||
test('get with default', function () { | ||
$cache = new PsrCacheAdapter(new DevNullStorage()); | ||
Assert::true($cache->get('test', true)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Bridges\Psr\PsrCacheAdapter; | ||
use Nette\Caching\Storages\DevNullStorage; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
test('get multiple', function () { | ||
$cache = new PsrCacheAdapter(new DevNullStorage()); | ||
$x = iterator_to_array($cache->getMultiple(['test', 'test1'])); | ||
|
||
Assert::same([ | ||
'test' => null, | ||
'test1' => null, | ||
], $x); | ||
}); | ||
|
||
test('get multiple with default', function () { | ||
$cache = new PsrCacheAdapter(new DevNullStorage()); | ||
$x = iterator_to_array($cache->getMultiple(['test', 'test1'], true)); | ||
|
||
Assert::same([ | ||
'test' => true, | ||
'test1' => true, | ||
], $x); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Bridges\Psr\PsrCacheAdapter; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
require __DIR__ . '/../Caching/Cache.php'; | ||
|
||
test('has', function () { | ||
$storage = new TestStorage(); | ||
$cache = new PsrCacheAdapter($storage); | ||
$cache->set('test1', '1'); | ||
|
||
Assert::true($cache->has('test1')); | ||
Assert::false($cache->has('test2')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Bridges\Psr\PsrCacheAdapter; | ||
use Nette\Caching; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
require __DIR__ . '/../Caching/Cache.php'; | ||
|
||
test('set ttl unlimited', function () { | ||
$storage = new TestStorage(); | ||
$cache = new PsrCacheAdapter($storage); | ||
$cache->set('test', '1'); | ||
Assert::same([ | ||
'data' => '1', | ||
'dependencies' => [], | ||
], $storage->read('test')); | ||
}); | ||
|
||
test('set ttl int', function () { | ||
$storage = new TestStorage(); | ||
$cache = new PsrCacheAdapter($storage); | ||
|
||
$cache->set('test', '2', 1); | ||
Assert::same([ | ||
'data' => '2', | ||
'dependencies' => [ | ||
Caching\Cache::Expire => 1, | ||
], | ||
], $storage->read('test')); | ||
}); | ||
|
||
test('set ttl DateInterval', function () { | ||
$storage = new TestStorage(); | ||
$cache = new PsrCacheAdapter($storage); | ||
|
||
$cache->set('test', '3', new DateInterval('P3Y6M4DT12H30M5S')); | ||
Assert::same([ | ||
'data' => '3', | ||
'dependencies' => [ | ||
Caching\Cache::Expire => 110_899_805, | ||
], | ||
], $storage->read('test')); | ||
|
||
$cache->set('test', '4', (new DateTime('1978-01-23 05:06:07'))->diff(new DateTime('1986-12-30 07:08:09'))); | ||
Assert::same([ | ||
'data' => '4', | ||
'dependencies' => [ | ||
Caching\Cache::Expire => 282_016_922, | ||
], | ||
], $storage->read('test')); | ||
|
||
$cache->set('test', '5', (new DateTime('1986-12-30 07:08:09'))->diff(new DateTime('1978-01-23 05:06:07'))); | ||
Assert::same([ | ||
'data' => '5', | ||
'dependencies' => [ | ||
Caching\Cache::Expire => -282_016_922, | ||
], | ||
], $storage->read('test')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Bridges\Psr\PsrCacheAdapter; | ||
use Nette\Caching; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
require __DIR__ . '/../Caching/Cache.php'; | ||
|
||
test('set multiple ttl unlimited', function () { | ||
$storage = new TestStorage(); | ||
$cache = new PsrCacheAdapter($storage); | ||
|
||
$cache->setMultiple(['test1' => '1', 'test2' => '2']); | ||
|
||
Assert::same([ | ||
'data' => '1', | ||
'dependencies' => [], | ||
], $storage->read('test1')); | ||
Assert::same([ | ||
'data' => '2', | ||
'dependencies' => [], | ||
], $storage->read('test2')); | ||
}); | ||
|
||
test('set multiple ttl int', function () { | ||
$storage = new TestStorage(); | ||
$cache = new PsrCacheAdapter($storage); | ||
|
||
$cache->setMultiple(['test1' => '1', 'test2' => '2'], 1); | ||
|
||
Assert::same([ | ||
'data' => '1', | ||
'dependencies' => [ | ||
Caching\Cache::Expire => 1, | ||
], | ||
], $storage->read('test1')); | ||
|
||
Assert::same([ | ||
'data' => '2', | ||
'dependencies' => [ | ||
Caching\Cache::Expire => 1, | ||
], | ||
], $storage->read('test2')); | ||
}); | ||
|
||
test('set multiple ttl DateInterval', function () { | ||
$storage = new TestStorage(); | ||
$cache = new PsrCacheAdapter($storage); | ||
|
||
$cache->setMultiple(['test1' => '1', 'test2' => '2'], new DateInterval('P3Y6M4DT12H30M5S')); | ||
Assert::same([ | ||
'data' => '1', | ||
'dependencies' => [ | ||
Caching\Cache::Expire => 110_899_805, | ||
], | ||
], $storage->read('test1')); | ||
|
||
Assert::same([ | ||
'data' => '2', | ||
'dependencies' => [ | ||
Caching\Cache::Expire => 110_899_805, | ||
], | ||
], $storage->read('test2')); | ||
}); |
Empty file.