【发布时间】:2014-05-13 17:54:15
【问题描述】:
这是我的配置:
pom.xml: 使用hibernate和hibernate-ehcache 4.2.8.Final版本
spring config:我有以下休眠属性
<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>
实体 - 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
当我最终进行手动数据库更改时,我似乎不明白缓存是如何获取最新值的。日志表明它也在从缓存中挑选这些更改的值。
二级缓存是否会接收手动 DB 更改,即 DB 更改不会像上面的日志所暗示的那样经过休眠?还是我做错了什么?
【问题讨论】:
标签: java spring hibernate ehcache