1
1
package restx .i18n ;
2
2
3
3
import 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 ;
4
7
import com .google .common .collect .Ordering ;
5
8
import com .google .common .io .Resources ;
6
9
import com .samskivert .mustache .Mustache ;
31
34
public class DefaultMessages extends AbstractMessages implements Messages {
32
35
private final String baseName ;
33
36
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
+ });
34
44
35
45
public DefaultMessages (String baseName ) {
36
46
this (baseName , StandardCharsets .UTF_8 );
@@ -56,8 +66,11 @@ public String getMessage(String key, MessageParams params, Locale locale) {
56
66
57
67
@ Override
58
68
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 );
61
74
}
62
75
63
76
@ Override
0 commit comments