有没有人将 Spring 数据与 Ehcache 二级缓存一起使用?我在正确配置它时遇到问题。
JPA 属性:
<prop key="hibernate.cache.provider_class">org.hibernate.cache.SingletonEhCacheProvider</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.generate_statistics">true</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ad_lobby/ehcache.xml</prop>
阻力单位:
<persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
类标有 Cacheable 注解
但是在启动测试时:
@Test
public void testCache() {
def device = new Device()
deviceRepository.save(device)
for (int i = 0; i < 25; i++) {
long now = new Date().getTime()
deviceRepository.findById(device.id)
long dif = new Date().getTime() - now
println('Difference: ' + dif)
}
}
我的对象有 25 个选择,尽管实体已被放入缓存:
second level cache puts=1
second level cache hits=0
second level cache misses=0
有任何想法吗?