Hibernate二级缓存<<net.sf.ehcache.hibernate.EhCacheProvider>>

标签 hibernate caching ehcache second-level-cache hibernate.cfg.xml

我想在我的 hibernate 项目中使用二级缓存,但我只了解一点关于 hibernate 二级缓存的知识,任何人都可以解释我应该如何在我的代码中使用它以及我需要什么配置和 .jar 文件吗? 我将这些设置设置为我的 hibernate.cfg.xml 文件

 <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>

并添加这些jar文件ehcache-1.6.1.jar、ehcache-1.6.1-javadoc.jar、ehcache-1.6.1-sources.jar 我想知道我是否应该更改任何其他配置?

我怎样才能知道我的项目使用二级缓存?

如果只是放置此设置,hibernate 会自动使用此设置,否则我必须在 .java 类中使用 ant 其他代码(如任何注释或其他内容)

最佳答案

您要查找的注释是 org.hibernate.annotations.Cache 。基本用法是:

@Entity
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public MyEntity {
    ...

  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  public List<ElementType> getSomeCollection() {
    ...
  }
}

如需查询,您需要enable query cache通过将 hibernate.cache.use_query_cache 属性设置为 true 并在其声明中指定查询可缓存(对于命名查询),或者在查询实例上调用 setCacheable(true) 。

总而言之,您需要非常小心缓存并真正理解您在做什么,否则弊大于利。不要将其视为“快速修复” - 例如,缓存所有内容绝对是错误的做法。

关于Hibernate二级缓存<<net.sf.ehcache.hibernate.EhCacheProvider>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1216630/

相关文章:

java - 如何在hibernate注解中实现组件映射

java - Hibernate:存储基元的嵌入映射

java - 如何在 LoadingCache 中有效存储可能具有多个键的对象

java - 分布式环境下的defaultCache和cache问题(数据不一致)

java - 如何获取Infinispan缓存中的数据大小,替代ehcache的calculateInMemorySize?

java - Hibernate - where 子句任何集合作为命名参数查询

java - Hibernate Association 在映射类时引用未映射的类

java - 全局清除 CloudFlare 缓存中的文件需要多长时间?

javascript - 如何始终刷新由ajax加载的文件,避免缓存文件

java - JBoss Cache 和 Ehcache 的性能