Spring-session: why RedisSessionExpirationPolicy#onDelete remove something not exist in the expirations SET?

Created on 8 Oct 2019  路  4Comments  路  Source: spring-projects/spring-session


the spring:session:expirations:${roundUpToNextMinute} redis SET is store value call keyToExpire:

        String expireKey = getExpirationKey(toExpire);
        BoundSetOperations<Object, Object> expireOperations = this.redis.boundSetOps(expireKey);
        expireOperations.add(keyToExpire);

the keyToExpire is a string like expires:${sessionId}
https://github.com/spring-projects/spring-session/blob/927008bdc89701555f2f8f900f4f898df23b1476/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionExpirationPolicy.java#L97

however, when delete the session, it actually remove the session id not expires:${sessionId}

    this.redis.boundSetOps(expireKey).remove(session.getId());

https://github.com/spring-projects/spring-session/blob/927008bdc89701555f2f8f900f4f898df23b1476/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionExpirationPolicy.java#L70

Is this a defact or My misunderstanding?

duplicate

Most helpful comment

Thank you for reply, I know Spring Redis will create 3 key in redis.

  1. the session, a redis HASH, which key is spring:session:sessions:${sessionId}
  2. the session expireKey, a redis STRING, which key is spring:session:sessions:expires:${sessionId}
  3. the set for tracking session expireKey, and then add expires:${sessionId} to the nearest minute set, a redis SET, which key is spring:session:expirations:${roundUpToNextMinute} .

when delete a session, the second key will be delete, right?

https://github.com/spring-projects/spring-session/blob/085554f56b0ee7c96d45a49daa1c664db5fb1184/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java#L484

And I found that SET still tracking the expireKey. Then I dont understand why not just remove expires:${sessionId} in the third key.

And I see the code, notice that RedisSessionExpirationPolicy#onDelete methond remove the ${sessionId} instead of expires:${sessionId} from the redis SET. but ${sessionId} will never add to the SET.
https://github.com/spring-projects/spring-session/blob/085554f56b0ee7c96d45a49daa1c664db5fb1184/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionExpirationPolicy.java#L70
So I create this issue. I think it's ok to clean up this element in the set. By change this line to something like:

this.redis.boundSetOps(expireKey).remove("expires:" + session.getId());

My English is not very good, hope you can understand what I mean.

All 4 comments

The behavior of session expiration handling for Redis session repository is described both in the RedisOperationsSessionRepository javadoc and reference manual.

Are you experiencing any particular issue here?

Thank you for reply, I know Spring Redis will create 3 key in redis.

  1. the session, a redis HASH, which key is spring:session:sessions:${sessionId}
  2. the session expireKey, a redis STRING, which key is spring:session:sessions:expires:${sessionId}
  3. the set for tracking session expireKey, and then add expires:${sessionId} to the nearest minute set, a redis SET, which key is spring:session:expirations:${roundUpToNextMinute} .

when delete a session, the second key will be delete, right?

https://github.com/spring-projects/spring-session/blob/085554f56b0ee7c96d45a49daa1c664db5fb1184/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java#L484

And I found that SET still tracking the expireKey. Then I dont understand why not just remove expires:${sessionId} in the third key.

And I see the code, notice that RedisSessionExpirationPolicy#onDelete methond remove the ${sessionId} instead of expires:${sessionId} from the redis SET. but ${sessionId} will never add to the SET.
https://github.com/spring-projects/spring-session/blob/085554f56b0ee7c96d45a49daa1c664db5fb1184/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionExpirationPolicy.java#L70
So I create this issue. I think it's ok to clean up this element in the set. By change this line to something like:

this.redis.boundSetOps(expireKey).remove("expires:" + session.getId());

My English is not very good, hope you can understand what I mean.

@vpavic please take a look

Thanks for the report @Alwayswithme.
I'm going to close this issue as a duplicate of gh-585.
Please follow gh-585 for updates.

Was this page helpful?
0 / 5 - 0 ratings