/** * By trying to access the session we only trigger a deletion if it the TTL is * expired. This is done to handle * https://github.com/spring-projects/spring-session/issues/93 * * @param key the key */ private void touch(String key) { this.redis.hasKey(key); }
@Override public void rename(K newKey) { if (ops.hasKey(key)) { ops.rename(key, newKey); } key = newKey; } }
@SuppressWarnings("unchecked") public Object execute(RedisOperations operations) throws DataAccessException { do { operations.watch(key); if (operations.hasKey(key)) { operations.multi(); operations.rename(key, newKey); } else { operations.multi(); } } while (operations.exec() == null); return null; } });
@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); } }
@Override public void rename(K newKey) { if (ops.hasKey(key)) { ops.rename(key, newKey); } key = newKey; } }
@Override public void rename(K newKey) { if (ops.hasKey(key)) { ops.rename(key, newKey); } key = newKey; } }
/** * 判断key下是否有值 * * @param key 判断的key */ public Boolean hasKey(String key) { return hashOperations.getOperations().hasKey(key); } }
@SuppressWarnings("unchecked") public Object execute(RedisOperations operations) throws DataAccessException { do { operations.watch(key); if (operations.hasKey(key)) { operations.multi(); operations.rename(key, newKey); } else { operations.multi(); } } while (operations.exec() == null); return null; } });
@SuppressWarnings("unchecked") public Object execute(RedisOperations operations) throws DataAccessException { do { operations.watch(key); if (operations.hasKey(key)) { operations.multi(); operations.rename(key, newKey); } else { operations.multi(); } } while (operations.exec() == null); return null; } });