开发者

What's the suitable use-case for the Hibernate 2nd level cache and the Spring 3.1's @Cacheable?

开发者 https://www.devze.com 2023-03-25 22:01 出处:网络
I have read about 开发者_StackOverflowthe Hibernate\'s reference about Hibernate 2nd level cache in here, and I\'ve read about the Spring 3.1\'s Cache abstraction in here.

I have read about 开发者_StackOverflowthe Hibernate's reference about Hibernate 2nd level cache in here, and I've read about the Spring 3.1's Cache abstraction in here.

After then, I've understood the Hibernate's 2nd level cache is very similar to the first level cache but, it's scope is extended to the SessionFactory. And, the Spring 3.1's @Cacheable looks good for the service layer.

I want to here from you more detailed use-cases for each.

  • When and where should I use the Hibernate 2nd level cache?
  • When and where should I use the Spring 3.1's Cache Abstraction?

Thanks for reading.


One common use case for Hibernate's second level cache is to cache static or very infrequently changing reference data. For example, a list of states/provinces and their attributes.

Spring's @Cacheable is useful anywhere you have a method that returns a value that is expensive to compute.

In both the @Cacacheable and Hibernate second level cache scenarios if the data will change over time then you should think hard about what cache implementation you should use (eg, should it be a distributed cache? What about cache invalidation?)


@Whiteship, these two are entirely different things.

@Cahceable has more to do with method and computaion extensive operation.

Hiberante second level cache is by and large for caching the persistent data across SessionFactory. it is not enabled by default and improves the performance a lot. (not just for read only data but writeable data too if used in right manner).

Cacheable can be equated to Hibernate query cache somewhat. For enabling hibernate query cache, hibernate second level cache needs to be enabled.

But as such we cache the result of query with same args and we cache the result of method with same args. There do is some conceptual-level of similarity there,

0

精彩评论

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

关注公众号