1、配置pom.xml文件,添加坐标
<!-- 测试工具的启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- Spring Boot 缓存支持启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!-- Ehcache 坐标 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
2、配置EHcache的配置文件
文件名:ehcache.xml(xml head信息可以从ehcache.jar包中获取)
位置:src/main/resource/ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/
XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <!--defaultCache:echcache 的默认缓存策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <!-- 自定义缓存策略 --> <cache name="users" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache> </ehcache>
3、修改Spring Boot全局配置文件 application.propreties
spring.cache.ehcache.cofnig=ehcache.xml
4、修改Spring Boot启动类
@SpringBootApplication @EnableCaching public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
5、添加实体类
@Entity @Table(name="t_users") public class Users implements Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="id") private Integer id; @Column(name="name") private String name; @Column(name="age") private Integer age; @Column(name="address") private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Users [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]"; } }
6、创建业务层
/** * UsersService 接口实现类 * * */ @Service public class UsersServiceImpl implements UsersService { @Autowired private UsersRepository usersRepository; @Override public List<Users> findUserAll() { return this.usersRepository.findAll(); } @Override //@Cacheable:对当前查询的对象做缓存处理 @Cacheable(value="users") public Users findUserById(Integer id) { return this.usersRepository.findOne(id); } @Override public Page<Users> findUserByPage(Pageable pageable) { return this.usersRepository.findAll(pageable); } @Override public void saveUsers(Users users) { this.usersRepository.save(users); } }
7、测试类(使用JUnit测试,需要添加JUnit坐标)
/** * UsersService 测试 * * */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes=App.class) public class UsersServiceTest { @Autowired private UsersService usersService; @Test public void testFindUserById(){ //第一次查询 System.out.println(this.usersService.findUserById(1)); //第二次查询 System.out.println(this.usersService.findUserById(1)); } }
8、注解操作(@Cacheable 与@CacheEvict)
8.1@Cacheable 作用:把方法的返回值添加到 Ehcache 中做缓存
Value 属性:指定一个 Ehcache 配置文件中的缓存策略,如果没有给定 value,name 则
表示使用默认的缓存策略。
Key 属性:给存储的值起个名称。在查询时如果缓存中有对应的KEY值,那么则直接从缓存中将
数据返回
8.1.1、业务层代码
@Override @Cacheable(value="users",key="#pageable.pageSize") public Page<Users> findUserByPage(Pageable pageable) { return this.usersRepository.findAll(pageable); }
8.1.2、测试代码
@Test public void testFindUserByPage(){ Pageable pageable = new PageRequest(0, 2); //第一次查询 System.out.println(this.usersService.findUserByPage(pageable).getTot alElements()); //第二次查询 System.out.println(this.usersService.findUserByPage(pageable).getTot alElements()); //第三次查询 pageable = new PageRequest(1, 2); System.out.println(this.usersService.findUserByPage(pageable).getTot alElements()); } }
8.2、@CacheEvict 作用:清除缓存(解决读取缓存数据与数据库保持一致,用在数据更新或插入操作)
8.2.1、业务层代码
/** * UsersService 接口实现类 * * */ @Service public class UsersServiceImpl implements UsersService { @Autowired private UsersRepository usersRepository; @Override @Cacheable(value="users") public List<Users> findUserAll() { return this.usersRepository.findAll(); } @Override //@Cacheable:对当前查询的对象做缓存处理 @Cacheable(value="users") public Users findUserById(Integer id) { return this.usersRepository.findOne(id); } @Override @Cacheable(value="users",key="#pageable.pageSize") public Page<Users> findUserByPage(Pageable pageable) { return this.usersRepository.findAll(pageable); } @Override //@CacheEvict(value="users",allEntries=true) 清除缓存中以 users 缓 存策略缓存的对象 @CacheEvict(value="users",allEntries=true) public void saveUsers(Users users) { this.usersRepository.save(users); } }
8.2.2、测试代码
@Test public void testFindAll(){ //第一次查询 System.out.println(this.usersService.findUserAll().size()); Users users = new Users(); users.setAddress("南京"); users.setAge(43); users.setName("朱七"); this.usersService.saveUsers(users); //第二次查询 System.out.println(this.usersService.findUserAll().size()); }