@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-26 09:06:58 浏览: 164
Spring MVC面试题(2022最新版)
5星 · 资源好评率100%
可以在afterCompletion方法中执行redis的刷新操作,使得模板引擎能够获取到最新的redis缓存数据。具体代码如下:
@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;
LoginUser loginUser = JSON.parseObject(redisCache.getCacheObject(redisKey), LoginUser.class);
String headerUrl = loginUser.getUser().getHeaderUrl();
System.out.println("获取到的图片路径为:" + headerUrl);
// 刷新redis缓存
redisCache.refreshCache(redisKey);
}
阅读全文