Hibernate 2nd level cache in a Grails app

时间秒杀一切 提交于 2019-11-29 20:26:58

Part 1:

Hibernate does the right thing. The query cache is not per entity. There is a single query cache region, shared by all queries, unless you set a specific region for a query. Each time a table is updated, its timestamp in the timestamps cache is updated. Each time a query is executed, the timestamp of each of the tables where the query searches is compared to the timestamp of the cached result. And of course, the cached result is returned only if itstimestamp is more recent than all the table timestamps.

Part 2:

Yes, it makes sense. The cache for the author remembers that the author with ID 456 has the name "foo" and the birth date 1975/07/19. Only the data stored in the author table is remembered. So, caching the association is also useful: instead of making an additional query to get the set of books of the author when calling author.getBooks(), Hibernate will get the IDs of the books of the author from its cache, and then load each book from the second-level cache. Make sure to cache the Books, though.

Part 3:

I can imagine several reasons:

  • there are so many entities and they are so changing that the number of cache hits would be very low, and that the second-level cache handling would in fact consume more time and memory than a solution without cache
  • the application is clustered, and the cost and complexity of a distributed second-level cache is too high, for a low gain
  • other non-hibernate applications write to the same database, and the cache has thus a big risk of returning stale data, which is not acceptable
  • everything goes very well without a second-level cache, and there is no reason to make the application more complex than it is.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!