@@ -69,14 +69,14 @@ public class CachedSchemaRegistryClient : ISchemaRegistryClient
69
69
private int latestCacheTtlSecs ;
70
70
private readonly ConcurrentDictionary < int , Schema > schemaById = new ConcurrentDictionary < int , Schema > ( ) ;
71
71
72
- private readonly Dictionary < string /*subject*/ , Dictionary < Schema , int > > idBySchemaBySubject =
73
- new Dictionary < string , Dictionary < Schema , int > > ( ) ;
72
+ private readonly ConcurrentDictionary < string /*subject*/ , ConcurrentDictionary < Schema , int > > idBySchemaBySubject =
73
+ new ConcurrentDictionary < string , ConcurrentDictionary < Schema , int > > ( ) ;
74
74
75
- private readonly Dictionary < string /*subject*/ , Dictionary < int , RegisteredSchema > > schemaByVersionBySubject =
76
- new Dictionary < string , Dictionary < int , RegisteredSchema > > ( ) ;
75
+ private readonly ConcurrentDictionary < string /*subject*/ , ConcurrentDictionary < int , RegisteredSchema > > schemaByVersionBySubject =
76
+ new ConcurrentDictionary < string , ConcurrentDictionary < int , RegisteredSchema > > ( ) ;
77
77
78
- private readonly Dictionary < string /*subject*/ , Dictionary < Schema , RegisteredSchema > > registeredSchemaBySchemaBySubject =
79
- new Dictionary < string , Dictionary < Schema , RegisteredSchema > > ( ) ;
78
+ private readonly ConcurrentDictionary < string /*subject*/ , ConcurrentDictionary < Schema , RegisteredSchema > > registeredSchemaBySchemaBySubject =
79
+ new ConcurrentDictionary < string , ConcurrentDictionary < Schema , RegisteredSchema > > ( ) ;
80
80
81
81
private readonly MemoryCache latestVersionBySubject = new MemoryCache ( new MemoryCacheOptions ( ) ) ;
82
82
@@ -603,13 +603,21 @@ public Task<int> GetSchemaIdAsync(string subject, string avroSchema, bool normal
603
603
/// <inheritdoc/>
604
604
public async Task < int > GetSchemaIdAsync ( string subject , Schema schema , bool normalize = false )
605
605
{
606
+ if ( idBySchemaBySubject . TryGetValue ( subject , out var idBySchema ) )
607
+ {
608
+ if ( idBySchema . TryGetValue ( schema , out int schemaId ) )
609
+ {
610
+ return schemaId ;
611
+ }
612
+ }
613
+
606
614
await cacheMutex . WaitAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
607
615
try
608
616
{
609
- if ( ! this . idBySchemaBySubject . TryGetValue ( subject , out Dictionary < Schema , int > idBySchema ) )
617
+ if ( ! this . idBySchemaBySubject . TryGetValue ( subject , out idBySchema ) )
610
618
{
611
- idBySchema = new Dictionary < Schema , int > ( ) ;
612
- this . idBySchemaBySubject . Add ( subject , idBySchema ) ;
619
+ idBySchema = new ConcurrentDictionary < Schema , int > ( ) ;
620
+ this . idBySchemaBySubject . TryAdd ( subject , idBySchema ) ;
613
621
}
614
622
615
623
// TODO: The following could be optimized in the usual case where idBySchema only
@@ -640,13 +648,21 @@ public async Task<int> GetSchemaIdAsync(string subject, Schema schema, bool norm
640
648
/// <inheritdoc/>
641
649
public async Task < int > RegisterSchemaAsync ( string subject , Schema schema , bool normalize = false )
642
650
{
651
+ if ( idBySchemaBySubject . TryGetValue ( subject , out var idBySchema ) )
652
+ {
653
+ if ( idBySchema . TryGetValue ( schema , out var schemaId ) )
654
+ {
655
+ return schemaId ;
656
+ }
657
+ }
658
+
643
659
await cacheMutex . WaitAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
644
660
try
645
661
{
646
- if ( ! this . idBySchemaBySubject . TryGetValue ( subject , out Dictionary < Schema , int > idBySchema ) )
662
+ if ( ! this . idBySchemaBySubject . TryGetValue ( subject , out idBySchema ) )
647
663
{
648
- idBySchema = new Dictionary < Schema , int > ( ) ;
649
- this . idBySchemaBySubject [ subject ] = idBySchema ;
664
+ idBySchema = new ConcurrentDictionary < Schema , int > ( ) ;
665
+ idBySchemaBySubject . TryAdd ( subject , idBySchema ) ;
650
666
}
651
667
652
668
// TODO: This could be optimized in the usual case where idBySchema only
@@ -703,13 +719,21 @@ private bool checkSchemaMatchesFormat(string format, string schemaString)
703
719
public async Task < RegisteredSchema > LookupSchemaAsync ( string subject , Schema schema , bool ignoreDeletedSchemas ,
704
720
bool normalize = false )
705
721
{
722
+ if ( registeredSchemaBySchemaBySubject . TryGetValue ( subject , out var registeredSchemaBySchema ) )
723
+ {
724
+ if ( registeredSchemaBySchema . TryGetValue ( schema , out var registeredSchema ) )
725
+ {
726
+ return registeredSchema ;
727
+ }
728
+ }
729
+
706
730
await cacheMutex . WaitAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
707
731
try
708
732
{
709
- if ( ! registeredSchemaBySchemaBySubject . TryGetValue ( subject , out var registeredSchemaBySchema ) )
733
+ if ( ! registeredSchemaBySchemaBySubject . TryGetValue ( subject , out registeredSchemaBySchema ) )
710
734
{
711
735
CleanCacheIfFull ( ) ;
712
- registeredSchemaBySchema = new Dictionary < Schema , RegisteredSchema > ( ) ;
736
+ registeredSchemaBySchema = new ConcurrentDictionary < Schema , RegisteredSchema > ( ) ;
713
737
registeredSchemaBySchemaBySubject [ subject ] = registeredSchemaBySchema ;
714
738
}
715
739
if ( ! registeredSchemaBySchema . TryGetValue ( schema , out var registeredSchema ) )
@@ -729,10 +753,15 @@ public async Task<RegisteredSchema> LookupSchemaAsync(string subject, Schema sch
729
753
/// <inheritdoc/>
730
754
public async Task < Schema > GetSchemaAsync ( int id , string format = null )
731
755
{
756
+ if ( schemaById . TryGetValue ( id , out var schema ) && checkSchemaMatchesFormat ( format , schema . SchemaString ) )
757
+ {
758
+ return schema ;
759
+ }
760
+
732
761
await cacheMutex . WaitAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
733
762
try
734
763
{
735
- if ( ! this . schemaById . TryGetValue ( id , out Schema schema ) ||
764
+ if ( ! this . schemaById . TryGetValue ( id , out schema ) ||
736
765
! checkSchemaMatchesFormat ( format , schema . SchemaString ) )
737
766
{
738
767
CleanCacheIfFull ( ) ;
@@ -753,7 +782,7 @@ public async Task<Schema> GetSchemaAsync(int id, string format = null)
753
782
/// <inheritdoc/>
754
783
public async Task < Schema > GetSchemaBySubjectAndIdAsync ( string subject , int id , string format = null )
755
784
{
756
- if ( this . schemaById . TryGetValue ( id , out Schema schema ) && checkSchemaMatchesFormat ( format , schema . SchemaString ) )
785
+ if ( this . schemaById . TryGetValue ( id , out var schema ) && checkSchemaMatchesFormat ( format , schema . SchemaString ) )
757
786
{
758
787
return schema ;
759
788
}
@@ -781,19 +810,24 @@ public async Task<Schema> GetSchemaBySubjectAndIdAsync(string subject, int id, s
781
810
/// <inheritdoc/>
782
811
public async Task < RegisteredSchema > GetRegisteredSchemaAsync ( string subject , int version , bool ignoreDeletedSchemas = true )
783
812
{
813
+ if ( schemaByVersionBySubject . TryGetValue ( subject , out var schemaByVersion ) &&
814
+ schemaByVersion . TryGetValue ( version , out var schema ) )
815
+ {
816
+ return schema ;
817
+ }
818
+
784
819
await cacheMutex . WaitAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
785
820
try
786
821
{
787
822
CleanCacheIfFull ( ) ;
788
823
789
- if ( ! schemaByVersionBySubject . TryGetValue ( subject ,
790
- out Dictionary < int , RegisteredSchema > schemaByVersion ) )
824
+ if ( ! schemaByVersionBySubject . TryGetValue ( subject , out schemaByVersion ) )
791
825
{
792
- schemaByVersion = new Dictionary < int , RegisteredSchema > ( ) ;
826
+ schemaByVersion = new ConcurrentDictionary < int , RegisteredSchema > ( ) ;
793
827
schemaByVersionBySubject [ subject ] = schemaByVersion ;
794
828
}
795
829
796
- if ( ! schemaByVersion . TryGetValue ( version , out RegisteredSchema schema ) )
830
+ if ( ! schemaByVersion . TryGetValue ( version , out schema ) )
797
831
{
798
832
schema = await restService . GetSchemaAsync ( subject , version )
799
833
. ConfigureAwait ( continueOnCapturedContext : false ) ;
0 commit comments