Skip to content

Commit

Permalink
Don't send expirations del events when expiration time does not change.
Browse files Browse the repository at this point in the history
Expiration time is rounded up, so in case of many request per second from a
single session we can skip sending of delete event to reduce the traffic.
This is good because actually at the moment the redis repository is subscribed
for this events, even though it ignores them.

Fix spring-projectsgh-315
  • Loading branch information
tsachev committed Nov 26, 2015
1 parent 5ca6377 commit d85624c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ public void onDelete(ExpiringSession session) {

public void onExpirationUpdated(Long originalExpirationTimeInMilli, ExpiringSession session) {
String keyToExpire = "expires:" + session.getId();
long toExpire = roundUpToNextMinute(expiresInMillis(session));

if(originalExpirationTimeInMilli != null) {
long originalRoundedUp = roundUpToNextMinute(originalExpirationTimeInMilli);
String expireKey = getExpirationKey(originalRoundedUp);
redis.boundSetOps(expireKey).remove(keyToExpire);
if(toExpire != originalRoundedUp) {
String expireKey = getExpirationKey(originalRoundedUp);
redis.boundSetOps(expireKey).remove(keyToExpire);
}
}

long toExpire = roundUpToNextMinute(expiresInMillis(session));

String expireKey = getExpirationKey(toExpire);
BoundSetOperations<Object, Object> expireOperations = redis.boundSetOps(expireKey);
expireOperations.add(keyToExpire);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void setup() {
RedisOperationsSessionRepository repository = new RedisOperationsSessionRepository(sessionRedisOperations);
policy = new RedisSessionExpirationPolicy(sessionRedisOperations, repository);
session = new MapSession();
session.setLastAccessedTime(1429116694665L);
session.setLastAccessedTime(1429116694675L);
session.setId("12345");

when(sessionRedisOperations.boundSetOps(anyString())).thenReturn(setOperations);
Expand All @@ -81,6 +81,19 @@ public void onExpirationUpdatedRemovesOriginalExpirationTimeRoundedUp() throws E
verify(setOperations).remove("expires:"+ session.getId());
}

@Test
public void onExpirationUpdatedDoNotSendDeleteWhenExpirationTimeDoesNotChange() throws Exception {
long originalExpirationTimeInMs = RedisSessionExpirationPolicy.expiresInMillis(session) - 10;
long originalRoundedToNextMinInMs = RedisSessionExpirationPolicy.roundUpToNextMinute(originalExpirationTimeInMs);
String originalExpireKey = policy.getExpirationKey(originalRoundedToNextMinInMs);

policy.onExpirationUpdated(originalExpirationTimeInMs, session);

// verify the original is not removed
verify(sessionRedisOperations).boundSetOps(originalExpireKey);
verify(setOperations, never()).remove("expires:"+ session.getId());
}

@Test
public void onExpirationUpdatedAddsExpirationTimeRoundedUp() throws Exception {
long expirationTimeInMs = RedisSessionExpirationPolicy.expiresInMillis(session);
Expand Down

0 comments on commit d85624c

Please sign in to comment.