-
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.
[FEATURE][BC-BREAK] Add possibility to cache schema IDs by hash (#11)
This is needed because when running serializers the cost of hashing the schemas' string representations is way less than going to the registry over HTTP. This adds 3 new methods to the `CacheAdapter` API: `cacheSchemaIdByHash(int $schemaId, string $schemaHash)` `getIdWithHash(string $hash)` `hasSchemaIdForHash(string $schemaHash)` Unfortunately this is a BC breaking change which requires clients that possibly have own implementations of the `CacheAdapterInterface` to implement those 3 methods. Clients that use the default shipped adapters are OK. Implements the new caching features in the `CachedRegistry`
- Loading branch information
Showing
8 changed files
with
312 additions
and
172 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
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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FlixTech\SchemaRegistryApi\Test\Registry\Cache; | ||
|
||
use AvroSchema; | ||
use FlixTech\SchemaRegistryApi\Registry\CacheAdapter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
abstract class AbstractCacheAdapterTestCase extends TestCase | ||
{ | ||
abstract protected function getAdapter(): CacheAdapter; | ||
|
||
/** | ||
* @var CacheAdapter | ||
*/ | ||
protected $cacheAdapter; | ||
|
||
protected function setUp() | ||
{ | ||
$this->cacheAdapter = $this->getAdapter(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_should_store_and_fetch_schemas_with_ids() | ||
{ | ||
$schemaId = 3; | ||
$invalidSchemaId = 2; | ||
$schema = AvroSchema::parse('{"type": "string"}'); | ||
|
||
$this->cacheAdapter->cacheSchemaWithId($schema, $schemaId); | ||
|
||
$this->assertFalse($this->cacheAdapter->hasSchemaForId($invalidSchemaId)); | ||
$this->assertTrue($this->cacheAdapter->hasSchemaForId($schemaId)); | ||
|
||
$this->assertNull($this->cacheAdapter->getWithId($invalidSchemaId)); | ||
$this->assertEquals($schema, $this->cacheAdapter->getWithId($schemaId)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_should_store_and_fetch_schemas_with_subject_and_version() | ||
{ | ||
$subject = 'test'; | ||
$version = 2; | ||
$invalidSubject = 'none'; | ||
$schema = AvroSchema::parse('{"type": "string"}'); | ||
|
||
$this->cacheAdapter->cacheSchemaWithSubjectAndVersion($schema, $subject, $version); | ||
|
||
$this->assertFalse($this->cacheAdapter->hasSchemaForSubjectAndVersion($invalidSubject, $version)); | ||
$this->assertTrue($this->cacheAdapter->hasSchemaForSubjectAndVersion($subject, $version)); | ||
|
||
$this->assertNull($this->cacheAdapter->getWithSubjectAndVersion($invalidSubject, $version)); | ||
$this->assertEquals($schema, $this->cacheAdapter->getWithSubjectAndVersion($subject, $version)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_should_store_and_fetch_schema_ids_with_schema_hashes() | ||
{ | ||
$schemaId = 3; | ||
$hash = 'hash'; | ||
$anotherHash = 'another'; | ||
|
||
$this->assertFalse($this->cacheAdapter->hasSchemaIdForHash($hash)); | ||
$this->assertFalse($this->cacheAdapter->hasSchemaIdForHash($anotherHash)); | ||
|
||
$this->cacheAdapter->cacheSchemaIdByHash($schemaId, $hash); | ||
|
||
$this->assertTrue($this->cacheAdapter->hasSchemaIdForHash($hash)); | ||
$this->assertFalse($this->cacheAdapter->hasSchemaIdForHash($anotherHash)); | ||
|
||
$this->assertNull($this->cacheAdapter->getIdWithHash($anotherHash)); | ||
$this->assertSame($schemaId, $this->cacheAdapter->getIdWithHash($hash)); | ||
} | ||
} |
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
Oops, something went wrong.