【问题标题】:How to cache list using hibernate ehcache如何使用休眠 ehcache 缓存列表
【发布时间】:2014-12-16 13:34:39
【问题描述】:

我在我的网络应用程序中使用休眠 ehcache。我缓存了如下列表。

model.put("projecttype",sessionfactory.getCurrentSession().createCriteria(TblProjectType.class).list());
         System.out.println("Fetch Count="+ stats.getEntityFetchCount());
        System.out.println("Second Level Hit Count="+ stats.getSecondLevelCacheHitCount());
        System.out.println("Second Level Miss Count="+ stats.getSecondLevelCacheMissCount());
        System.out.println("Second Level Put Count="+ stats.getSecondLevelCachePutCount());

EHCACHE.xml

<cache
    name="com.uniphore.timesheet.domain.TblProjectType"
    maxElementsInMemory="1000"
    eternal="false"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600"
    overflowToDisk="true" statistics="true"/>
TblProjectType table has 5 rows only.

我的输出:

--First---
Fetch Count=0
Second Level Hit Count=0
Second Level Miss Count=0
Second Level Put Count=0
--Second---
Fetch Count=0
Second Level Hit Count=0
Second Level Miss Count=0
Second Level Put Count=11
---Third--
Fetch Count=0
Second Level Hit Count=0
Second Level Miss Count=0
Second Level Put Count=22

我的 Ebtity 类:

 @Entity
    @Table(name="tblProjectType")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @Repository
    public class TblProjectType  implements java.io.Serializable {

        private static final long serialVersionUID = -1798070786993154681L;
         @Id
         @GeneratedValue
         @Column(name ="ID")
         private short id;
         @Column(name ="TypeName")
         private String typeName;
         @Column(name="CreatedDate")
         private Date createdDate;
         @Column(name="ModifiedDate")
         private Date modifiedDate;
         @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblProjectType", cascade = CascadeType.ALL)
         private Set<TblStage> tblStages = new HashSet<TblStage>(0);
         @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblProjectType", cascade = CascadeType.ALL)
         private Set<TblProject> tblProjects = new HashSet<TblProject>(0);

        //getters and setters 



    }

每次放置计数增加11。但其他值没有改变。为什么二级缓存没有命中?

为什么每次都执行查询而不是从二级缓存中获取结果?

为什么当我点击它应该更新而不是插入的相同查询时,二级放置计数会增加? 任何帮助将不胜感激!!!

【问题讨论】:

  • @Madhesh 这个model 在这里做什么,你是如何缓存你的实体和查询的
  • Tblproject 类型是我的实体类。我将查询结果存储在缓存中。通过使用 ehcache.xml
  • 您是否将 TblProjectType 实体标记为可缓存?发布您的 TbProjectType 类代码。
  • @Andy Durfens,我已将 tblprojecttype 标记为实体类。我已在我的问题中发布了我的实体类。
  • 这很奇怪。我假设您已启用二级缓存和查询缓存。你也可以发布休眠配置吗?

标签: java spring hibernate caching ehcache


【解决方案1】:

尝试两件事。

  1. 在实体类中指定一个缓存区域

    @Entity
    @Table(name="tblProjectType")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region="yourcacheregion")
    @Repository
    public class TblProjectType  implements java.io.Serializable {
    }
    
  2. 将条件查询标记为可缓存

    model.put("projecttype",sessionfactory.getCurrentSession().createCriteria(TblProjectType.class)..setCacheable(true).list());

我假设你有如下的休眠配置

<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<property name="hibernate.generate_statistics" value="true"/>

【讨论】:

  • 是的,你的假设是正确的。但我很清楚这个错误 DummyPinnedKey cannot be cast to org.hibernate.cache.QueryKey。
  • 我没有关注你。什么时候出现错误?
  • sessionfactory.getCurrentSession().createCriteria(TblProjectType.class).setCacheable(true).list() 当尝试执行此查询时,我收到异常 DummyPinnedKey cannot be cast to org.hibernate。 cache.QueryKey
  • 不设置cacheable true时没有出现这个异常?
猜你喜欢
  • 1970-01-01
  • 2015-04-24
  • 2012-01-03
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
相关资源
最近更新 更多