开发者

caching spring/hibernate webapp

I have a website that allows searches for lists of content in various ways, for instance "show stuff created by user 523 ordered by date" or "show a list of the most recent 10 posts."

I use Hibernate for my ORM, and Hibernate provides a cache for objects. For lists of objects, though, like the front page list of most recent content, I'm at a loss as how best to cache that content. Right now, I have my Spring controllers just return a standard JSP page, and then I use oscache at the JSP le开发者_JAVA技巧vel wrapped around a call to another class.

This seems inelegant, though. What I really want is for my controller to have access to a cached result if one's available so that the JSP can just be concerned with displaying results.

What are my options here?


Do you mean you want to cache the results of hibernate queries, in addition to the entities? If so, then you need to look at query caching.


For this caching, Hibernate splits the problem in two levels:

  1. The list of ids that correspond to the query : this result is subject to change in many ways. For example, if any of the tables used by the query is changed, the result could be changed. Whether or not it is effectively changed would be difficult to compute and maintain up to date, so it is inefficient in most cases. So Hibernate chooses not to cache the ids that correspond to a query.
  2. Once a list of ids corresponding to a query is known, there is also the need for all data corresponding to the entities returned, plus all entities (or components) that are fetched. Here we suppose that you decided that these entities are constant-enough and read enough that caching them is advisable. This data retrieval benefits fully from the regular second-level Hibernate cache.

For caching efficiency and code simplicity, I split such results I want to cache in two levels :

  • a query returning only the ids (plus possibly entities fetched that were not chosen for the second-level Hibernate cache)
  • other data fetched by id, to benefit from the second-level cache
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜