前言
对一个应用系统而言,把经常使用且很少更新的数据加入到缓存,是非常有必要的。因为从缓存中获取数据的速度要优于数据库,把redis内存型数据库作为缓存是非常常见的做法。
当项目集成redis框架后,就可以使用StringRedisTemplate对象对数据进行缓存,优先从redis中获取数据,若获取不到再查询数据库。当缓存数据较多时,每次都需要判断,代码未免有些臃肿。可使用方法加@Cacheable注解解决,但需要避免踩坑。
失效场景
@Cacheable注解是基于spring aop切面实现,必须走代理才有效,开发过程中有些场景会导致不能缓存或缓存失效。经过这两天的踩坑,总结失效的情况有以下三点:
1、同类或子类调用父类带有缓存注解的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Service public class UserService {
@Autowired private UserDao userDao;
@Cacheable(cacheNames = "user", key = "#id", unless = "#result==null") public User getUserById(Integer id){ return userDao.getUserById(id); } public void updateUser(User user) throws Exception{ User user = this.getUserById(user.getId()); if(user == null){ throw new Exception("用户不存在"); } userDao.updateUser(user); } }
|
代码说明:更新用户信息时调用了同类的查询方法,若用户存在再进行更新操作
解决方法:将缓存方法放在单独的类中,通过service注入的方式在另一类中调用
2、加了@PostConstruct注解的方法,调用了带有缓存的注解方法
1 2 3 4 5 6 7 8 9 10 11
| public class CacheService {
@Autowired private UserService userService;
@PostConstruct public void initData(){ userService.getUserList(); } }
|
代码说明:项目启动时,将所有用户信息初始化到缓存中
解决方法:创建监听器实现接口ApplicationListener,项目启动时将数据加载
1 2 3 4 5 6 7 8 9 10
| public class InitCacheListener implements ApplicationListener<ApplicationReadyEvent> {
@Override public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { ConfigurableApplicationContext ctx = applicationReadyEvent.getApplicationContext(); UserService userService = ctx.getBean(UserService.class); userService.getUserList(); }
}
|
启动类添加已创建的监听器
1 2 3 4 5 6 7 8 9 10 11
| @SpringBootApplication public class Application {
public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); // 添加初始化缓存监听器 app.addListeners(new InitCacheListener()); app.run(args); }
}
|
3、添加Cacheable注解不合适的属性
key的属性值,一般会使用#带形参或者采取字符串拼接参数的方式。若需要查询所有数据,直接用字符串常量,数据会进行缓存,但每次调用都不会从缓存里面取数据
1 2 3 4
| @Cacheable(cacheNames = "user", key = "'userList'", unless = "#result==null") public List<User> getUserList() { return userDao.getUserList(); }
|
解决方法:将key取值为方法名,如key = “#root.methodName”或者在方法中定义常量,再进行引入
1 2 3 4 5 6 7
| public static final USER_KEY = "userList";
@Cacheable(cacheNames = "user", key = "#root.target.USER_KEY", unless = "#result==null") public List<User> getUserList() { return userDao.getUserList(); } }
|