Java ResourceBundle 性能
我正在使用 ResourceBundle 和 Locale 来查找属性值。很简单,代码如下所示:
public static String getPropertyValue(Locale locale, String resourceName, String key) {
ResourceBundle resource = ResourceBundle.getBundle(resourceName, locale);
return resource.getString(key);
}
我的问题是关于性能的。与访问类路径上的属性文件相比,缓存方法是否会更快或更好的实现?我的理解是,ResourceBundle 的性能总体来说非常好。
属性文件(在本例中)少于 30 行(即约 30 个键/值对)。
我对性能提出质疑,因为我们可以在高负载页面上使用类似的方法,而按需查找方法可能成本高昂。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Javadocs:
所以你不需要自己做缓存。但是,如果您需要对缓存行为进行更细粒度的控制,可以使用
getBundle(String, ResourceBundle.Control)
重载并传递自定义的Control
中。According to the Javadocs:
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 customizedControl
in.