views:

262

answers:

1

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.

+3  A: 

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.

Michael Myers
Ack, don't know how I missed that. Thanks, mm.
Marcus