-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Session creation with pre-specified id. #1545
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
Changes from all commits
b7edb32
0b8049b
0249529
6c83991
12d7c2c
799278c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2014-2019 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 | ||
* | ||
* https://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 org.springframework.lang.Nullable; | ||
|
||
/** | ||
* Extends a basic {@link SessionRepository} to allow creating session with specific id. | ||
* | ||
* @param <S> the type of Session being managed by this | ||
* {@link CreateWithIdSessionRepository} | ||
* @author Jakub Maciej | ||
*/ | ||
public interface CreateWithIdSessionRepository<S extends Session> extends SessionRepository<S> { | ||
|
||
/** | ||
* Creates a new {@link Session} that is capable of being persisted by this | ||
* {@link SessionRepository}. | ||
* | ||
* <p> | ||
* This allows optimizations and customizations in how the {@link Session} is | ||
* persisted. For example, the implementation returned might keep track of the changes | ||
* ensuring that only the delta needs to be persisted on a save. | ||
* </p> | ||
* @param id a desired session id, if id is null - it will be generated by underlying | ||
* implementation | ||
* @return a new {@link Session} that is capable of being persisted by this | ||
* {@link SessionRepository} | ||
*/ | ||
S createSession(@Nullable String id); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.logging.Log; | ||
|
@@ -36,6 +37,7 @@ | |
import org.springframework.data.redis.core.RedisOperations; | ||
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializer; | ||
import org.springframework.session.CreateWithIdSessionRepository; | ||
import org.springframework.session.DelegatingIndexResolver; | ||
import org.springframework.session.FindByIndexNameSessionRepository; | ||
import org.springframework.session.FlushMode; | ||
|
@@ -244,10 +246,12 @@ | |
* | ||
* @author Rob Winch | ||
* @author Vedran Pavic | ||
* @author Jakub Maciej | ||
* @since 2.2.0 | ||
*/ | ||
public class RedisIndexedSessionRepository | ||
implements FindByIndexNameSessionRepository<RedisIndexedSessionRepository.RedisSession>, MessageListener { | ||
implements FindByIndexNameSessionRepository<RedisIndexedSessionRepository.RedisSession>, | ||
CreateWithIdSessionRepository<RedisIndexedSessionRepository.RedisSession>, MessageListener { | ||
|
||
private static final Log logger = LogFactory.getLog(RedisIndexedSessionRepository.class); | ||
|
||
|
@@ -489,7 +493,12 @@ public void deleteById(String sessionId) { | |
|
||
@Override | ||
public RedisSession createSession() { | ||
MapSession cached = new MapSession(); | ||
return createSession(null); | ||
} | ||
|
||
@Override | ||
public RedisSession createSession(final String id) { | ||
MapSession cached = Optional.ofNullable(id).map(MapSession::new).orElse(new MapSession()); | ||
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. It's better to use |
||
if (this.defaultMaxInactiveInterval != null) { | ||
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval)); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
|
@@ -39,6 +40,7 @@ | |
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.session.CreateWithIdSessionRepository; | ||
import org.springframework.session.DelegatingIndexResolver; | ||
import org.springframework.session.FindByIndexNameSessionRepository; | ||
import org.springframework.session.FlushMode; | ||
|
@@ -108,10 +110,12 @@ | |
* @author Tommy Ludwig | ||
* @author Mark Anderson | ||
* @author Aleksandar Stojsavljevic | ||
* @author Jakub Maciej | ||
* @since 2.2.0 | ||
*/ | ||
public class HazelcastIndexedSessionRepository | ||
implements FindByIndexNameSessionRepository<HazelcastIndexedSessionRepository.HazelcastSession>, | ||
CreateWithIdSessionRepository<HazelcastIndexedSessionRepository.HazelcastSession>, | ||
EntryAddedListener<String, MapSession>, EntryEvictedListener<String, MapSession>, | ||
EntryRemovedListener<String, MapSession> { | ||
|
||
|
@@ -234,7 +238,12 @@ public void setSaveMode(SaveMode saveMode) { | |
|
||
@Override | ||
public HazelcastSession createSession() { | ||
MapSession cached = new MapSession(); | ||
return createSession(null); | ||
} | ||
|
||
@Override | ||
public HazelcastSession createSession(final String id) { | ||
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. please make the |
||
MapSession cached = Optional.ofNullable(id).map(MapSession::new).orElse(new MapSession()); | ||
if (this.defaultMaxInactiveInterval != null) { | ||
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval)); | ||
} | ||
|
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.
final
is here redundant, it's not used in other methods.