Spring Boot使用@Cacheable注解

通常,我使用Hibernate的@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)注解来缓存@Entity类.

在JAP2规范内另一个注解@Cacheable有与Hibernate的@Cache的一样的功能使用@Cacheable的条件如下.

1.Entity Class实现Serializable接口

2.在Entity Class前加入@Cacheable(true)

例如

@Entity
@Cacheable(true) 
public class UserEntity implements Serializable {
 // properties
}

 

3.在配置文件内开启缓存[1]

spring.jpa.properties.javax.persistence.sharedCache.mode=ENABLE_SELECTIVE

 

参考: http://docs.oracle.com/javaee/6/tutorial/doc/gkjio.html [1]

http://stackoverflow.com/questions/31585698/spring-boot-jpa2-hibernate-enable-second-level-cache