【发布时间】: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