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

feat: add psr-16 implementation #77

Merged
merged 2 commits into from
Jun 3, 2024
Merged
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"nette/di": "^3.1 || ^4.0",
"latte/latte": "^3.0.12",
"tracy/tracy": "^2.9",
"phpstan/phpstan": "^1.0"
"phpstan/phpstan": "^1.0",
"psr/simple-cache": "^2.0 || ^3.0"
Copy link
Contributor Author

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.

},
"conflict": {
"latte/latte": "<3.0.12"
Expand Down
120 changes: 120 additions & 0 deletions src/Bridges/Psr/PsrCacheAdapter.php
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();
}
}
19 changes: 19 additions & 0 deletions tests/Bridges.Psr/PsrCacheAdapter.get.phpt
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));
});
29 changes: 29 additions & 0 deletions tests/Bridges.Psr/PsrCacheAdapter.getMultiple.phpt
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);
});
18 changes: 18 additions & 0 deletions tests/Bridges.Psr/PsrCacheAdapter.has.phpt
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'));
});
62 changes: 62 additions & 0 deletions tests/Bridges.Psr/PsrCacheAdapter.set.phpt
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'));
});
67 changes: 67 additions & 0 deletions tests/Bridges.Psr/PsrCacheAdapter.setMultiple.phpt
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 added tests/lock
Empty file.