11from __future__ import annotations
22
3+ import functools
34import logging
45import sys
56from typing import Any , Dict , NamedTuple
@@ -58,8 +59,6 @@ class SchemaRegistrySerializer:
5859 alternate strategies.
5960 """
6061
61- _cache : Dict [Schema , SchemaVersion ]
62-
6362 def __init__ (
6463 self ,
6564 client : SchemaRegistryClient ,
@@ -71,32 +70,29 @@ def __init__(
7170 self .is_key = is_key
7271 self .compatibility_mode : CompatibilityMode = compatibility_mode
7372 self .schema_naming_strategy = schema_naming_strategy
74- self ._cache = {}
7573
76- def serialize (self , topic , data_and_schema : DataAndSchema ):
74+ def serialize (self , topic : str , data_and_schema : DataAndSchema ):
7775 if data_and_schema is None :
7876 return None
7977 if not isinstance (data_and_schema , DataAndSchema ):
8078 raise TypeError ('AvroSerializer can only serialize' ,
8179 f' { DataAndSchema } , got { type (data_and_schema )} ' )
8280 data , schema = data_and_schema
83- schema_version = self ._cache .get (schema )
84- if not schema_version :
85- schema_name = self .schema_naming_strategy (
86- topic , self .is_key , schema
87- )
88- LOG .info ('Schema %s not cached locally, registering...' ,
89- schema_name )
90- schema_version = self .client .get_or_register_schema_version (
91- definition = schema .string ,
92- schema_name = schema_name ,
93- data_format = schema .data_format ,
94- compatibility_mode = self .compatibility_mode
95- )
96- self ._cache [schema ] = schema_version
81+ schema_version = self ._get_schema_version (topic , schema )
9782 serialized = schema .write (data )
9883 return encode (serialized , schema_version .version_id )
9984
85+ @functools .lru_cache (maxsize = None )
86+ def _get_schema_version (self , topic : str , schema : Schema ) -> SchemaVersion :
87+ schema_name = self .schema_naming_strategy (topic , self .is_key , schema )
88+ LOG .info ('Fetching schema %s...' , schema_name )
89+ return self .client .get_or_register_schema_version (
90+ definition = str (schema ),
91+ schema_name = schema_name ,
92+ data_format = schema .data_format ,
93+ compatibility_mode = self .compatibility_mode
94+ )
95+
10096
10197class SchemaRegistryDeserializer :
10298 """Kafka serializer that uses the AWS Schema Registry.
@@ -146,10 +142,13 @@ def deserialize(self, topic: str, bytes_: bytes):
146142 schema_version = self .client .get_schema_version (
147143 version_id = schema_version_id
148144 )
149- if schema_version .data_format == 'AVRO' :
150- writer_schema = AvroSchema (schema_version .definition )
151- elif schema_version .data_format == 'JSON' :
152- raise NotImplementedError ('JSON schema not supported' )
145+ writer_schema = self ._create_writer_schema (schema_version )
153146 self ._writer_schemas [schema_version_id ] = writer_schema
154- LOG .info ('Schema %s fetched' , schema_version_id )
147+ LOG .info ('Schema version %s fetched' , schema_version_id )
155148 return writer_schema .read (data_bytes )
149+
150+ def _create_writer_schema (self , schema_version : SchemaVersion ) -> Schema :
151+ if schema_version .data_format == 'AVRO' :
152+ return AvroSchema (schema_version .definition )
153+ elif schema_version .data_format == 'JSON' :
154+ raise NotImplementedError ('JSON schema not supported' )
0 commit comments