/** * Constructs a new {@link DefaultRedisSet} instance. * * @param key Redis key of this set. * @param operations {@link RedisOperations} for the value type of this set. */ public DefaultRedisSet(String key, RedisOperations<String, E> operations) { super(key, operations); boundSetOps = operations.boundSetOps(key); }
public void cleanExpiredSessions() { long now = System.currentTimeMillis(); long prevMin = roundDownMinute(now); if (logger.isDebugEnabled()) { logger.debug("Cleaning up sessions expiring at " + new Date(prevMin)); } String expirationKey = getExpirationKey(prevMin); Set<Object> sessionsToExpire = this.redis.boundSetOps(expirationKey).members(); this.redis.delete(expirationKey); for (Object session : sessionsToExpire) { String sessionKey = getSessionKey((String) session); touch(sessionKey); } }
@Override public RedisSet<E> diffAndStore(RedisSet<?> set, String destKey) { boundSetOps.diffAndStore(set.getKey(), destKey); return new DefaultRedisSet<>(boundSetOps.getOperations().boundSetOps(destKey)); }
private void cleanupPrincipalIndex(RedisSession session) { String sessionId = session.getId(); String principal = PRINCIPAL_NAME_RESOLVER.resolvePrincipal(session); if (principal != null) { this.sessionRedisOperations.boundSetOps(getPrincipalKey(principal)) .remove(sessionId); } }
@Override public RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey) { boundSetOps.diffAndStore(CollectionUtils.extractKeys(sets), destKey); return new DefaultRedisSet<>(boundSetOps.getOperations().boundSetOps(destKey)); }
@Override public RedisSet<E> unionAndStore(RedisSet<?> set, String destKey) { boundSetOps.unionAndStore(set.getKey(), destKey); return new DefaultRedisSet<>(boundSetOps.getOperations().boundSetOps(destKey)); }
@Override public RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey) { boundSetOps.unionAndStore(CollectionUtils.extractKeys(sets), destKey); return new DefaultRedisSet<>(boundSetOps.getOperations().boundSetOps(destKey)); }
@Override public RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey) { boundSetOps.intersectAndStore(set.getKey(), destKey); return new DefaultRedisSet<>(boundSetOps.getOperations().boundSetOps(destKey)); }
@Override public RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey) { boundSetOps.intersectAndStore(CollectionUtils.extractKeys(sets), destKey); return new DefaultRedisSet<>(boundSetOps.getOperations().boundSetOps(destKey)); }
public void onDelete(Session session) { long toExpire = roundUpToNextMinute(expiresInMillis(session)); String expireKey = getExpirationKey(toExpire); this.redis.boundSetOps(expireKey).remove(session.getId()); }
@Test public void onExpirationUpdatedRemovesOriginalExpirationTimeRoundedUp() throws Exception { long originalExpirationTimeInMs = ONE_MINUTE_AGO; long originalRoundedToNextMinInMs = RedisSessionExpirationPolicy .roundUpToNextMinute(originalExpirationTimeInMs); String originalExpireKey = this.policy .getExpirationKey(originalRoundedToNextMinInMs); this.policy.onExpirationUpdated(originalExpirationTimeInMs, this.session); // verify the original is removed verify(this.sessionRedisOperations).boundSetOps(originalExpireKey); verify(this.setOperations).remove("expires:" + this.session.getId()); }
@Test public void onExpirationUpdatedDoNotSendDeleteWhenExpirationTimeDoesNotChange() throws Exception { long originalExpirationTimeInMs = RedisSessionExpirationPolicy .expiresInMillis(this.session) - 10; long originalRoundedToNextMinInMs = RedisSessionExpirationPolicy .roundUpToNextMinute(originalExpirationTimeInMs); String originalExpireKey = this.policy .getExpirationKey(originalRoundedToNextMinInMs); this.policy.onExpirationUpdated(originalExpirationTimeInMs, this.session); // verify the original is not removed verify(this.sessionRedisOperations).boundSetOps(originalExpireKey); verify(this.setOperations, never()).remove("expires:" + this.session.getId()); }
@Test public void cleanupExpiredSessions() { String expiredId = "expired-id"; given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); Set<Object> expiredIds = new HashSet<>( Arrays.asList("expired-key1", "expired-key2")); given(this.boundSetOperations.members()).willReturn(expiredIds); this.redisRepository.cleanupExpiredSessions(); for (Object id : expiredIds) { String expiredKey = "spring:session:sessions:" + id; // https://github.com/spring-projects/spring-session/issues/93 verify(this.redisOperations).hasKey(expiredKey); } }
@Test public void flushModeSetMaxInactiveIntervalInSeconds() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.setRedisFlushMode(RedisFlushMode.IMMEDIATE); RedisSession session = this.redisRepository.createSession(); reset(this.boundHashOperations); session.setMaxInactiveInterval(Duration.ofSeconds(1)); verify(this.boundHashOperations).expire(anyLong(), any(TimeUnit.class)); }
@Test public void changeSessionIdWhenNotSaved() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); RedisSession createSession = this.redisRepository.createSession(); String originalId = createSession.getId(); String changeSessionId = createSession.changeSessionId(); this.redisRepository.save(createSession); verify(this.redisOperations, never()).rename(anyString(), anyString()); assertThat(originalId).isNotEqualTo(changeSessionId); assertThat(createSession.getId()).isEqualTo(createSession.getId()); }
@Test public void onExpirationUpdatedAddsExpirationTimeRoundedUp() throws Exception { long expirationTimeInMs = RedisSessionExpirationPolicy .expiresInMillis(this.session); long expirationRoundedUpInMs = RedisSessionExpirationPolicy .roundUpToNextMinute(expirationTimeInMs); String expectedExpireKey = this.policy.getExpirationKey(expirationRoundedUpInMs); this.policy.onExpirationUpdated(null, this.session); verify(this.sessionRedisOperations).boundSetOps(expectedExpireKey); verify(this.setOperations).add("expires:" + this.session.getId()); verify(this.setOperations).expire( this.session.getMaxInactiveInterval().plusMinutes(5).getSeconds(), TimeUnit.SECONDS); }
@Test public void saveExpired() { RedisSession session = this.redisRepository.new RedisSession(new MapSession()); session.setMaxInactiveInterval(Duration.ZERO); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); this.redisRepository.save(session); String id = session.getId(); verify(this.redisOperations, atLeastOnce()).delete(getKey("expires:" + id)); verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id)); }
@Test public void saveLastAccessChanged() { RedisSession session = this.redisRepository.new RedisSession( new MapSession(this.cached)); session.setLastAccessedTime(Instant.ofEpochMilli(12345678L)); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.save(session); assertThat(getDelta()) .isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, session.getLastAccessedTime().toEpochMilli())); }
@Test public void saveRemoveAttribute() { String attrName = "attrName"; RedisSession session = this.redisRepository.new RedisSession(new MapSession()); session.removeAttribute(attrName); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.save(session); assertThat(getDelta()).isEqualTo(map( RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), null)); }
@Test public void saveSetAttribute() { String attrName = "attrName"; RedisSession session = this.redisRepository.new RedisSession(new MapSession()); session.setAttribute(attrName, "attrValue"); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.save(session); assertThat(getDelta()).isEqualTo( map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName))); }