diff --git a/README.md b/README.md index d4769a3..73702c8 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ compliant requests that can be used as well as high level abstractions to ease d | Dependency | Version | Reason | |:--- |:---:|:--- | | **`doctrine/cache`** | ~1.3 | If you want to use the `DoctrineCacheAdapter` | +| **`psr/cache`** | ^1.0 | If you want to use the `CacheItemPoolAdapter` | +| **`psr/simple-cache`** | ^1.0 | If you want to use the `SimpleCacheAdapter` | | **`raphhh/trex-reflection`** | ~1.0 | If you want to use the `RequestCallbackValidator`s | ## Installation diff --git a/composer.json b/composer.json index 7ddf2f6..94219ea 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "ext-curl": "*", "ext-json": "*", "guzzlehttp/guzzle": "~6.3", + "guzzlehttp/psr7": "<1.7", "beberlei/assert": "~2.7|~3.0", "flix-tech/avro-php": "^3.0|^4.0" }, @@ -22,11 +23,16 @@ "phpunit/phpunit": "~7.0", "phpstan/phpstan": "^0.12", "raphhh/trex-reflection": "~1.0", - "doctrine/cache": "~1.3" + "doctrine/cache": "~1.3", + "psr/cache": "^1.0", + "symfony/cache": "^5.1", + "psr/simple-cache": "^1.0" }, "suggest": { "raphhh/trex-reflection": "Needed if you want to use the `RequestCallbackValidator`", - "doctrine/cache": "If you want to use the DoctrineCacheAdapter" + "doctrine/cache": "If you want to use the DoctrineCacheAdapter", + "psr/cache": "If you want to use the CacheItemPoolAdapter", + "psr/simple-cache": "If you want to use the SimpleCacheAdapter" }, "autoload": { "psr-4": { diff --git a/src/Registry/Cache/CacheItemPoolAdapter.php b/src/Registry/Cache/CacheItemPoolAdapter.php new file mode 100644 index 0000000..6c8a350 --- /dev/null +++ b/src/Registry/Cache/CacheItemPoolAdapter.php @@ -0,0 +1,144 @@ +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); + } +} diff --git a/src/Registry/Cache/SimpleCacheAdapter.php b/src/Registry/Cache/SimpleCacheAdapter.php new file mode 100644 index 0000000..64f9150 --- /dev/null +++ b/src/Registry/Cache/SimpleCacheAdapter.php @@ -0,0 +1,128 @@ +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); + } +} diff --git a/test/Registry/Cache/CacheItemPoolAdapterTest.php b/test/Registry/Cache/CacheItemPoolAdapterTest.php new file mode 100644 index 0000000..642c198 --- /dev/null +++ b/test/Registry/Cache/CacheItemPoolAdapterTest.php @@ -0,0 +1,17 @@ +