-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
feat: add psr-16 implementation #77
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't add dependency for version 1.0.