目录
1 Spring Cache 相关注解说明
1.1 @CacheConfig
1.2 @Cacheable
1.3 @CachePut
1.4 @CacheEvict
1.5 @Caching
2 环境配置
2.1 pom.xml 依赖添加
2.2 ehcahe.xml ⽂件添加
2.3 application.yml 缓存配置
2.4 启动缓存
2.5 JavaBean 对象实现序列化
3 缓存实现
3.1 用户详情查询缓存
3.2 用户列表查询缓存
3.3 用户更新&删除缓存
EhCache 是⼀个⽐较成熟的 Java 缓存框架,最早从 hibernate 发展⽽来, 是进程中的缓存系统,它提供了⽤内存,磁盘⽂件存储,以及分布式存储⽅式等多种灵活的 cache 管理⽅案,快速简单。
Spring Boot 对 Ehcache 的使⽤提供⽀持,所以在 Spring Boot 中只需简单配置即可使⽤ Ehcache 实现数据缓存处理。
1 Spring Cache 相关注解说明
SpringBoot 缓存实现内部使⽤ SpringCache 实现缓存控制,这⾥集成 Ehcache 实际上是对 SpringCache 抽象的其中⼀种实现,这⾥在使⽤ Ehcache 实现缓存控制时相关注解说明如下。
1.1 @CacheConfig
⽤于标注在类上,可以存放该类中所有缓存的公有属性,⽐如设置缓存的名字。
@CacheConfig(cacheNames = "users")
public interface UserService {。。。}
配置了该数据访问对象中返回的内容将存储于名为users的缓存对象中,我们也可以不使⽤该注解, 直接通过@Cacheable⾃⼰配置缓存集的名字来定义。
1.2 @Cacheable
应⽤到读取数据的⽅法上,即可缓存的⽅法,如查找⽅法,先从缓存中读取,如果没有再调⽤相应⽅ 法获取数据,然后把数据添加到缓存中。
该注解主要有下⾯⼏个参数:
value、cacheNames:两个等同的参数( cacheNames 为 Spring 4 新增,作为 value 的别 名),⽤于指定缓存存储的集合名。由于 Spring 4 中新增了 @CacheConfig,因此在 Spring 3 中 原本必须有的 value 属性,也成为⾮必需项了。
key:缓存对象存储在Map集合中的 key 值,⾮必需,缺省按照函数的所有参数组合作为 key 值, 若⾃⼰配置需使⽤ SpEL 表达式,⽐如:@Cacheable(key = "#p0"):使⽤函数第⼀个参数作为缓 存的 key 值,更多关于SpEL 表达式的详细内容可参考官⽅⽂档。
condition:缓存对象的条件,⾮必需,也需使⽤SpEL表达式,只有满⾜表达式条件的内容才会被 缓存,⽐如:@Cacheable(key = "#p0", condition = "#p0.length() < 3"),表示只有当第⼀个参数 的⻓度⼩于3的时候才会被缓存。
unless:另外⼀个缓存条件参数,⾮必需,需使⽤ SpEL 表达式。它不同于 condition 参数的地⽅ 在于它的判断时机,该条件是在函数被调⽤之后才做判断的,所以它可以通过对 result 进⾏判 断。
keyGenerator:⽤于指定 key ⽣成器,⾮必需。若需要指定⼀个⾃定义的 key ⽣成器,我们需要 去实现org.springframework.cache.interceptor.KeyGenerator 接⼝,并使⽤该参数来指定。需 要注意的是:该参数与 key 是互斥的。
cacheManager:⽤于指定使⽤哪个缓存管理器,⾮必需。只有当有多个时才需要使⽤ cacheResolver:⽤于指定使⽤那个缓存解析器,⾮必需。需通过 org.springframework.cache. interceptor.CacheResolver 接⼝来实现⾃⼰的缓存解析器,并⽤该 参数指定。
@Cacheable(value = "user", key = "#id")
User selectUserById(final Integer id);
1.3 @CachePut
应⽤到写数据的⽅法上,如新增/修改⽅法,调⽤⽅法时会⾃动把相应的数据放⼊缓存,@Cache Put的参数与 @Cacheable 类似,示例如下:
@CachePut(value = "user", key = "#user.id")
public User save(User user) { users.add(user); return user;
}
1.4 @CacheEvict
应⽤到移除数据的⽅法上,如删除⽅法,调⽤⽅法时会从缓存中移除相应的数据,示例如下:
@CacheEvict(value = "user", key = "#id")
void delete(final Integer id);
除了同 @Cacheable ⼀样的参数之外,@CacheEvict 还有下⾯两个参数:
allEntries:⾮必需,默认为 false。当为 true 时,会移除所有数据
beforeInvocation:⾮必需,默认为 false,会在调⽤⽅法之后移除数据。当为 true 时,会在调 ⽤⽅法之前移除数据。
1.5 @Caching
组合多个 Cache 注解使⽤。示例:
@Caching(put = {@CachePut(value = "user", key = "#user.id"),@CachePut(value = "user", key = "#user.username"),@CachePut(value = "user", key = "#user.age")}
}
将 id ---> user;username ---> user;age ---> user 进⾏缓存。
2 环境配置
2.1 pom.xml 依赖添加
<!-- Ehcache -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId>
</dependency>
2.2 ehcahe.xml ⽂件添加
src/main/resources ⽬录下添加 ehcache.xml ⽂件,内容如下:
<ehcache name="mycache"><!--如果不使⽤磁盘存储,只需要将diskStore注释掉即可;如果使⽤,需要在ehcache.xml⽂件中的ehcahce元素下的定义⼀个diskStore元素并指定其path
属性。--><diskStore path="C:\java\cache"/><!--name:缓存名称。maxElementsInMemory:缓存最⼤数⽬maxElementsOnDisk:硬盘最⼤缓存个数。eternal:对象是否永久有效,⼀但设置了,timeout将不起作⽤。overflowToDisk:是否保存到磁盘,当系统宕机时timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使⽤,可选属性,默认值是0,表示可闲置时间⽆
穷⼤。timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最⼤时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使⽤,默认是0,也就是对象存活时间⽆穷⼤。diskPersistent:是否缓存虚拟机重启期数据Whether the disk store persists between restarts of the Virtual Machine.
The default value is false.diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区⼤⼩。默认是30MB。每个Cache都应该有⾃⼰的⼀个缓冲区。diskExpiryThreadIntervalSeconds:磁盘失效线程运⾏时间间隔,默认是120秒。memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,会根据指定的策略
去清理内存默认策略是LRU(最近最少使⽤)。你可以设置为FIFO(先进先出)或是LFU(较少使
⽤)。clearOnFlush:内存数量最⼤时是否清除。memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使⽤,默认策略)Less Frequently Used,就是例⼦中使⽤的策略,就是⼀直以来最少被使⽤的。FIFO(先进先出)first in first out,这个是⼤家最熟的,先进先出。LFU(最少访问次数)Least Recently Used,最近最少使⽤的。缓存的元素有⼀个时间戳,当缓存容量满了,⽽⼜需要腾出地⽅来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。--><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"maxElementsOnDisk="10000000"diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"></defaultCache><cachename="users"eternal="false"maxElementsInMemory="100"overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="0"timeToLiveSeconds="300"memoryStoreEvictionPolicy="LRU"/>
</ehcache>
2.3 application.yml 缓存配置
spring:datasource:type: com.mchange.v2.c3p0.ComboPooledDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/springboot_mybatis?
useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8username: rootpassword: rootdevtools:restart:enabled: true# 设置重启的⽬录,添加⽬录的⽂件需要restartadditional-paths: src/main/java# 解决项⽬⾃动重新编译后接⼝报404的问题poll-interval: 3000quiet-period: 1000## Ehcache缓存配置cache:ehcache:config: classpath:ehcache.xml
2.4 启动缓存
在 Starter 启动⼊⼝类中,添加 @EnableCaching 注解,启动缓存:
@SpringBootApplication
@EnableCaching
@MapperScan("com.xxxx.springboot.dao")
public class Starter {public static void main(String[] args) {SpringApplication.run(Starter.class);}
}
2.5 JavaBean 对象实现序列化
@ApiModel(description = "⽤户实体对象")
public class User implements Serializable {@ApiModelProperty(value = "⽤户id")private Integer id;@ApiModelProperty(value = "⽤户名")private String userName;@ApiModelProperty(value = "⽤户密码")private String userPwd;/*省略get|set*/
}
3 缓存实现
以 UserService 的⽅法为例
3.1 用户详情查询缓存
@Cacheable(value = "users",key = "#userId")
public User queryUserByUserId(Integer userId){return userMapper.queryById(userId);
}
3.2 用户列表查询缓存
@Cacheable(value = "users",key="#userQuery.userName+'-'+#userQuery.pageNum+'-
'+#userQuery.pageSize")
public PageInfo<User> queryUserByParams(UserQuery userQuery){PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());return new PageInfo<User>(userMapper.selectByParams(userQuery));
}
3.3 用户更新&删除缓存
@CacheEvict(value = "users",key="#user.id")
public void updateUser(User user) {AssertUtil.isTrue(StringUtils.isBlank(user.getUserName()), "⽤户名不能为
空!");AssertUtil.isTrue(StringUtils.isBlank(user.getUserPwd()),"⽤户密码不能为
空!");User temp = userMapper.queryUserByUserName(user.getUserName());AssertUtil.isTrue(null != temp && !(temp.getId().equals(user.getId())), "该
⽤户已存在!");AssertUtil.isTrue(userMapper.update(user)<1,"⽤户记录添加失败!");
}
@CacheEvict(value = "users",allEntries=true)
public void deleteUser(Integer userId){AssertUtil.isTrue(null == userId || null ==
userMapper.queryById(userId),"待删除记录不存在!");AssertUtil.isTrue(userMapper.delete(userId)<1,"⽤户删除失败!");
}