|
23 | 23 | using System.Threading.Tasks;
|
24 | 24 | using System.Linq;
|
25 | 25 | using System;
|
| 26 | +using System.Collections.Concurrent; |
26 | 27 | using System.Net;
|
27 | 28 | using System.Threading;
|
28 | 29 | using System.Security.Cryptography.X509Certificates;
|
@@ -65,7 +66,7 @@ public class CachedSchemaRegistryClient : ISchemaRegistryClient
|
65 | 66 | private IRestService restService;
|
66 | 67 | private int identityMapCapacity;
|
67 | 68 | private int latestCacheTtlSecs;
|
68 |
| - private readonly Dictionary<int, Schema> schemaById = new Dictionary<int, Schema>(); |
| 69 | + private readonly ConcurrentDictionary<int, Schema> schemaById = new ConcurrentDictionary<int, Schema>(); |
69 | 70 |
|
70 | 71 | private readonly Dictionary<string /*subject*/, Dictionary<Schema, int>> idBySchemaBySubject =
|
71 | 72 | new Dictionary<string, Dictionary<Schema, int>>();
|
@@ -657,11 +658,15 @@ public async Task<Schema> GetSchemaAsync(int id, string format = null)
|
657 | 658 | /// <inheritdoc/>
|
658 | 659 | public async Task<Schema> GetSchemaBySubjectAndIdAsync(string subject, int id, string format = null)
|
659 | 660 | {
|
| 661 | + if (this.schemaById.TryGetValue(id, out Schema schema) && checkSchemaMatchesFormat(format, schema.SchemaString)) |
| 662 | + { |
| 663 | + return schema; |
| 664 | + } |
| 665 | + |
660 | 666 | await cacheMutex.WaitAsync().ConfigureAwait(continueOnCapturedContext: false);
|
661 | 667 | try
|
662 | 668 | {
|
663 |
| - if (!this.schemaById.TryGetValue(id, out Schema schema) || |
664 |
| - !checkSchemaMatchesFormat(format, schema.SchemaString)) |
| 669 | + if (!this.schemaById.TryGetValue(id, out schema) || !checkSchemaMatchesFormat(format, schema.SchemaString)) |
665 | 670 | {
|
666 | 671 | CleanCacheIfFull();
|
667 | 672 | schema = (await restService.GetSchemaBySubjectAndIdAsync(subject, id, format)
|
|
0 commit comments