Skip to content

Commit 02f0db9

Browse files
authored
Add support for psr cache interfaces (#37)
* Add support for psr-6 with the CacheItemPoolAdapter * Add support for psr-16 with the SimpleCacheAdapter * The package guzzlehttp/psr7 from version 1.7 breaks the tests, so constraint it at version <1.7 Thanks to @fcoedno for the contribution!
1 parent 4b5b788 commit 02f0db9

File tree

6 files changed

+317
-2
lines changed

6 files changed

+317
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ compliant requests that can be used as well as high level abstractions to ease d
4343
| Dependency | Version | Reason |
4444
|:--- |:---:|:--- |
4545
| **`doctrine/cache`** | ~1.3 | If you want to use the `DoctrineCacheAdapter` |
46+
| **`psr/cache`** | ^1.0 | If you want to use the `CacheItemPoolAdapter` |
47+
| **`psr/simple-cache`** | ^1.0 | If you want to use the `SimpleCacheAdapter` |
4648
| **`raphhh/trex-reflection`** | ~1.0 | If you want to use the `RequestCallbackValidator`s |
4749

4850
## Installation

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
1515
"ext-curl": "*",
1616
"ext-json": "*",
1717
"guzzlehttp/guzzle": "~6.3",
18+
"guzzlehttp/psr7": "<1.7",
1819
"beberlei/assert": "~2.7|~3.0",
1920
"flix-tech/avro-php": "^3.0|^4.0"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "~7.0",
2324
"phpstan/phpstan": "^0.12",
2425
"raphhh/trex-reflection": "~1.0",
25-
"doctrine/cache": "~1.3"
26+
"doctrine/cache": "~1.3",
27+
"psr/cache": "^1.0",
28+
"symfony/cache": "^5.1",
29+
"psr/simple-cache": "^1.0"
2630
},
2731
"suggest": {
2832
"raphhh/trex-reflection": "Needed if you want to use the `RequestCallbackValidator`",
29-
"doctrine/cache": "If you want to use the DoctrineCacheAdapter"
33+
"doctrine/cache": "If you want to use the DoctrineCacheAdapter",
34+
"psr/cache": "If you want to use the CacheItemPoolAdapter",
35+
"psr/simple-cache": "If you want to use the SimpleCacheAdapter"
3036
},
3137
"autoload": {
3238
"psr-4": {
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Registry\Cache;
6+
7+
use AvroSchema;
8+
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter;
9+
use Psr\Cache\CacheItemPoolInterface;
10+
11+
class CacheItemPoolAdapter implements CacheAdapter
12+
{
13+
/**
14+
* @var CacheItemPoolInterface
15+
*/
16+
private $cacheItemPool;
17+
18+
public function __construct(CacheItemPoolInterface $cacheItemPool)
19+
{
20+
$this->cacheItemPool = $cacheItemPool;
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function cacheSchemaWithId(AvroSchema $schema, int $schemaId): void
27+
{
28+
$item = $this->cacheItemPool->getItem((string) $schemaId);
29+
$item->set((string) $schema);
30+
$this->cacheItemPool->save($item);
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function cacheSchemaIdByHash(int $schemaId, string $schemaHash): void
37+
{
38+
$item = $this->cacheItemPool->getItem($schemaHash);
39+
$item->set($schemaId);
40+
$this->cacheItemPool->save($item);
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function cacheSchemaWithSubjectAndVersion(AvroSchema $schema, string $subject, int $version): void
47+
{
48+
$item = $this->cacheItemPool->getItem($this->makeKeyFromSubjectAndVersion($subject, $version));
49+
$item->set((string) $schema);
50+
$this->cacheItemPool->save($item);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*
56+
* @throws \AvroSchemaParseException
57+
*/
58+
public function getWithId(int $schemaId): ?AvroSchema
59+
{
60+
$item = $this->cacheItemPool->getItem((string) $schemaId);
61+
62+
if (!$item->isHit()) {
63+
return null;
64+
}
65+
66+
$rawSchema = $item->get();
67+
68+
return AvroSchema::parse($rawSchema);
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function getIdWithHash(string $hash): ?int
75+
{
76+
$item = $this->cacheItemPool->getItem($hash);
77+
78+
if (!$item->isHit()) {
79+
return null;
80+
}
81+
82+
return $item->get();
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*
88+
* @throws \AvroSchemaParseException
89+
*/
90+
public function getWithSubjectAndVersion(string $subject, int $version): ?AvroSchema
91+
{
92+
$item = $this->cacheItemPool->getItem(
93+
$this->makeKeyFromSubjectAndVersion($subject, $version)
94+
);
95+
96+
if (!$item->isHit()) {
97+
return null;
98+
}
99+
100+
$rawSchema = $item->get();
101+
102+
return AvroSchema::parse($rawSchema);
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function hasSchemaForId(int $schemaId): bool
109+
{
110+
return $this->cacheItemPool
111+
->getItem((string) $schemaId)
112+
->isHit();
113+
}
114+
115+
/**
116+
* {@inheritdoc}
117+
*/
118+
public function hasSchemaIdForHash(string $schemaHash): bool
119+
{
120+
return $this->cacheItemPool
121+
->getItem($schemaHash)
122+
->isHit();
123+
}
124+
125+
/**
126+
* {@inheritdoc}
127+
*/
128+
public function hasSchemaForSubjectAndVersion(string $subject, int $version): bool
129+
{
130+
return $this->cacheItemPool
131+
->getItem(
132+
$this->makeKeyFromSubjectAndVersion($subject, $version)
133+
)
134+
->isHit();
135+
}
136+
137+
/**
138+
* {@inheritdoc}
139+
*/
140+
private function makeKeyFromSubjectAndVersion(string $subject, int $version): string
141+
{
142+
return sprintf('%s_%d', $subject, $version);
143+
}
144+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Registry\Cache;
6+
7+
use AvroSchema;
8+
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter;
9+
use Psr\SimpleCache\CacheInterface;
10+
11+
class SimpleCacheAdapter implements CacheAdapter
12+
{
13+
/**
14+
* @var CacheInterface $cache
15+
*/
16+
private $cache;
17+
18+
public function __construct(CacheInterface $cache)
19+
{
20+
$this->cache = $cache;
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function cacheSchemaWithId(AvroSchema $schema, int $schemaId): void
27+
{
28+
$this->cache->set((string) $schemaId, (string) $schema);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function cacheSchemaIdByHash(int $schemaId, string $schemaHash): void
35+
{
36+
$this->cache->set($schemaHash, $schemaId);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function cacheSchemaWithSubjectAndVersion(AvroSchema $schema, string $subject, int $version): void
43+
{
44+
$this->cache->set(
45+
$this->makeKeyFromSubjectAndVersion($subject, $version),
46+
(string) $schema
47+
);
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*
53+
* @throws \AvroSchemaParseException
54+
*/
55+
public function getWithId(int $schemaId): ?AvroSchema
56+
{
57+
$rawSchema = $this->cache->get((string) $schemaId);
58+
59+
if (null === $rawSchema) {
60+
return null;
61+
}
62+
63+
return AvroSchema::parse($rawSchema);
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function getIdWithHash(string $hash): ?int
70+
{
71+
72+
return $this->cache->get($hash);
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*
78+
* @throws \AvroSchemaParseException
79+
*/
80+
public function getWithSubjectAndVersion(string $subject, int $version): ?AvroSchema
81+
{
82+
$rawSchema = $this->cache->get(
83+
$this->makeKeyFromSubjectAndVersion($subject, $version)
84+
);
85+
86+
if (null === $rawSchema) {
87+
return null;
88+
}
89+
90+
return AvroSchema::parse($rawSchema);
91+
}
92+
93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function hasSchemaForId(int $schemaId): bool
97+
{
98+
return null !== $this->cache->get((string) $schemaId);
99+
}
100+
101+
/**
102+
* {@inheritdoc}
103+
*/
104+
public function hasSchemaIdForHash(string $schemaHash): bool
105+
{
106+
return null !== $this->cache->get($schemaHash);
107+
}
108+
109+
/**
110+
* {@inheritdoc}
111+
*/
112+
public function hasSchemaForSubjectAndVersion(string $subject, int $version): bool
113+
{
114+
$schema = $this->cache->get(
115+
$this->makeKeyFromSubjectAndVersion($subject, $version)
116+
);
117+
118+
return null !== $schema;
119+
}
120+
121+
/**
122+
* {@inheritdoc}
123+
*/
124+
private function makeKeyFromSubjectAndVersion(string $subject, int $version): string
125+
{
126+
return sprintf('%s_%d', $subject, $version);
127+
}
128+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Test\Registry\Cache;
6+
7+
use FlixTech\SchemaRegistryApi\Registry\Cache\CacheItemPoolAdapter;
8+
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter;
9+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
10+
11+
class CacheItemPoolAdapterTest extends AbstractCacheAdapterTestCase
12+
{
13+
protected function getAdapter(): CacheAdapter
14+
{
15+
return new CacheItemPoolAdapter(new ArrayAdapter());
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Test\Registry\Cache;
6+
7+
use FlixTech\SchemaRegistryApi\Registry\Cache\SimpleCacheAdapter;
8+
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter;
9+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
10+
use Symfony\Component\Cache\Psr16Cache;
11+
12+
class SimpleCacheAdapterTest extends AbstractCacheAdapterTestCase
13+
{
14+
protected function getAdapter(): CacheAdapter
15+
{
16+
return new SimpleCacheAdapter(new Psr16Cache(new ArrayAdapter()));
17+
}
18+
}

0 commit comments

Comments
 (0)