修改代码

parent 61478609
......@@ -18,9 +18,11 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.persistence.Cacheable;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.TimeUnit;
......@@ -41,7 +43,7 @@ public class SessionServiceImpl implements ISessionService {
AuthBean authBean = new AuthBean();
LoginProperties properties = new LoginProperties();
properties.setAppChannel(appChannel);
if(null!=merchant){
if (null != merchant) {
properties.setMerchantName(merchant.getName());
}
properties.setChannelId(channelId);
......@@ -133,27 +135,29 @@ public class SessionServiceImpl implements ISessionService {
String key = generateLoginPropertiesKey(sessionValue.getUser().getId(), sessionValue.getLoginProperties());
stringRedisTemplate.opsForValue().set(key, token, Constants.Session.ONE_DAY, TimeUnit.SECONDS);
setUserIdTokenKeys(sessionValue, key);
setUserIdTokenKeys(sessionValue.getUser().getId(), key);
}
/**
* 设置用户token集合方便注销使用
* @param sessionValue
*
* @param userId
* @param key
*/
private void setUserIdTokenKeys(SessionValue sessionValue, String key) {
String useIdKeys = stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSION_KEY_SET + sessionValue.getUser().getId());
Set useIdKeySet = null;
if (StringUtils.isNotEmpty(useIdKeys)) {
useIdKeySet = JSON.parseObject(useIdKeys, Set.class);
} else {
useIdKeySet = new HashSet();
private void setUserIdTokenKeys(long userId, String key) {
if (0L != userId) {
try {
stringRedisTemplate.opsForSet().add(Constants.Session.USER_SESSION_KEY_SET + userId, key);
stringRedisTemplate.expire(Constants.Session.USER_SESSION_KEY_SET + userId, Constants.Session.ONE_DAY, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("存储用户注销件失败,userId:{},Exception:{}", userId, e);
}
}
useIdKeySet.add(key);
stringRedisTemplate.opsForValue().set(Constants.Session.USER_SESSION_KEY_SET + sessionValue.getUser().getId(), JSONObject.toJSONString(useIdKeySet), Constants.Session.ONE_DAY, TimeUnit.SECONDS);
}
@Override
public SessionStruct findSessionBySessionId(String sessionId) {
String sessionValue = findSessionValueBySessionId(sessionId);
......@@ -177,18 +181,10 @@ public class SessionServiceImpl implements ISessionService {
@Override
public void deleteByUserId(long userId) {
// String pattern = Constants.Session.USER_SESSION_ID_CACHE + ":" + String.valueOf(userId) + ":*";
// Set<String> keys = stringRedisTemplate.keys(pattern);
// if (!CollectionUtils.isEmpty(keys)) {
// log.info("删除用户userId={}的缓存信息,个数:{},keys={}", userId,
// keys.size(),
// Joiner.on(",").join(keys));
// }
String useIdKeys = stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSION_KEY_SET + userId);
if (StringUtils.isNotEmpty(useIdKeys)) {
Set useIdKeySet = JSON.parseObject(useIdKeys, Set.class);
useIdKeySet.forEach(key -> {
Set useIdKeys = stringRedisTemplate.opsForSet().members(Constants.Session.USER_SESSION_KEY_SET + userId);
if (!CollectionUtils.isEmpty(useIdKeys)) {
useIdKeys.forEach(key -> {
stringRedisTemplate.delete(String.valueOf(key));
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment