@@ -418,7 +418,7 @@ public RedisOperations<Object, Object> getSessionRedisOperations() {
418
418
419
419
@ Override
420
420
public void save (RedisSession session ) {
421
- session .saveDelta ();
421
+ session .save ();
422
422
if (session .isNew ()) {
423
423
String sessionCreatedKey = getSessionCreatedChannel (session .getId ());
424
424
this .sessionRedisOperations .convertAndSend (sessionCreatedKey , session .delta );
@@ -516,12 +516,13 @@ public void deleteById(String sessionId) {
516
516
517
517
@ Override
518
518
public RedisSession createSession () {
519
- RedisSession redisSession = new RedisSession ();
520
- if (this .defaultMaxInactiveInterval != null ) {
521
- redisSession .setMaxInactiveInterval (
522
- Duration .ofSeconds (this .defaultMaxInactiveInterval ));
523
- }
524
- return redisSession ;
519
+ Duration maxInactiveInterval = Duration
520
+ .ofSeconds ((this .defaultMaxInactiveInterval != null )
521
+ ? this .defaultMaxInactiveInterval
522
+ : MapSession .DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS );
523
+ RedisSession session = new RedisSession (maxInactiveInterval );
524
+ session .flushImmediateIfNecessary ();
525
+ return session ;
525
526
}
526
527
527
528
@ Override
@@ -711,14 +712,17 @@ final class RedisSession implements Session {
711
712
/**
712
713
* Creates a new instance ensuring to mark all of the new attributes to be
713
714
* persisted in the next save operation.
715
+ *
716
+ * @param maxInactiveInterval the amount of time that the {@link Session} should
717
+ * be kept alive between client requests.
714
718
*/
715
- RedisSession () {
719
+ RedisSession (Duration maxInactiveInterval ) {
716
720
this (new MapSession ());
721
+ this .cached .setMaxInactiveInterval (maxInactiveInterval );
717
722
this .delta .put (CREATION_TIME_ATTR , getCreationTime ().toEpochMilli ());
718
723
this .delta .put (MAX_INACTIVE_ATTR , (int ) getMaxInactiveInterval ().getSeconds ());
719
724
this .delta .put (LAST_ACCESSED_ATTR , getLastAccessedTime ().toEpochMilli ());
720
725
this .isNew = true ;
721
- this .flushImmediateIfNecessary ();
722
726
}
723
727
724
728
/**
@@ -808,7 +812,7 @@ public void removeAttribute(String attributeName) {
808
812
809
813
private void flushImmediateIfNecessary () {
810
814
if (RedisOperationsSessionRepository .this .redisFlushMode == RedisFlushMode .IMMEDIATE ) {
811
- saveDelta ();
815
+ save ();
812
816
}
813
817
}
814
818
@@ -817,16 +821,20 @@ private void putAndFlush(String a, Object v) {
817
821
this .flushImmediateIfNecessary ();
818
822
}
819
823
824
+ private void save () {
825
+ saveChangeSessionId ();
826
+ saveDelta ();
827
+ }
828
+
820
829
/**
821
830
* Saves any attributes that have been changed and updates the expiration of this
822
831
* session.
823
832
*/
824
833
private void saveDelta () {
825
- String sessionId = getId ();
826
- saveChangeSessionId (sessionId );
827
834
if (this .delta .isEmpty ()) {
828
835
return ;
829
836
}
837
+ String sessionId = getId ();
830
838
getSessionBoundHashOperations (sessionId ).putAll (this .delta );
831
839
String principalSessionKey = getSessionAttrNameKey (
832
840
FindByIndexNameSessionRepository .PRINCIPAL_NAME_INDEX_NAME );
@@ -859,30 +867,32 @@ private void saveDelta() {
859
867
.onExpirationUpdated (originalExpiration , this );
860
868
}
861
869
862
- private void saveChangeSessionId (String sessionId ) {
863
- if (!sessionId .equals (this .originalSessionId )) {
864
- if (!isNew ()) {
865
- String originalSessionIdKey = getSessionKey (this .originalSessionId );
866
- String sessionIdKey = getSessionKey (sessionId );
867
- try {
868
- RedisOperationsSessionRepository .this .sessionRedisOperations
869
- .rename (originalSessionIdKey , sessionIdKey );
870
- }
871
- catch (NonTransientDataAccessException ex ) {
872
- handleErrNoSuchKeyError (ex );
873
- }
874
- String originalExpiredKey = getExpiredKey (this .originalSessionId );
875
- String expiredKey = getExpiredKey (sessionId );
876
- try {
877
- RedisOperationsSessionRepository .this .sessionRedisOperations
878
- .rename (originalExpiredKey , expiredKey );
879
- }
880
- catch (NonTransientDataAccessException ex ) {
881
- handleErrNoSuchKeyError (ex );
882
- }
870
+ private void saveChangeSessionId () {
871
+ String sessionId = getId ();
872
+ if (sessionId .equals (this .originalSessionId )) {
873
+ return ;
874
+ }
875
+ if (!isNew ()) {
876
+ String originalSessionIdKey = getSessionKey (this .originalSessionId );
877
+ String sessionIdKey = getSessionKey (sessionId );
878
+ try {
879
+ RedisOperationsSessionRepository .this .sessionRedisOperations
880
+ .rename (originalSessionIdKey , sessionIdKey );
881
+ }
882
+ catch (NonTransientDataAccessException ex ) {
883
+ handleErrNoSuchKeyError (ex );
884
+ }
885
+ String originalExpiredKey = getExpiredKey (this .originalSessionId );
886
+ String expiredKey = getExpiredKey (sessionId );
887
+ try {
888
+ RedisOperationsSessionRepository .this .sessionRedisOperations
889
+ .rename (originalExpiredKey , expiredKey );
890
+ }
891
+ catch (NonTransientDataAccessException ex ) {
892
+ handleErrNoSuchKeyError (ex );
883
893
}
884
- this .originalSessionId = sessionId ;
885
894
}
895
+ this .originalSessionId = sessionId ;
886
896
}
887
897
888
898
private void handleErrNoSuchKeyError (NonTransientDataAccessException ex ) {
0 commit comments