redis LoginUser取不出来
时间: 2023-11-07 19:28:05 浏览: 31
可能是以下原因导致登录用户无法取出:
1. Redis中未存储登录用户信息。
2. 登录用户信息存储在Redis中,但是键名或键值被修改或删除。
3. Redis连接异常,无法正常获取数据。
4. Redis配置错误,导致无法读取登录用户数据。
解决方法:
1. 确认Redis中是否存储了登录用户信息。
2. 检查登录用户信息的键名和键值是否正确。
3. 检查Redis连接是否正常,尝试重新连接。
4. 检查Redis配置是否正确,如端口号、密码等。
相关问题
redis LocalDateTime getCacheObject
```java
LoginUser user = redisCache.getCacheObject(userKey);
```
```java
// 异常定位到是解析List roleIdList 这句时报错,可能是解析List集合的问题
@ApiModelProperty("用户已分配角色id列表")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = WRAPPER_ARRAY)
private List<Long> roleIdList;
@ApiModelProperty("用户已分配角色列表")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = WRAPPER_ARRAY)
private List<SysRoleVO> roleList;
```
@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 { }
可以在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);
}