1:修改pom.xml文件,添加依赖
<!-- Spring Boot 缓存支持启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
2:添加配置文件
在src/main/resources下创建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:修改application.yml文件
spring:
cache:
ehcache:
config: ehcache.xml
4:修改启动类,开启缓存。
@EnableCaching 开启缓存
package com.zhaoy;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
@MapperScan(basePackages = {"com.zhaoy.mapper"})
@EnableCaching
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
5:实体类持久化
实体类需要进行持久化才能进行缓存。
package com.zhaoy.entity;
import java.io.Serializable;
import lombok.Data;
@Data
public class Comment implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String productId;
private String commentTime;
private Boolean isDeleted;
}
6:业务层使用缓存
需使用@Cacheable(value = "users")使用缓存,若不指定value,则使用默认的缓存配置。
package com.zhaoy.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhaoy.entity.Comment;
import com.zhaoy.mapper.CommentMapper;
@Service
public class CommentService {
@Autowired
private CommentMapper commentMapper;
@CacheEvict
@Transactional
public int insertComment(String id, String productId, String commentTime, Boolean isDeleted) {
int insertComment = commentMapper.insertComment(id,productId,commentTime,isDeleted);
return insertComment;
}
@Cacheable(value = "users")
public PageInfo<Comment> selCommentList(int pageNum, int pageSize){
PageHelper.startPage(pageNum, pageSize);
List<Comment> list = commentMapper.selCommentList();
PageInfo<Comment> pageInfo = new PageInfo<Comment>(list);
return pageInfo;
}
}
7:@Cacheable与@CacheEvict
@Cacheable:把方法的返回值添加到Ehcache中做缓存。
@@CacheEvict:清除缓存,保证数据一致性。