public Long getSessionIdByUserId(Long systemId, Long customerUserId) {
if (customerUserId == null) {
LoggerHelper.info(this.getClass(), "custom user id is null.");
return DEFAULT_SESSION_ID;
}
Long sessionId = getSessionId(String.valueOf(customerUserId)); ------判断这用户是否有session的记录
if (sessionId == null) { --------创建一条新的记录放入数据库 同时反馈出sessionid
QuerySession session = new QuerySession(systemId, customerUserId);
querySessionDao.save(session);
sessionId = session.getId();
}
putSessionId(String.valueOf(customerUserId), sessionId, SESSION_EXPIRE_MINUTES); -----------比较意思的地方,把sessionid和超时时间放入redis
return sessionId;
}
放入redis
private void putSessionId(String userName, Long sessionId, int expireAfterMinutes) {
if (userName == null || sessionId == null) {
return;
}
ValueData valueData = new ValueData(sessionId, System.currentTimeMillis() + expireAfterMinutes * 60 * 1000); --------创建一条记录 sessionid 和 超时的时间点
redisTemplate.boundHashOps(QUERY_SESSION_KEY).put(userName, valueData);
}
之后取redis判断函数
private Long getSessionId(String userName) {
ValueData value = (ValueData) redisTemplate.boundHashOps(QUERY_SESSION_KEY).get(userName);
if (value == null) {
return null;
}
// check if expire
if (System.currentTimeMillis() > expireTimeMillion) { --------------验证是否过了这个时间点~~~
redisTemplate.boundHashOps(QUERY_SESSION_KEY).delete(userName);
return null;
} else {
return value.sessionId;
}
}
模拟的超时请求~~~