Class: ProtoTurf::CachedConfluentSchemaRegistry
- Inherits:
-
Object
- Object
- ProtoTurf::CachedConfluentSchemaRegistry
- Defined in:
- lib/proto_turf/cached_confluent_schema_registry.rb
Instance Method Summary collapse
-
#fetch(id) ⇒ String
The schema string stored in the registry for the given id.
-
#fetch_version(id, subject) ⇒ Integer?
The version of the schema for the given subject and id, or nil if not found.
-
#initialize(upstream) ⇒ CachedConfluentSchemaRegistry
constructor
A new instance of CachedConfluentSchemaRegistry.
- #register(subject, schema, references: []) ⇒ Object
-
#registered?(subject, schema) ⇒ Boolean
True if we know the schema has been registered for that subject.
Constructor Details
#initialize(upstream) ⇒ CachedConfluentSchemaRegistry
Returns a new instance of CachedConfluentSchemaRegistry.
4 5 6 7 8 9 |
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 4 def initialize(upstream) @upstream = upstream @schemas_by_id = {} @ids_by_schema = {} @versions_by_subject_and_id = {} end |
Instance Method Details
#fetch(id) ⇒ String
Returns the schema string stored in the registry for the given id.
20 21 22 |
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 20 def fetch(id) @schemas_by_id[id] ||= @upstream.fetch(id) end |
#fetch_version(id, subject) ⇒ Integer?
Returns the version of the schema for the given subject and id, or nil if not found.
27 28 29 30 31 32 33 |
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 27 def fetch_version(id, subject) key = [subject, id] return @versions_by_subject_and_id[key] if @versions_by_subject_and_id[key] results = @upstream.schema_subject_versions(id) @versions_by_subject_and_id[key] = results&.find { |r| r['subject'] == subject }&.dig('version') end |
#register(subject, schema, references: []) ⇒ Object
45 46 47 48 49 |
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 45 def register(subject, schema, references: []) key = [subject, schema] @ids_by_schema[key] ||= @upstream.register(subject, schema, references: references) end |
#registered?(subject, schema) ⇒ Boolean
Returns true if we know the schema has been registered for that subject.
38 39 40 |
# File 'lib/proto_turf/cached_confluent_schema_registry.rb', line 38 def registered?(subject, schema) @ids_by_schema[[subject, schema]].present? end |