@@ -26,14 +26,12 @@ public class RedisHttpSessionManager extends AbstractHttpSessionManager {
26
26
27
27
@ Override
28
28
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 ());
37
35
}
38
36
39
37
@ Override
@@ -59,40 +57,33 @@ public String getSessionIdByUserId(String userId) {
59
57
@ Override
60
58
public HttpSession getSessionBySessionId (String sessionId ) {
61
59
ExpiringSession redisSession = redisOperationsSessionRepository .getSession (sessionId );
62
- if (redisSession == null )return null ;
60
+ if (redisSession == null ) return null ;
63
61
return new HttpSessionWrapper (redisSession );
64
62
}
65
63
66
64
@ Override
67
65
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 );
77
73
}
78
74
79
75
@ Override
80
76
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 ));
84
78
}
85
79
86
80
@ Override
87
81
public void addUser (User user , HttpSession session ) {
88
82
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 );
96
87
}
97
88
98
89
@ Override
@@ -131,7 +122,7 @@ public Set<String> getSessionIdList() {
131
122
@ Override
132
123
public boolean isLogin (String userId ) {
133
124
return (Boolean ) sessionRedisTemplate .execute ((RedisCallback ) connection ->
134
- connection .exists (("http.session.user:" + userId ).getBytes ())
125
+ connection .exists (("http.session.user:" + userId ).getBytes ())
135
126
);
136
127
}
137
128
@@ -145,8 +136,8 @@ public void setSessionRedisTemplate(RedisTemplate sessionRedisTemplate) {
145
136
146
137
private final class HttpSessionWrapper implements HttpSession {
147
138
private ExpiringSession session ;
148
- private boolean invalidated ;
149
- private boolean old ;
139
+ private boolean invalidated ;
140
+ private boolean old ;
150
141
151
142
public HttpSessionWrapper (ExpiringSession session ) {
152
143
this .session = session ;
0 commit comments