I am using a ResourceBundle and Locale to lookup property values. Quite simply, the code looks like this:
public static String getPropertyValue(Locale locale, String resourceName, String key) { ResourceBundle resource = ResourceBundle.getBundle(resourceName, locale); return resource.getString(key); }
My question is about performance. Would a caching approach be quicker or a better implementation than accessing property files on the classpath? My understanding is that ResourceBundle performance is very good in general.
The properties file (in this case) is fewer than 30 lines (i.e., ~30 key/value pairs).
I question the performance since we could use a similar approach on high-load pages, and the lookup-on-demand approach might prove costly.
ResourceBundle class is used to store text and objects which are locale sensitive. Generally we use property files to store locale specific text and then represent them using ResourceBundle object.
Spring's application context is able to resolve text messages for a target locale by their keys. Typically, the messages for one locale should be stored in one separate properties file. This properties file is called a resource bundle. MessageSource is an interface that defines several methods for resolving messages.
getBundle(String baseName) method gets a resource bundle using the specified base name, the default locale, and the caller's class loader.
According to the Javadocs:
Resource bundle instances created by the
getBundle
factory methods are cached by default, and the factory methods return the same resource bundle instance multiple times if it has been cached.
So you shouldn't need to do caching on your own. But if you need finer-grained control of the caching behavior, you can use the getBundle(String, ResourceBundle.Control)
overload and pass a customized Control
in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With