@Override public Mono<RedisSession> findById(String id) { String sessionKey = getSessionKey(id); // @formatter:off return this.sessionRedisOperations.opsForHash().entries(sessionKey) .collectMap((e) -> e.getKey().toString(), Map.Entry::getValue) .filter((map) -> !map.isEmpty()) .map(new SessionMapper(id)) .filter((session) -> !session.isExpired()) .map(RedisSession::new) .switchIfEmpty(Mono.defer(() -> deleteById(id).then(Mono.empty()))); // @formatter:on }
private Mono<Void> saveDelta() { if (this.delta.isEmpty()) { return Mono.empty(); } String sessionKey = getSessionKey(getId()); Mono<Boolean> update = ReactiveRedisOperationsSessionRepository.this.sessionRedisOperations .opsForHash().putAll(sessionKey, this.delta); Mono<Boolean> setTtl = ReactiveRedisOperationsSessionRepository.this.sessionRedisOperations .expire(sessionKey, getMaxInactiveInterval()); return update.and(setTtl).and((s) -> { this.delta.clear(); s.onComplete(); }).then(); }
@Test public void getSessionNotFound() { given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.entries(anyString())).willReturn(Flux.empty()); given(this.redisOperations.delete(anyString())).willReturn(Mono.just(0L)); StepVerifier.create(this.repository.findById("test")).verifyComplete(); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).entries(anyString()); verify(this.redisOperations).delete(anyString()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); }
@Test public void saveNewSession() { given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())) .willReturn(Mono.just(true)); StepVerifier .create(this.repository.createSession().doOnNext(this.repository::save)) .consumeNextWith((session) -> { verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); Map<String, Object> delta = this.delta.getAllValues().get(0); assertThat(delta.size()).isEqualTo(3); assertThat(delta.get( ReactiveRedisOperationsSessionRepository.CREATION_TIME_KEY)) .isEqualTo(session.getCreationTime().toEpochMilli()); assertThat(delta.get( ReactiveRedisOperationsSessionRepository.MAX_INACTIVE_INTERVAL_KEY)) .isEqualTo((int) Duration.ofSeconds( MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS) .getSeconds()); assertThat(delta.get( ReactiveRedisOperationsSessionRepository.LAST_ACCESSED_TIME_KEY)) .isEqualTo( session.getLastAccessedTime().toEpochMilli()); }).verifyComplete(); }
@Test public void saveLastAccessChanged() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())) .willReturn(Mono.just(true)); RedisSession session = this.repository.new RedisSession(this.cached); session.setLastAccessedTime(Instant.ofEpochMilli(12345678L)); Mono.just(session).subscribe(this.repository::save); verify(this.redisOperations).hasKey(anyString()); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); assertThat(this.delta.getAllValues().get(0)) .isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, session.getLastAccessedTime().toEpochMilli())); }
@Test public void saveSetAttribute() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())) .willReturn(Mono.just(true)); String attrName = "attrName"; RedisSession session = this.repository.new RedisSession(this.cached); session.setAttribute(attrName, "attrValue"); Mono.just(session).subscribe(this.repository::save); verify(this.redisOperations).hasKey(anyString()); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); assertThat(this.delta.getAllValues().get(0)).isEqualTo( map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName))); }
@Test public void saveRemoveAttribute() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())) .willReturn(Mono.just(true)); String attrName = "attrName"; RedisSession session = this.repository.new RedisSession(new MapSession()); session.removeAttribute(attrName); Mono.just(session).subscribe(this.repository::save); verify(this.redisOperations).hasKey(anyString()); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); assertThat(this.delta.getAllValues().get(0)).isEqualTo(map( RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), null)); }
@Test @SuppressWarnings("unchecked") public void getSessionExpired() { given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); Map map = map(ReactiveRedisOperationsSessionRepository.CREATION_TIME_KEY, 0L, ReactiveRedisOperationsSessionRepository.MAX_INACTIVE_INTERVAL_KEY, 1, ReactiveRedisOperationsSessionRepository.LAST_ACCESSED_TIME_KEY, Instant.now().minus(5, ChronoUnit.MINUTES).toEpochMilli()); given(this.hashOperations.entries(anyString())) .willReturn(Flux.fromIterable(map.entrySet())); given(this.redisOperations.delete(anyString())).willReturn(Mono.just(0L)); StepVerifier.create(this.repository.findById("test")).verifyComplete(); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).entries(anyString()); verify(this.redisOperations).delete(anyString()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); }
@Test @SuppressWarnings("unchecked") public void getSessionFound() { given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); String attribute1 = "attribute1"; String attribute2 = "attribute2"; verify(this.redisOperations).opsForHash(); verify(this.hashOperations).entries(anyString()); verifyZeroInteractions(this.redisOperations);
Mono<Post> findById(String id) { return template.<String, Post>opsForHash().get("posts", id); }
Mono<Boolean> deleteAll() { return template.<String, Post>opsForHash().delete("posts"); }
Mono<Void> deleteById(String id) { return template.<String, Post>opsForHash().remove("posts", id) .flatMap(p -> Mono.<Void>empty()); }
Flux<Post> findAll() { return template.<String, Post>opsForHash().values("posts"); }
Mono<Post> save(Post post) { if (post.getId() != null) { String id = UUID.randomUUID().toString(); post.setId(id); } return template.<String, Post>opsForHash().put("posts", post.getId(), post) .log() .map(p -> post); }