11package restx .i18n ;
22
33import com .google .common .base .Optional ;
4+ import com .google .common .cache .CacheBuilder ;
5+ import com .google .common .cache .CacheLoader ;
6+ import com .google .common .cache .LoadingCache ;
47import com .google .common .collect .Ordering ;
58import com .google .common .io .Resources ;
69import com .samskivert .mustache .Mustache ;
3134public class DefaultMessages extends AbstractMessages implements Messages {
3235 private final String baseName ;
3336 private final Charset charset ;
37+ private final LoadingCache <Locale , Iterable <String >> cachedKeysByLocale = CacheBuilder .newBuilder ().build (new CacheLoader <Locale , Iterable <String >>() {
38+ @ Override
39+ public Iterable <String > load (Locale locale ) throws Exception {
40+ Optional <ResourceBundle > bundle = getBundle (locale );
41+ return bundle .isPresent () ? Ordering .natural ().sortedCopy (bundle .get ().keySet ()) : Collections .<String >emptySet ();
42+ }
43+ });
3444
3545 public DefaultMessages (String baseName ) {
3646 this (baseName , StandardCharsets .UTF_8 );
@@ -56,8 +66,11 @@ public String getMessage(String key, MessageParams params, Locale locale) {
5666
5767 @ Override
5868 public Iterable <String > keys (Locale locale ) {
59- Optional <ResourceBundle > bundle = getBundle (locale );
60- return bundle .isPresent () ? Ordering .natural ().sortedCopy (bundle .get ().keySet ()) : Collections .<String >emptySet ();
69+ return cachedKeysByLocale .getUnchecked (locale );
70+ }
71+
72+ protected void invalidateCachedKeysFor (Locale locale ) {
73+ cachedKeysByLocale .invalidate (locale );
6174 }
6275
6376 @ Override
0 commit comments