Hibernate二级缓存未更新驱逐所有条目的缓存

时间:2019-02-26 15:43:03

标签: java spring hibernate jpa caching

我已经使用EHcache提供程序在Spring Data中启用了Hibernate L2缓存。

我正在使用Spring Boot 2.1.0.RELEASE

Ehcache休眠

 <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.1.Final</version>
        </dependency>

application.properties

spring.jpa.show-sql=true

logging.level.root=INFO


# jpa properties
spring.jpa.properties.hibernate.show-sql=true
spring.jpa.properties.hibernate.format-sql=true





# statistics
spring.jpa.properties.hibernate.generate_statistics=false

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

#spring.jpa.properties.javax.persistence.sharedCache.mode=ALL

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

我的实体

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "TCountry")
@Data
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

public class TCountry {
    @Id
    private String countryCode;

    @Column(name = "region_code")
    private String regionCode;

    @Column(name = "entryTime")
    private Date entryTime;


    private String entryUser;


}

存储库

public interface TCountryRepository extends CrudRepository<TCountry, String> {

    @Override
    @QueryHints({
            @QueryHint(name = "org.hibernate.cacheable", value = "true")})
    List<TCountry> findAll();


}

测试

 @Test
    public void contextLoads() throws InterruptedException {

        log.info("found {}", tCountryRepository.findAll());

        TCountry ad = tCountryRepository.findById("AD").get();
        ad.setEntryTime(new Date());
        tCountryRepository.save(ad);

        log.info("found {}", tCountryRepository.findAll().stream().filter(
                c -> c.getCountryCode().equals("AD")).findAny().get());

    }

当我执行保存并再次运行findAll()时,它将再次进入数据库。我认为保存将更新高速缓存。

如果我删除了save(),那么它就不会进入数据库。

我想念什么吗?如果在save()之后进入数据库,那么缓存的意义何在!

0 个答案:

没有答案