这是我的配置:
的pom.xml: 使用hibernate和hibernate-ehcache 4.2.8.Final version
spring config:我有以下hibernate属性
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
ehcache.xml中
<ehcache>
<diskStore path="java.io.tmpdir"/>
<!-- cache setting for com.cisco.locker.entity.SystemConfig entity -->
<cache name="com.cisco.locker.entity.SystemConfig"
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="600"
overflowToDisk="false"
statistics="true"
/>
</ehcache>
entity - com.cisco.locker.entity.SystemConfig.java
@Entity
@Table(name = "system_config")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SystemConfig implements Serializable {
...
}
日志:
First call: http://localhost:8080/test/getSystemConfigs
{
"statusCode": "200",
"statusMessage": "SUCCESS",
"response": {
"admin.lcu_status_polling_timer": "120",
"admin.site_status_polling_interval": "15"
}
}
Hibernate: select systemconf0_.ID as ID1_12_, systemconf0_.description as descript2_12_, systemconf0_.property as property3_12_, systemconf0_.value as value4_12_ from system_config systemconf0_ where systemconf0_.property like ?
2014-05-13 00:53:40,553 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-1]: com.cisco.locker.entity.SystemConfig cache - Miss
2014-05-13 00:53:40,561 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-1]: com.cisco.locker.entity.SystemConfig cache - Miss
Second call: http://localhost:8080/test/getSystemConfigs
{
"statusCode": "200",
"statusMessage": "SUCCESS",
"response": {
"admin.lcu_status_polling_timer": "120",
"admin.site_status_polling_interval": "15"
}
}
Hibernate: select systemconf0_.ID as ID1_12_, systemconf0_.description as descript2_12_, systemconf0_.property as property3_12_, systemconf0_.value as value4_12_ from system_config systemconf0_ where systemconf0_.property like ?
2014-05-13 00:53:48,037 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-2]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#11
2014-05-13 00:53:48,037 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-2]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#12
Third call – After manually changing value in DB "admin.site_status_polling_interval" from 15 to 10: http://localhost:8080/test/getSystemConfigs
{
"statusCode": "200",
"statusMessage": "SUCCESS",
"response": {
"admin.lcu_status_polling_timer": "120",
"admin.site_status_polling_interval": "10"
}
}
Hibernate: select systemconf0_.ID as ID1_12_, systemconf0_.description as descript2_12_, systemconf0_.property as property3_12_, systemconf0_.value as value4_12_ from system_config systemconf0_ where systemconf0_.property like ?
2014-05-13 00:54:08,738 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-4]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#11
2014-05-13 00:54:08,738 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-4]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#12
我似乎不明白当我最终进行手动数据库更改时,缓存如何获取最新值。日志表明它也从缓存中选择这些更改的值。
二级缓存是否会收到手动数据库更改,即数据库更改未通过休眠,如上面的日志所示?或者我做错了什么?
答案 0 :(得分:0)
好吧,正如日志所示,对数据库执行SQL查询。数据来自数据库,而不是来自缓存。你看到的点击是商店点击。
要从缓存中获取实体,请使用session.get()
,而不是查询。