@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 获取当前线程的用户信息 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String userId = authentication.getName(); String redisKey = "login:" + userId; LoginUser loginUser = JSON.parseObject(redisCache.getCacheObject(redisKey), LoginUser.class); String headerUrl = loginUser.getUser().getHeaderUrl(); System.out.println("获取到的图片路径为:" + headerUrl); if (loginUser != null && modelAndView != null){ modelAndView.addObject("loginUser",loginUser.getUser()); } } 对于以上代码,以下代码要写什么才能清空redis缓存, // 在模板引擎 TemplateEngine 之后执行 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
时间: 2023-12-17 11:02:23 浏览: 97
Spring MVC面试题(2022最新版)
5星 · 资源好评率100%
在 `afterCompletion` 方法中,可以通过 Redis Cache 的 `deleteObject` 方法清空缓存,示例代码如下:
```java
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 获取当前线程的用户信息
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String userId = authentication.getName();
String redisKey = "login:" + userId;
redisCache.deleteObject(redisKey); // 清空缓存
}
```
阅读全文