@Override public Iterator<E> iterator() { Set<E> members = boundSetOps.members(); checkResult(members); return new DefaultRedisSetIterator(members.iterator()); }
@Override public Map<String, RedisSession> findByIndexNameAndIndexValue(String indexName, String indexValue) { if (!PRINCIPAL_NAME_INDEX_NAME.equals(indexName)) { return Collections.emptyMap(); } String principalKey = getPrincipalKey(indexValue); Set<Object> sessionIds = this.sessionRedisOperations.boundSetOps(principalKey) .members(); Map<String, RedisSession> sessions = new HashMap<>( sessionIds.size()); for (Object id : sessionIds) { RedisSession session = findById((String) id); if (session != null) { sessions.put(session.getId(), session); } } return sessions; }
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); } }
@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 findByPrincipalName() { Instant lastAccessed = Instant.now().minusMillis(10); Instant createdTime = lastAccessed.minusMillis(10); Duration maxInactive = Duration.ofHours(1); String sessionId = "some-id"; given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.boundSetOperations.members()) .willReturn(Collections.singleton(sessionId)); given(this.redisOperations.boundHashOps(getKey(sessionId))) .willReturn(this.boundHashOperations); Map map = map(RedisOperationsSessionRepository.CREATION_TIME_ATTR, createdTime.toEpochMilli(), RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, (int) maxInactive.getSeconds(), RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, lastAccessed.toEpochMilli()); given(this.boundHashOperations.entries()).willReturn(map); Map<String, RedisSession> sessionIdToSessions = this.redisRepository .findByIndexNameAndIndexValue( FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "principal"); assertThat(sessionIdToSessions).hasSize(1); RedisSession session = sessionIdToSessions.get(sessionId); assertThat(session).isNotNull(); assertThat(session.getId()).isEqualTo(sessionId); assertThat(session.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(lastAccessed.truncatedTo(ChronoUnit.MILLIS)); assertThat(session.getMaxInactiveInterval()).isEqualTo(maxInactive); assertThat(session.getCreationTime().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(createdTime.truncatedTo(ChronoUnit.MILLIS)); }
@Test public void findByPrincipalNameExpired() { String expiredId = "expired-id"; given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.boundSetOperations.members()) .willReturn(Collections.singleton(expiredId)); given(this.redisOperations.boundHashOps(getKey(expiredId))) .willReturn(this.boundHashOperations); Map map = map(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, 1, RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, Instant.now().minus(5, ChronoUnit.MINUTES).toEpochMilli()); given(this.boundHashOperations.entries()).willReturn(map); assertThat(this.redisRepository.findByIndexNameAndIndexValue( FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "principal")) .isEmpty(); }
private Set<String> getUserNavigationHistory(String user) { Set<String> userHistory = usersNavigatedItems.get(user); if (userHistory == null) { userHistory = redisTemplate.boundSetOps(buildKey(user)).members(); if (userHistory == null) { userHistory = new HashSet<String>(); } usersNavigatedItems.put(user, userHistory); } return userHistory; }
@Override public Iterator<E> iterator() { Set<E> members = boundSetOps.members(); checkResult(members); return new DefaultRedisSetIterator(members.iterator()); }
@Override public Iterator<E> iterator() { Set<E> members = boundSetOps.members(); checkResult(members); return new DefaultRedisSetIterator(members.iterator()); }
public Collection<String> getRegisteredConsumers(String queueName) { BoundSetOperations<String, String> ops = redisTemplate.boundSetOps(keyForRegisteredConsumers(queueName)); return ops.members(); }