From d49492886fd04f1c690a78fba1b3dbf6519bdd03 Mon Sep 17 00:00:00 2001 From: Mike Skells Date: Mon, 3 Feb 2025 09:58:02 +0000 Subject: [PATCH] msg-simple/issues/9 cache results of lookup --- .../github/fge/msgsimple/bundle/MessageBundle.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/fge/msgsimple/bundle/MessageBundle.java b/src/main/java/com/github/fge/msgsimple/bundle/MessageBundle.java index 9e9611d..1c4a0ee 100644 --- a/src/main/java/com/github/fge/msgsimple/bundle/MessageBundle.java +++ b/src/main/java/com/github/fge/msgsimple/bundle/MessageBundle.java @@ -33,6 +33,8 @@ import java.util.IllegalFormatException; import java.util.List; import java.util.Locale; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; /** * A message bundle @@ -67,6 +69,9 @@ public final class MessageBundle final List providers = new ArrayList(); + private final ConcurrentMap> cache + = new ConcurrentHashMap<>(); + /** * Create a new, empty builder for a bundle @@ -109,10 +114,14 @@ public static MessageBundle withSingleSource(final MessageSource source) * found * @throws NullPointerException either the key or the locale is null */ - public String getMessage(final Locale locale, final String key) - { + public String getMessage(final Locale locale, final String key) { BUNDLE.checkNotNull(key, "query.nullKey"); BUNDLE.checkNotNull(locale, "query.nullLocale"); + var localCache = cache.computeIfAbsent(locale, k -> new ConcurrentHashMap<>()); + return localCache.computeIfAbsent(key, k -> getMessageImpl(locale, k)); + } + private String getMessageImpl(final Locale locale, final String key) + { String ret; MessageSource source;