Skip to content

Commit

Permalink
Added SimpleCache 2/3 compatible DevNullCache implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Jun 22, 2022
1 parent 8723d41 commit ad7ee15
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 66 deletions.
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<file>tests</file>

<exclude-pattern>tests/psalm-stubs.php</exclude-pattern>
<exclude-pattern>src/Cache</exclude-pattern>

<rule ref="Doctrine">
<exclude name="Generic.Files.LineLength.TooLong"/>
Expand Down
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<file name="stub/TwigMethods-Twig2.php" />
<file name="stub/TwigMethods-Twig3.php" />
<file name="tests/psalm-stubs.php" />
<directory name="src/Cache" />
</ignoreFiles>
</projectFiles>
<stubs>
Expand Down
76 changes: 10 additions & 66 deletions src/Cache/DevNullCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,19 @@

namespace Scoutapm\Cache;

use Psr\SimpleCache\CacheInterface;
use Composer\InstalledVersions;

use function array_combine;
use function array_map;
use function is_array;
use function iterator_to_array;

/** @internal */
final class DevNullCache implements CacheInterface
{
/** @inheritDoc */
public function get($key, $default = null)
{
return $default;
}

/** @inheritDoc */
public function set($key, $value, $ttl = null): bool
{
return true;
}

/** @inheritDoc */
public function delete($key): bool
{
return true;
}

public function clear(): bool
{
return true;
}

/** @inheritDoc */
public function getMultiple($keys, $default = null)
$simpleCacheVersion = InstalledVersions::getPrettyVersion('psr/simple-cache');
if (version_compare($simpleCacheVersion, '2.0.0', '<')) {
/** @internal */
final class DevNullCache extends DevNullCacheSimpleCache1
{
if (is_array($keys)) {
$keysAsArray = $keys;
} else {
$keysAsArray = iterator_to_array($keys, false);
}

return array_combine(
$keysAsArray,
array_map(
/** @return mixed */
function (string $key) use ($default) {
return $this->get($key, $default);
},
$keysAsArray
)
);
}

/** @inheritDoc */
public function setMultiple($values, $ttl = null): bool
{
return true;
}

/** @inheritDoc */
public function deleteMultiple($keys): bool
{
return true;
}
return;
}

/** @inheritDoc */
public function has($key): bool
{
return false;
}
/** @internal */
final class DevNullCache extends DevNullCacheSimpleCache2And3
{
}
78 changes: 78 additions & 0 deletions src/Cache/DevNullCacheSimpleCache1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Scoutapm\Cache;

use Psr\SimpleCache\CacheInterface;

use function array_combine;
use function array_map;
use function is_array;
use function iterator_to_array;

/** @internal */
abstract class DevNullCacheSimpleCache1 implements CacheInterface
{
/** @inheritDoc */
public function get($key, $default = null)
{
return $default;
}

/** @inheritDoc */
public function set($key, $value, $ttl = null): bool
{
return true;
}

/** @inheritDoc */
public function delete($key): bool
{
return true;
}

public function clear(): bool
{
return true;
}

/** @inheritDoc */
public function getMultiple($keys, $default = null)
{
if (is_array($keys)) {
$keysAsArray = $keys;
} else {
$keysAsArray = iterator_to_array($keys, false);
}

return array_combine(
$keysAsArray,
array_map(
/** @return mixed */
function (string $key) use ($default) {
return $this->get($key, $default);
},
$keysAsArray
)
);
}

/** @inheritDoc */
public function setMultiple($values, $ttl = null): bool
{
return true;
}

/** @inheritDoc */
public function deleteMultiple($keys): bool
{
return true;
}

/** @inheritDoc */
public function has($key): bool
{
return false;
}
}
79 changes: 79 additions & 0 deletions src/Cache/DevNullCacheSimpleCache2And3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Scoutapm\Cache;

use DateInterval;
use Psr\SimpleCache\CacheInterface;

use function array_combine;
use function array_map;
use function is_array;
use function iterator_to_array;

/** @internal */
abstract class DevNullCacheSimpleCache2And3 implements CacheInterface
{
/** @inheritDoc */
public function get(string $key, mixed $default = null): mixed
{
return $default;
}

/** @inheritDoc */
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
return true;
}

/** @inheritDoc */
public function delete(string $key): bool
{
return true;
}

public function clear(): bool
{
return true;
}

/** @inheritDoc */
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
if (is_array($keys)) {
$keysAsArray = $keys;
} else {
$keysAsArray = iterator_to_array($keys, false);
}

return array_combine(
$keysAsArray,
array_map(
/** @return mixed */
function (string $key) use ($default) {
return $this->get($key, $default);
},
$keysAsArray
)
);
}

/** @inheritDoc */
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
return true;
}

/** @inheritDoc */
public function deleteMultiple(iterable $keys): bool
{
return true;
}

/** @inheritDoc */
public function has(string $key): bool
{
return false;
}
}

0 comments on commit ad7ee15

Please sign in to comment.