1.为什么引入缓存?
主要是因为系统中有些接口速度比较慢,其实是sql比较慢(如果想从根本上解决问题的话,其实应该是优化我们的sql),这时候引入缓存可以大大提高响应速度
2.加入缓存带来的问题?
用了缓存后,速度确实大大提升,但是相应带来的问题是 ==缓存的维护==变得复杂,即何时何地需要更新、清除缓存?这个是需要针对具体业务具体情况而定的。
3.SpringBoot中怎么用?
- pom引入相应jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
- 启动类加入注解
@EnableCaching
- 方法上加注解缓存
@Cacheable(value = RedisConstant.PREFIX_APPCODE_MENU, key = "'roleId:' + #roleId + ':appCode:' + #appCode")
public List<Map<String, Object>> findByAppCode(String appCode, String roleId, boolean isRolePermission, String userId, int userType) {
String terminalTypeStr = SsoUserKit.getTerminalType();
int terminalType = 1;
if ("mobile".equals(terminalTypeStr)) {
terminalType = 2;
}
return menuMapper.findMenuByAppCode(appCode, roleId, isRolePermission, userId, userType, terminalType);
}
- 清除缓存
@CacheEvict(value = RedisConstant.PREFIX_APPCODE_MENU, beforeInvocation = true, allEntries = true)