Caching a resultset
My app will retrieve a countylist from MySql using a datasource bean. Since all pages will potentially use the same recordset every time I could store 开发者_高级运维the countrylist as a List in some global bean, safe in this case. I could manage to refresh the list any time I want... but when things become more complex what is the best strategy for it, the more scalable solution?
Use a in memory database? A 3rd part cached resultset?
didnt found one, probably because I'm to new to the subject.
ResultSet
is tied to the database connection, it's a fairly ephemeral construct, and not suitable for caching.
I recommend using EhCache to cache your results. It's an in-memory cache mechanism (with options to overflow to disk, and options to distribute across a cluster). It integrates very nicely with Spring (via EhCacheManagerFactoryBean
and EhCacheFactoryBean
).
If the country list is updated almost never, fill a FastMap on application startup with the map keys being the two letter country abbreviations and the values being other info you want to store for each country. Otherwise, use an in-memory DB cache such as EhCache and Memcached.
Use CachedRowSet. It can be treated like a ResultSet, but it doesn't need to maintain a connection to the database.
精彩评论