简体   繁体   English

休眠二级缓存了解

[英]hibernate 2nd level cache understanding

Just started learning hibernate, and to understand hibernate 2nd level cache. 刚开始学习休眠,并了解休眠2级缓存。 I created a comment Entity and try to work on it. 我创建了一个注释实体,并尝试对其进行处理。 Here are my codes! 这是我的代码!

 @Entity
 @Table(name = "comment")
 @FilterDefs(value={@FilterDef(name="projectFilter",parameters=@ParamDef(name="projectID", type="java.lang.Long" )),   @FilterDef(name="issueFilter", parameters=@ParamDef( name="issueID", type="java.lang.Long" ) )})
 @Filters(value={@Filter(name = "projectFilter", condition = "project_id = :projectID"), 
    @Filter(name = "issueFilter", condition = "issue_id = :issueID")})
  @Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="comment")
  public class Comment {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false, precision = 5, scale = 0 )
private long id;
@Column(name = "project_id", nullable = false)
private long projectId;
@Column(name = "issue_id", nullable = false)
private long issueId;
@Column(name = "author_id", nullable = false)
private long auhorId;
@Column(name = "author_name", nullable = false)
private String authorName;
@Column(name = "comment")
private String comment;
@Column(name = "created_date")
private Date createdDate;

I want to fetch user comment based on project or project and issue. 我想获取基于项目或项目和问题的用户评论。 In DAO,I have written the following function for comments based on projectid. 在DAO中,我编写了以下基于projectid的注释功能。

   @Autowired
   private SessionFactory sessionFactory;
  ...
  @Override
  public List<Comment> getAllProjectComments(long projectId) {  
    Session session = getCurrentSession();
    Filter filter = session.enableFilter("projectFilter");
    filter.setParameter("projectID", projectId);
    return session.createQuery("from Comment").setCacheable(true).list();

}

and ehcahe.xml is below 并且ehcahe.xml在下面

    <?xml  version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false">
    <diskStore path="java.io.tmpdir/hibernate-cache"/>

    <defaultCache maxElementsInMemory="500"
            eternal="false"
            timeToIdleSeconds="1200"
            timeToLiveSeconds="2400"
            overflowToDisk="false"
            maxElementsOnDisk="1000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="1200"
            memoryStoreEvictionPolicy="LRU"/>

    <cache  name="org.trackMyProject.entity.Comment"
   maxElementsInMemory="50"
   maxElementsOnDisk="500"
   eternal="false"
       timeToIdleSeconds="30"
       timeToLiveSeconds="120"
       overflowToDisk="true"
    />

I have added 500 comments in table for 2 projectids. 我在表中为2个projectids添加了500条注释。

when the controller call DAO method for getting the comments based on the project id and subsequent call for that project id seems to work perfectly with 2nd level cache and DB is not getting hit. 当控制器调用DAO方法以基于项目ID获取注释,并且随后对该项目ID的调用似乎与第二级缓存完美配合,而DB未被命中时。 But if I switch between 2 projectIDs constantly, then each time DB gets hit, that I don't want. 但是,如果我不断在2个projectID之间切换,那么每次DB被命中时,我都不会想要。

Can anybody tell me what kind of mistake I have made or need to do more configuration. 谁能告诉我我犯了什么样的错误或需要做更多的配置。

Thanks in advance!! 提前致谢!!

EDIT! 编辑!

classpath:hibernate.cfg.xml classpath:hibernate.cfg.xml

<property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">${jdbc.dialect}</prop>
        <prop key="hibernate.show_sql">true</prop>
        <!-- <prop key="hibernate.cache.use_query_cache">true</prop> -->
        <prop key="hibernate.connection.release_mode">after_transaction</prop>
        <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
        <prop key="hibernate.cache.use_second_level_cache">true</prop>
        <prop key="hibernate.cache.use_query_cache">true</prop>
        <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
        <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    </props>
</property>
</bean>

您正在使用createQuery,这意味着您需要查询级缓存,请尝试添加以下内容:

<property name='hibernate.cache.use_query_cache'>true</property>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM