Skip to content

Commit 25dac29

Browse files
committed
react/cache
1 parent 2badbe8 commit 25dac29

3 files changed

Lines changed: 134 additions & 2 deletions

File tree

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "valgorithms/psr-null-cache",
33
"type": "library",
44
"homepage": "https://github.com/valgorithms/PSR-NullCache",
5-
"description": "NullCache implementation (Fake Storage) of PSR-16 with Basic data validation",
5+
"description": "NullCache implementation (Fake Storage) of PSR-16 and ReactPHP with Basic data validation",
66
"license": "LGPL-3.0+",
77
"authors": [
88
{
@@ -17,7 +17,8 @@
1717
"require": {
1818
"php": "^8.0",
1919
"psr/simple-cache": "^1.0",
20-
"psr/cache": "^1.0"
20+
"psr/cache": "^1.0",
21+
"react/cache": "^0.5 || ^0.6 || ^1.0"
2122
},
2223
"provide": {
2324
"psr/simple-cache-implementation": "1.0"
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ihipop\PsrNullCache\SimpleCache;
4+
5+
use ihipop\PsrNullCache\Exception\InvalidArgumentException;
6+
7+
trait NullCacheReactTrait
8+
{
9+
public static function typeString(mixed $object): string
10+
{
11+
return \is_object($object) ? \get_class($object) : \gettype($object);
12+
}
13+
14+
public static function validateKey(mixed $key): string
15+
{
16+
if (!\is_string($key)) {
17+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', self::typeString($key)));
18+
}
19+
if ('' === $key) {
20+
throw new InvalidArgumentException('Cache key length must be greater than zero');
21+
}
22+
if (false !== strpbrk($key, '{}()/\@:')) {
23+
throw new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters {}()/\@:', $key));
24+
}
25+
26+
return $key;
27+
}
28+
29+
public static function traversToArray(mixed $travers): array
30+
{
31+
if ($travers instanceof \Traversable) {
32+
$travers = iterator_to_array($travers, false);
33+
}
34+
if (!\is_array($travers)) {
35+
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given',
36+
self::typeString($travers)));
37+
}
38+
39+
return $travers;
40+
}
41+
42+
/** @inheritdoc */
43+
public function get($key, $default = null)
44+
{
45+
self::validateKey($key);
46+
47+
return $default;
48+
}
49+
50+
/** @inheritdoc */
51+
public function set($key, $value, $ttl = null)
52+
{
53+
self::validateKey($key);
54+
55+
return $this->defaultBool;
56+
}
57+
58+
/** @inheritdoc */
59+
public function delete($key)
60+
{
61+
self::validateKey($key);
62+
63+
return $this->defaultBool;
64+
}
65+
66+
/** @inheritdoc */
67+
public function clear()
68+
{
69+
return $this->defaultBool;
70+
}
71+
72+
/** @inheritdoc */
73+
public function getMultiple(array $keys, $default = null)
74+
{
75+
$keys = self::traversToArray($keys);
76+
foreach ($keys as $value) {
77+
self::validateKey($value);
78+
}
79+
foreach ($keys as $key) {
80+
yield $key => $default;
81+
}
82+
}
83+
84+
/** @inheritdoc */
85+
public function setMultiple(array $values, $ttl = null)
86+
{
87+
$KeyValues = self::traversToArray($values);
88+
89+
foreach ($KeyValues as $key => $__) {
90+
self::validateKey($key);
91+
}
92+
93+
return $this->defaultBool;
94+
}
95+
96+
/** @inheritdoc */
97+
public function deleteMultiple(array $keys)
98+
{
99+
$keys = self::traversToArray($keys);
100+
foreach ($keys as $value) {
101+
self::validateKey($value);
102+
}
103+
104+
return $this->defaultBool;
105+
}
106+
107+
/** @inheritdoc */
108+
public function has($key)
109+
{
110+
self::validateKey($key);
111+
112+
return $this->defaultBool;
113+
}
114+
}

src/SimpleCache/NullReactCache.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ihipop\PsrNullCache\SimpleCache;
4+
5+
use React\Cache\CacheInterface;
6+
7+
class NullCache implements CacheInterface
8+
{
9+
protected bool $defaultBool;
10+
11+
public function __construct(bool $defaultBool = false)
12+
{
13+
$this->defaultBool = $defaultBool;
14+
}
15+
16+
use NullCacheReactTrait;
17+
}

0 commit comments

Comments
 (0)