When to use Hibernate caching (second level)?
This is a basic question about Hibernate Caching
, but I've to be sure before going forward. I had use query
caching before in small projects, but now I'm开发者_如何学编程 involved in a big project, so this is:
In really big projects (national) what are your suggestion about when to use Query
Caching in Hibernate?
note: *The platform is Struts2, Spring3, Hibernate, Java6 WAS6 *
2nd level cache is used, when your db relations r complex as in that case you know hitting db each and every time will be a costly operation. Performance of app can be increased by using cache in such cases.
I reckon you mean second-level cache, that is cache which spans more than one Hibernate session.
Generally, query cache is used for queries that are heavy or often accessed, to make your app hit the database less often.
I'm not sure if your question includes entity cache, but you definitely should investigate it as well. This cache includes individual entities or their collections regardless of context (i.e. concrete queries). I would say it's the most beneficial type of caching.
The bigger your TPS or number of entities, the more you will benefit from using such cache. When you run into having a few thousand queries per transaction, fetching entities from cache (usually in RAM) rather than querying database and mapping can save a lot of precious time.
Be careful when you need 100% up-to-date (online) results.
See also:
Improving Performance at Hibernate docs.
I highly recommend the article truly understanding the second level and query caches. In general, caching has a lot of benefits but also introduces a lot of complexity, and you should have a good reason for caching, and understand what benefits/risks it will give you.
Note that turning on the query cache is by itself not enough, you need to mark things as cacheable, here is an explanation. This whole article is really good and discusses when the query cache is not helpful. Again, make sure you have a good reason for turning on query caching in your application.
精彩评论