@@ -66,9 +66,11 @@ public abstract class AbstractKafkaHeaderMapper implements KafkaHeaderMapper {
6666
6767 private final List <HeaderMatcher > matchers = new ArrayList <>();
6868
69+ private final ConcurrentLruCache <String , Boolean > matcherResultCache = new ConcurrentLruCache <>(1000 , this ::doesMatchInternal );
70+
6971 private final List <HeaderMatcher > multiValueHeaderMatchers = new ArrayList <>();
7072
71- private final HeaderPatternMatchCache headerMatchedCache = new HeaderPatternMatchCache ( );
73+ private final ConcurrentLruCache < String , Boolean > multiValueMatcherResultCache = new ConcurrentLruCache <>( 1000 , this :: doesMatchMultiValueHeaderInternal );
7274
7375 private final Map <String , Boolean > rawMappedHeaders = new HashMap <>();
7476
@@ -243,13 +245,17 @@ protected boolean matchesForInbound(String header) {
243245 }
244246
245247 private boolean doesMatch (String header ) {
248+ return this .matcherResultCache .get (header );
249+ }
250+
251+ private boolean doesMatchInternal (String header ) {
246252 for (HeaderMatcher matcher : this .matchers ) {
247253 if (matcher .matchHeader (header )) {
248254 return !matcher .isNegated ();
249255 }
250256 }
251257 this .logger .debug (() -> MessageFormat .format ("headerName=[{0}] WILL NOT be mapped; matched no patterns" ,
252- header ));
258+ header ));
253259 return false ;
254260 }
255261
@@ -269,28 +275,25 @@ protected Object headerValueToAddOut(String key, Object value) {
269275 }
270276
271277 /**
272- * Check whether the header value should be mapped to multiple values.
278+ * Determine whether the given header name should be mapped to multiple values.
279+ * This method first checks if the mapping result is already cached.
280+ * If a cached result exists, it is returned immediately.
281+ * If not, {@code doesMatchInternal(headerName)} is called to compute the result,
282+ * which is then cached and returned.
273283 * @param headerName the header name.
274284 * @return True for multiple values at the same key.
275285 * @since 4.0
276286 */
277287 protected boolean doesMatchMultiValueHeader (String headerName ) {
278- if (this .headerMatchedCache .isMultiValuePattern (headerName )) {
279- return true ;
280- }
281-
282- if (this .headerMatchedCache .isSingleValuePattern (headerName )) {
283- return false ;
284- }
288+ return this .multiValueMatcherResultCache .get (headerName );
289+ }
285290
291+ private boolean doesMatchMultiValueHeaderInternal (String headerName ) {
286292 for (HeaderMatcher headerMatcher : this .multiValueHeaderMatchers ) {
287293 if (headerMatcher .matchHeader (headerName )) {
288- this .headerMatchedCache .cacheAsMultiValueHeader (headerName );
289294 return true ;
290295 }
291296 }
292-
293- this .headerMatchedCache .cacheAsSingleValueHeader (headerName );
294297 return false ;
295298 }
296299
@@ -441,33 +444,4 @@ public boolean isNegated() {
441444
442445 }
443446
444- /**
445- * A Cache that remembers whether a header name matches the multi-value pattern.
446- */
447- private static final class HeaderPatternMatchCache {
448-
449- private static final int MAX_SIZE = 1000 ;
450-
451- private final ConcurrentLruCache <String , Boolean > multiValueHeaderPatternMatchCache = new ConcurrentLruCache <>(MAX_SIZE , key -> Boolean .TRUE );
452-
453- private final ConcurrentLruCache <String , Boolean > singleValueHeaderPatternMatchCache = new ConcurrentLruCache <>(MAX_SIZE , key -> Boolean .TRUE );
454-
455- boolean isMultiValuePattern (String headerName ) {
456- return this .multiValueHeaderPatternMatchCache .contains (headerName );
457- }
458-
459- boolean isSingleValuePattern (String headerName ) {
460- return this .singleValueHeaderPatternMatchCache .contains (headerName );
461- }
462-
463- void cacheAsSingleValueHeader (String headerName ) {
464- this .singleValueHeaderPatternMatchCache .get (headerName );
465- }
466-
467- void cacheAsMultiValueHeader (String headerName ) {
468- this .multiValueHeaderPatternMatchCache .get (headerName );
469- }
470-
471- }
472-
473447}
0 commit comments