Skip to content

Commit af76d0c

Browse files
committed
简化代码
1 parent 18ad13f commit af76d0c

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

hsweb-web-core/src/main/java/org/hsweb/web/core/session/redis/RedisHttpSessionManager.java

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ public class RedisHttpSessionManager extends AbstractHttpSessionManager {
2626

2727
@Override
2828
public Set<User> tryGetAllUser() {
29-
return (Set<User>) sessionRedisTemplate.execute((RedisCallback<Set<User>>) connection -> {
30-
Set<byte[]> keys = connection.keys("spring:session:sessions:*".getBytes());
31-
return keys.stream().map(key -> {
32-
String sessionId = new String(key).split("[:]")[3];
33-
ExpiringSession expiringSession = redisOperationsSessionRepository.getSession(sessionId);
34-
return (User) expiringSession.getAttribute("user");
35-
}).filter(user -> user != null).collect(Collectors.toSet());
36-
});
29+
return (Set<User>) sessionRedisTemplate.keys("spring:session:sessions:*")
30+
.stream().map(key -> {
31+
String sessionId = String.valueOf(key).split("[:]")[3];
32+
ExpiringSession expiringSession = redisOperationsSessionRepository.getSession(sessionId);
33+
return expiringSession.getAttribute("user");
34+
}).filter(user -> user != null).collect(Collectors.toSet());
3735
}
3836

3937
@Override
@@ -59,40 +57,33 @@ public String getSessionIdByUserId(String userId) {
5957
@Override
6058
public HttpSession getSessionBySessionId(String sessionId) {
6159
ExpiringSession redisSession = redisOperationsSessionRepository.getSession(sessionId);
62-
if(redisSession==null)return null;
60+
if (redisSession == null) return null;
6361
return new HttpSessionWrapper(redisSession);
6462
}
6563

6664
@Override
6765
public void removeUser(String userId) {
68-
sessionRedisTemplate.execute((RedisCallback) connection -> {
69-
String key = "http.session.user:" + userId;
70-
String sessionId = getSessionIdByUserId(userId);
71-
ExpiringSession redisSession = redisOperationsSessionRepository.getSession(sessionId);
72-
HttpSession session = new HttpSessionWrapper(redisSession);
73-
onUserLoginOut(userId, session);
74-
removeSession(sessionId);
75-
return connection.del(key.getBytes());
76-
});
66+
String key = "http.session.user:" + userId;
67+
String sessionId = getSessionIdByUserId(userId);
68+
ExpiringSession redisSession = redisOperationsSessionRepository.getSession(sessionId);
69+
HttpSession session = new HttpSessionWrapper(redisSession);
70+
onUserLoginOut(userId, session);
71+
removeSession(sessionId);
72+
sessionRedisTemplate.delete(key);
7773
}
7874

7975
@Override
8076
public void removeSession(String sessionId) {
81-
sessionRedisTemplate.execute((RedisCallback) connection ->
82-
connection.del(("spring:session:sessions:" + sessionId).getBytes())
83-
);
77+
sessionRedisTemplate.delete("spring:session:sessions:".concat(sessionId));
8478
}
8579

8680
@Override
8781
public void addUser(User user, HttpSession session) {
8882
removeUser(user.getId());
89-
sessionRedisTemplate.execute((RedisCallback) connection -> {
90-
String key = "http.session.user:" + user.getId();
91-
String value = session.getId();
92-
connection.set(key.getBytes(), value.getBytes());
93-
onUserLogin(user, session);
94-
return null;
95-
});
83+
String key = "http.session.user:" + user.getId();
84+
String value = session.getId();
85+
sessionRedisTemplate.opsForValue().set(key, value);
86+
onUserLogin(user, session);
9687
}
9788

9889
@Override
@@ -131,7 +122,7 @@ public Set<String> getSessionIdList() {
131122
@Override
132123
public boolean isLogin(String userId) {
133124
return (Boolean) sessionRedisTemplate.execute((RedisCallback) connection ->
134-
connection.exists(("http.session.user:" + userId).getBytes())
125+
connection.exists(("http.session.user:" + userId).getBytes())
135126
);
136127
}
137128

@@ -145,8 +136,8 @@ public void setSessionRedisTemplate(RedisTemplate sessionRedisTemplate) {
145136

146137
private final class HttpSessionWrapper implements HttpSession {
147138
private ExpiringSession session;
148-
private boolean invalidated;
149-
private boolean old;
139+
private boolean invalidated;
140+
private boolean old;
150141

151142
public HttpSessionWrapper(ExpiringSession session) {
152143
this.session = session;

0 commit comments

Comments
 (0)