-
Notifications
You must be signed in to change notification settings - Fork 1.1k
adding ReactiveFindByIndexNameSessionRepository #1205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ayudovin
wants to merge
7
commits into
spring-projects:master
from
ayudovin:add-reactiveFindByIndexNameSessionRepository
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8cf44d3
adding ReactiveFindByIndexNameSessionRepository
406e35b
adding tests for findByIndexNameAndIndexValue
6c84351
fixing check styles
31f2b28
fixing check styles
28dd1f0
fixing check styles
1d9ac51
Merge branch 'master' of https://github.com/spring-projects/spring-se…
6da75ad
fixing check styles
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
...e/src/main/java/org/springframework/session/ReactiveFindByIndexNameSessionRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2014-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.session; | ||
|
||
import java.util.Map; | ||
|
||
import reactor.core.publisher.Flux; | ||
import reactor.util.function.Tuple2; | ||
|
||
/** | ||
* Extends a basic {@link SessionRepository} to allow finding sessions by the specified | ||
* index name and index value. | ||
* | ||
* @param <S> the type of Session being managed by this | ||
* {@link ReactiveFindByIndexNameSessionRepository} | ||
* @author Artsiom Yudovin | ||
*/ | ||
public interface ReactiveFindByIndexNameSessionRepository<S extends Session> | ||
extends ReactiveSessionRepository<S> { | ||
|
||
/** | ||
* A session index that contains the current principal name (i.e. username). | ||
* <p> | ||
* It is the responsibility of the developer to ensure the index is populated since | ||
* Spring Session is not aware of the authentication mechanism being used. | ||
* | ||
* @since 1.1 | ||
*/ | ||
String PRINCIPAL_NAME_INDEX_NAME = ReactiveFindByIndexNameSessionRepository.class.getName() | ||
.concat(".PRINCIPAL_NAME_INDEX_NAME"); | ||
|
||
/** | ||
* Find a {@link Map} of the session id to the {@link Session} of all sessions that | ||
* contain the specified index name index value. | ||
* | ||
* @param indexName the name of the index (i.e. | ||
* {@link ReactiveFindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME}) | ||
* @param indexValue the value of the index to search for. | ||
* @return a {@code Map} (never {@code null}) of the session id to the {@code Session} | ||
* of all sessions that contain the specified index name and index value. If no | ||
* results are found, an empty {@code Map} is returned. | ||
*/ | ||
Flux<Tuple2<String, S>> findByIndexNameAndIndexValue(String indexName, String indexValue); | ||
|
||
/** | ||
* Find a {@link Map} of the session id to the {@link Session} of all sessions that | ||
* contain the index with the name | ||
* {@link ReactiveFindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME} and the | ||
* specified principal name. | ||
* | ||
* @param principalName the principal name | ||
* @return a {@code Map} (never {@code null}) of the session id to the {@code Session} | ||
* of all sessions that contain the specified principal name. If no results are found, | ||
* an empty {@code Map} is returned. | ||
* @since 2.1.0 | ||
*/ | ||
default Flux<Tuple2<String, S>> findByPrincipalName(String principalName) { | ||
|
||
return findByIndexNameAndIndexValue(PRINCIPAL_NAME_INDEX_NAME, principalName); | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,10 +24,15 @@ | |
import java.util.function.Function; | ||
|
||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.util.function.Tuple2; | ||
import reactor.util.function.Tuples; | ||
|
||
import org.springframework.data.redis.core.ReactiveRedisOperations; | ||
import org.springframework.session.FindByIndexNameSessionRepository; | ||
import org.springframework.session.MapSession; | ||
import org.springframework.session.ReactiveFindByIndexNameSessionRepository; | ||
import org.springframework.session.ReactiveSessionRepository; | ||
import org.springframework.session.Session; | ||
import org.springframework.util.Assert; | ||
|
@@ -37,10 +42,11 @@ | |
* {@link ReactiveRedisOperations}. | ||
* | ||
* @author Vedran Pavic | ||
* @author Artsiom Yudovin | ||
* @since 2.0 | ||
*/ | ||
public class ReactiveRedisOperationsSessionRepository implements | ||
ReactiveSessionRepository<ReactiveRedisOperationsSessionRepository.RedisSession> { | ||
ReactiveFindByIndexNameSessionRepository<ReactiveRedisOperationsSessionRepository.RedisSession> { | ||
|
||
/** | ||
* The default namespace for each key and channel in Redis used by Spring Session. | ||
|
@@ -192,6 +198,24 @@ private String getSessionKey(String sessionId) { | |
return this.namespace + "sessions:" + sessionId; | ||
} | ||
|
||
private String getPrincipalKey(String principalName) { | ||
return this.namespace + "index:" | ||
+ FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME + ":" | ||
+ principalName; | ||
} | ||
|
||
@Override | ||
public Flux<Tuple2<String, RedisSession>> findByIndexNameAndIndexValue(String indexName, String indexValue) { | ||
if (!PRINCIPAL_NAME_INDEX_NAME.equals(indexName)) { | ||
return Flux.empty(); | ||
} | ||
String principalKey = getPrincipalKey(indexValue); | ||
return this.sessionRedisOperations.opsForSet() | ||
.scan(principalKey) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid this isn't optimal, as you would be scanning for a match each time, rather having an index that's updated on write and have reads simple. |
||
.cast(String.class) | ||
.flatMap((id) -> this.findById(id).map((session) -> Tuples.of(id, session))); | ||
} | ||
|
||
/** | ||
* A custom implementation of {@link Session} that uses a {@link MapSession} as the | ||
* basis for its mapping. It keeps track of any attributes that have changed. When | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm not mistaken, the index key constructed here is only used for reading - what does actually write the index?