@Override public UUID findSignUpCodeByUsername(String username) { String foundSignUpCodeKey = redisTemplate.execute(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { try (Cursor<byte[]> cursor = connection.scan( ScanOptions.scanOptions().match("signup:signupcode*").build())) { while (cursor.hasNext()) { String key = new String(cursor.next(), "UTF-8"); String usernameFromRedis = (String) redisTemplate.opsForHash().get(key, "username"); if (username.equals(usernameFromRedis)) { return key; } } } catch (IOException e) { throw new FlexPokerException("error in Redis"); } throw new FlexPokerException("could not find username in Redis"); } }); return UUID.fromString(foundSignUpCodeKey.split(":")[2]); }
@Override public Cursor<V> scan(K key, final ScanOptions options) { final byte[] rawKey = rawKey(key); return execute(new RedisCallback<Cursor<V>>() { @Override public Cursor<V> doInRedis(RedisConnection connection) throws DataAccessException { return new ConvertingCursor<byte[], V>(connection.sScan(rawKey, options), new Converter<byte[], V>() { @Override public V convert(byte[] source) { return deserializeValue(source); } }); } }, true); }
@Override public Cursor<TypedTuple<V>> scan(K key, final ScanOptions options) { final byte[] rawKey = rawKey(key); Cursor<Tuple> cursor = execute(new RedisCallback<Cursor<Tuple>>() { @Override public Cursor<Tuple> doInRedis(RedisConnection connection) throws DataAccessException { connection.select(dbIndex); return connection.zScan(rawKey, options); } }, true); return new ConvertingCursor<Tuple, TypedTuple<V>>(cursor, new Converter<Tuple, TypedTuple<V>>() { @Override public TypedTuple<V> convert(Tuple source) { return deserializeTuple(source); } }); }
@ManagedOperation(description = "Clear the store") @Override public void clear() { valueOperations.getOperations().execute(new RedisCallback<List<byte[]>>() { @Override public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException { List<byte[]> binaryKeys = new ArrayList<>(); Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().match("*" + createRedisKey("*")).build()); while (cursor.hasNext()) { byte[] key = cursor.next(); binaryKeys.add(key); } if (binaryKeys.size() > 0) { connection.del(binaryKeys.toArray(new byte[][]{})); } return binaryKeys; } }); }
/** * Uses {@code SCAN} command for loading all matching keys. <br /> * {@code SCAN} uses a cursor on server side returning only a subset of the available data with the possibility to * ripple load further elements using the cursors position. <br /> * All keys will be loaded using <strong>multiple</strong> operations. */ @Test public void iterateOverKeysMatchingPrefixUsingScanCommand() { generateRandomKeys(1000); Cursor<byte[]> cursor = this.connection.scan(ScanOptions.scanOptions().match(KEY_PATTERN).build()); printKeys(cursor); }
@Override public Cursor<Entry<HK, HV>> scan(K key, final ScanOptions options) { final byte[] rawKey = rawKey(key); return execute(new RedisCallback<Cursor<Map.Entry<HK, HV>>>() { @Override public Cursor<Entry<HK, HV>> doInRedis(RedisConnection connection) throws DataAccessException { return new ConvertingCursor<Map.Entry<byte[], byte[]>, Map.Entry<HK, HV>>(connection.hScan(rawKey, options), new Converter<Map.Entry<byte[], byte[]>, Map.Entry<HK, HV>>() { @Override public Entry<HK, HV> convert(final Entry<byte[], byte[]> source) { return new Map.Entry<HK, HV>() { @Override public HK getKey() { return deserializeHashKey(source.getKey()); } @Override public HV getValue() { return deserializeHashValue(source.getValue()); } @Override public HV setValue(HV value) { throw new UnsupportedOperationException("Values cannot be set when scanning through entries."); } }; } }); } }, true); }
@Override public Cursor<Entry<Object, Object>> scan(String key, ScanOptions options) { throw new UnsupportedOperationException("Unsupported method"); }
@Override public Cursor<byte[]> scan(ScanOptions options) { return redisConnection.scan(options); }
@Override public Cursor<byte[]> sScan(byte[] key, ScanOptions options) { return redisConnection.sScan(key, options); }
@Override public Cursor<Tuple> zScan(byte[] key, ScanOptions options) { return redisConnection.zScan(key, options); }
@Override public Cursor<Map.Entry<byte[], byte[]>> hScan(byte[] key, ScanOptions options) { return redisConnection.hScan(key, options); }
@Override public Cursor<V> scan(ScanOptions options) { return ops.scan(getKey(), options); }
@Override public Cursor<Entry<HK, HV>> scan(ScanOptions options) { return ops.scan(getKey(), options); }
@Override public Cursor<TypedTuple<V>> scan(ScanOptions options) { return ops.scan(getKey(), options); }
/** * 前缀 * @param prefix * @return * @since 1.0 */ public static ScanOptions getStartOptions(String prefix) { return ScanOptions.scanOptions().match("^" + prefix + ".*").build(); }
/** * 后缀 * @param suffix * @return * @since 1.0 */ public static ScanOptions getEndOptions(String suffix) { return ScanOptions.scanOptions().match(".*" + suffix + "$").build(); }