Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top