/** * Constructs a new {@link DefaultRedisMap} instance. * * @param key Redis key of this map. * @param operations {@link RedisOperations} for this map. * @see RedisOperations#getHashKeySerializer() * @see RedisOperations#getValueSerializer() */ public DefaultRedisMap(String key, RedisOperations<String, ?> operations) { this.hashOps = operations.boundHashOps(key); }
/** * Constructs a new {@link RedisProperties} instance. * * @param key Redis key of this property map. * @param operations {@link RedisOperations} for this properties. * @see RedisOperations#getHashKeySerializer() * @see RedisOperations#getHashValueSerializer() */ public RedisProperties(String key, RedisOperations<String, ?> operations) { this(null, operations.<String, String> boundHashOps(key)); }
/** * Constructs a new {@link RedisProperties} instance. * * @param defaults default properties to apply, can be {@literal null}. * @param key Redis key of this property map. * @param operations {@link RedisOperations} for this properties. * @see RedisOperations#getHashKeySerializer() * @see RedisOperations#getHashValueSerializer() */ public RedisProperties(Properties defaults, String key, RedisOperations<String, ?> operations) { this(defaults, operations.boundHashOps(key)); }
/** * Gets the {@link BoundHashOperations} to operate on a {@link Session}. * @param sessionId the id of the {@link Session} to work with * @return the {@link BoundHashOperations} to operate on a {@link Session} */ private BoundHashOperations<Object, Object, Object> getSessionBoundHashOperations( String sessionId) { String key = getSessionKey(sessionId); return this.sessionRedisOperations.boundHashOps(key); }
@Test public void deleteNullSession() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); String id = "abc"; this.redisRepository.deleteById(id); verify(this.redisOperations, times(0)).delete(anyString()); verify(this.redisOperations, times(0)).delete(anyString()); }
@Test public void onExpirationUpdatedSetExpireSession() throws Exception { String sessionKey = this.policy.getSessionKey(this.session.getId()); this.policy.onExpirationUpdated(null, this.session); verify(this.sessionRedisOperations).boundHashOps(sessionKey); verify(this.hashOperations).expire( this.session.getMaxInactiveInterval().plusMinutes(5).getSeconds(), TimeUnit.SECONDS); }
@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 flushModeSetLastAccessedTime() { 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(); session.setLastAccessedTime(Instant.now()); Map<String, Object> delta = getDelta(2); assertThat(delta.size()).isEqualTo(1); assertThat(delta) .isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, session.getLastAccessedTime().toEpochMilli())); }
@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 changeSessionIdWhenSaved() { 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 session = this.redisRepository.new RedisSession(this.cached); session.setLastAccessedTime(session.getLastAccessedTime()); String originalId = session.getId(); String changeSessionId = session.changeSessionId(); this.redisRepository.save(session); verify(this.redisOperations, times(2)).rename(anyString(), anyString()); assertThat(originalId).isNotEqualTo(changeSessionId); assertThat(session.getId()).isEqualTo(session.getId()); }
@Test public void getSessionExpired() { String expiredId = "expired-id"; 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.findById(expiredId)).isNull(); }
@Test public void flushModeImmediateRemoveAttribute() { 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(); String attrName = "someAttribute"; session.removeAttribute(attrName); Map<String, Object> delta = getDelta(2); assertThat(delta.size()).isEqualTo(1); assertThat(delta).isEqualTo( map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName))); }
@Test public void flushModeImmediateSetAttribute() { 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(); String attrName = "someAttribute"; session.setAttribute(attrName, "someValue"); Map<String, Object> delta = getDelta(2); assertThat(delta.size()).isEqualTo(1); assertThat(delta).isEqualTo( map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName))); }
@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())); }
@Before public void setup() { RedisOperationsSessionRepository repository = new RedisOperationsSessionRepository( this.sessionRedisOperations); this.policy = new RedisSessionExpirationPolicy(this.sessionRedisOperations, repository::getExpirationsKey, repository::getSessionKey); this.session = new MapSession(); this.session.setLastAccessedTime(Instant.ofEpochMilli(1429116694675L)); this.session.setId("12345"); given(this.sessionRedisOperations.boundSetOps(anyString())) .willReturn(this.setOperations); given(this.sessionRedisOperations.boundHashOps(anyString())) .willReturn(this.hashOperations); given(this.sessionRedisOperations.boundValueOps(anyString())) .willReturn(this.valueOperations); }
@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(); }
@Test public void getSessionNotFound() { String id = "abc"; given(this.redisOperations.boundHashOps(getKey(id))) .willReturn(this.boundHashOperations); given(this.boundHashOperations.entries()).willReturn(map()); assertThat(this.redisRepository.findById(id)).isNull(); }
@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))); }