/**
* 保存用户基础信息 --> Redis
* @param user
*/
public void saveUserInfoToRedis(User user) {
String userId = user.getUserId();
redisTemplate.opsForValue().set(Config.getLoginUserKey(userId), JSONObject.toJSONString(user));
redisTemplate.expire(Config.getLoginUserKey(userId), Config.USER_INFO_TIMEOUT, TimeUnit.MILLISECONDS);
}
/**
* 保存用户登录信息 --> Redis
* @param userId
* @param sessionId
*/
public void saveUserLoginInfoToRedis(String userId,String sessionId) {
JSONObject userLoginInfo = new JSONObject();
userLoginInfo.put("userId", userId);
redisTemplate.opsForValue().set(Config.getLoginSessionKey(sessionId), userLoginInfo.toJSONString());
redisTemplate.expire(Config.getLoginSessionKey(sessionId), Config.LOGIN_INFO_TIMEOUT, TimeUnit.MILLISECONDS);
}
配置类
package com.video.base.utils;
public class Config {
/**
* 线程名规范化
*/
public static String THREAD_NAME_FORMAT = "video-download-at-%d";
public static final String LOGIN_USER_SESSION = "login:user:session";
public static final String LOGIN_USER_INFO = "login:user:info";
public static final String REGIST_SESSION_VCODE = "regist:session:vcode";
/**
* 登录信息过期时间,与session保持一致为30分钟
*/
public static final Long LOGIN_INFO_TIMEOUT = 60*30*1000L;
/**
* 用户信息保存在session中,为7天(一周)
*/
public static final Long USER_INFO_TIMEOUT = 7*24*3600*1000L;
public static String getLoginSessionKey(String sessionId) {
return LOGIN_USER_SESSION + ":" + sessionId;
}
public static String getLoginUserKey(String userId) {
return LOGIN_USER_INFO + ":" + userId;
}
public static String getRegistSessionVCodeKey(String sessionId,String phone) {
return REGIST_SESSION_VCODE + ":" + sessionId + ":" + phone;
}
}
查询
/**
* 从Redis中获取用户基本信息
* @param userId
* @return
*/
@Override
public User getUserInfo(String userId) {
Object obj = redisTemplate.opsForValue().get(Config.getLoginUserKey(userId));
if(obj == null) return null;
return JSONObject.parseObject(Objects.toString(obj), User.class);
}
删除
/**
* 删除用户登录信息
* @param session
*/
public void deleteLoginInfo(HttpSession session) {
String loginInfoHKey = Config.getLoginSessionKey(session.getId());
if(redisTemplate.hasKey(loginInfoHKey)) {
redisTemplate.delete(loginInfoHKey);
redisTemplate.delete(Config.getLoginSessionKey(session.getId()));
}
}
/**
* 删除用户基本信息
* @param userId
*/
public void deleteUserInfo(String userId) {
String userInfoHKey = Config.getLoginUserKey(userId);
if(redisTemplate.hasKey(userInfoHKey)) {
redisTemplate.delete(userInfoHKey);
redisTemplate.delete(Config.getLoginUserKey(userId));
}
}
/**
* 批量删除缓存
* @param userIdList
*/
public void deleteTaskInfo(Collection<String> userIdList) {
if (!CollectionUtils.isEmpty(userIdList)) {
List<String> userInfoHKeys = new ArrayList<>();
//List<String> taskFileHKeys = new ArrayList<>();
userIdList.parallelStream().forEach(userId -> {
String userInfoHKey = Config.getLoginUserKey(userId);
//String taskFileHKey = Config.getTaskFileHKey(userId);
userInfoHKeys.add(userInfoHKey);
//taskFileHKeys.add(taskFileHKey);
});
redisTemplate.delete(userInfoHKeys);
//redisTemplate.delete(taskFileHKeys);
}
}
导入jar包中的StringRedisTemplate
@Autowired
private StringRedisTemplate redisTemplate;
导入jar包 spring-data-redis-1.6.2.RELEASE.jar