-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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!
- Loading branch information
Showing
6 changed files
with
317 additions
and
2 deletions.
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
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,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FlixTech\SchemaRegistryApi\Registry\Cache; | ||
|
||
use AvroSchema; | ||
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
|
||
class CacheItemPoolAdapter implements CacheAdapter | ||
{ | ||
/** | ||
* @var CacheItemPoolInterface | ||
*/ | ||
private $cacheItemPool; | ||
|
||
public function __construct(CacheItemPoolInterface $cacheItemPool) | ||
{ | ||
$this->cacheItemPool = $cacheItemPool; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function cacheSchemaWithId(AvroSchema $schema, int $schemaId): void | ||
{ | ||
$item = $this->cacheItemPool->getItem((string) $schemaId); | ||
$item->set((string) $schema); | ||
$this->cacheItemPool->save($item); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function cacheSchemaIdByHash(int $schemaId, string $schemaHash): void | ||
{ | ||
$item = $this->cacheItemPool->getItem($schemaHash); | ||
$item->set($schemaId); | ||
$this->cacheItemPool->save($item); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function cacheSchemaWithSubjectAndVersion(AvroSchema $schema, string $subject, int $version): void | ||
{ | ||
$item = $this->cacheItemPool->getItem($this->makeKeyFromSubjectAndVersion($subject, $version)); | ||
$item->set((string) $schema); | ||
$this->cacheItemPool->save($item); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws \AvroSchemaParseException | ||
*/ | ||
public function getWithId(int $schemaId): ?AvroSchema | ||
{ | ||
$item = $this->cacheItemPool->getItem((string) $schemaId); | ||
|
||
if (!$item->isHit()) { | ||
return null; | ||
} | ||
|
||
$rawSchema = $item->get(); | ||
|
||
return AvroSchema::parse($rawSchema); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIdWithHash(string $hash): ?int | ||
{ | ||
$item = $this->cacheItemPool->getItem($hash); | ||
|
||
if (!$item->isHit()) { | ||
return null; | ||
} | ||
|
||
return $item->get(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws \AvroSchemaParseException | ||
*/ | ||
public function getWithSubjectAndVersion(string $subject, int $version): ?AvroSchema | ||
{ | ||
$item = $this->cacheItemPool->getItem( | ||
$this->makeKeyFromSubjectAndVersion($subject, $version) | ||
); | ||
|
||
if (!$item->isHit()) { | ||
return null; | ||
} | ||
|
||
$rawSchema = $item->get(); | ||
|
||
return AvroSchema::parse($rawSchema); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasSchemaForId(int $schemaId): bool | ||
{ | ||
return $this->cacheItemPool | ||
->getItem((string) $schemaId) | ||
->isHit(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasSchemaIdForHash(string $schemaHash): bool | ||
{ | ||
return $this->cacheItemPool | ||
->getItem($schemaHash) | ||
->isHit(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasSchemaForSubjectAndVersion(string $subject, int $version): bool | ||
{ | ||
return $this->cacheItemPool | ||
->getItem( | ||
$this->makeKeyFromSubjectAndVersion($subject, $version) | ||
) | ||
->isHit(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
private function makeKeyFromSubjectAndVersion(string $subject, int $version): string | ||
{ | ||
return sprintf('%s_%d', $subject, $version); | ||
} | ||
} |
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,128 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FlixTech\SchemaRegistryApi\Registry\Cache; | ||
|
||
use AvroSchema; | ||
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
class SimpleCacheAdapter implements CacheAdapter | ||
{ | ||
/** | ||
* @var CacheInterface $cache | ||
*/ | ||
private $cache; | ||
|
||
public function __construct(CacheInterface $cache) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function cacheSchemaWithId(AvroSchema $schema, int $schemaId): void | ||
{ | ||
$this->cache->set((string) $schemaId, (string) $schema); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function cacheSchemaIdByHash(int $schemaId, string $schemaHash): void | ||
{ | ||
$this->cache->set($schemaHash, $schemaId); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function cacheSchemaWithSubjectAndVersion(AvroSchema $schema, string $subject, int $version): void | ||
{ | ||
$this->cache->set( | ||
$this->makeKeyFromSubjectAndVersion($subject, $version), | ||
(string) $schema | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws \AvroSchemaParseException | ||
*/ | ||
public function getWithId(int $schemaId): ?AvroSchema | ||
{ | ||
$rawSchema = $this->cache->get((string) $schemaId); | ||
|
||
if (null === $rawSchema) { | ||
return null; | ||
} | ||
|
||
return AvroSchema::parse($rawSchema); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIdWithHash(string $hash): ?int | ||
{ | ||
|
||
return $this->cache->get($hash); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws \AvroSchemaParseException | ||
*/ | ||
public function getWithSubjectAndVersion(string $subject, int $version): ?AvroSchema | ||
{ | ||
$rawSchema = $this->cache->get( | ||
$this->makeKeyFromSubjectAndVersion($subject, $version) | ||
); | ||
|
||
if (null === $rawSchema) { | ||
return null; | ||
} | ||
|
||
return AvroSchema::parse($rawSchema); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasSchemaForId(int $schemaId): bool | ||
{ | ||
return null !== $this->cache->get((string) $schemaId); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasSchemaIdForHash(string $schemaHash): bool | ||
{ | ||
return null !== $this->cache->get($schemaHash); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasSchemaForSubjectAndVersion(string $subject, int $version): bool | ||
{ | ||
$schema = $this->cache->get( | ||
$this->makeKeyFromSubjectAndVersion($subject, $version) | ||
); | ||
|
||
return null !== $schema; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
private function makeKeyFromSubjectAndVersion(string $subject, int $version): string | ||
{ | ||
return sprintf('%s_%d', $subject, $version); | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FlixTech\SchemaRegistryApi\Test\Registry\Cache; | ||
|
||
use FlixTech\SchemaRegistryApi\Registry\Cache\CacheItemPoolAdapter; | ||
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
|
||
class CacheItemPoolAdapterTest extends AbstractCacheAdapterTestCase | ||
{ | ||
protected function getAdapter(): CacheAdapter | ||
{ | ||
return new CacheItemPoolAdapter(new ArrayAdapter()); | ||
} | ||
} |
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); | ||
|
||
namespace FlixTech\SchemaRegistryApi\Test\Registry\Cache; | ||
|
||
use FlixTech\SchemaRegistryApi\Registry\Cache\SimpleCacheAdapter; | ||
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Cache\Psr16Cache; | ||
|
||
class SimpleCacheAdapterTest extends AbstractCacheAdapterTestCase | ||
{ | ||
protected function getAdapter(): CacheAdapter | ||
{ | ||
return new SimpleCacheAdapter(new Psr16Cache(new ArrayAdapter())); | ||
} | ||
} |