修改代码

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