Java Code Examples for redis.clients.jedis.ScanResult#getStringCursor()

The following examples show how to use redis.clients.jedis.ScanResult#getStringCursor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisService.java    From seckill with Apache License 2.0 6 votes vote down vote up
public List<String> scanKeys(String key) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        List<String> keys = new ArrayList<String>();
        String cursor = "0";
        ScanParams sp = new ScanParams();
        sp.match("*" + key + "*");
        sp.count(100);
        do {
            ScanResult<String> ret = jedis.scan(cursor, sp);
            List<String> result = ret.getResult();
            if (result != null && result.size() > 0) {
                keys.addAll(result);
            }
            //再处理cursor
            cursor = ret.getStringCursor();
        } while (!cursor.equals("0"));
        return keys;
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
 
Example 2
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public ScanResult<Entry<String, Double>> zscan(final String key, final long cursor, final ScanParams params) {
    Assert.hasText(key);
    Assert.notNull(params);

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ScanResult<Tuple> res = jedis.zscan(key, String.valueOf(cursor), params);
        final List<Tuple> tuples = res.getResult();
        if (CollectionUtils.isEmpty(tuples)) {
            return new ScanResult<>(res.getStringCursor(), Collections.emptyList());
        }

        final List<Entry<String, Double>> newTuples = Lists.newArrayList();
        tuples.forEach(tuple -> newTuples.add(new AbstractMap.SimpleEntry<>(tuple.getElement(), tuple.getScore())));
        return new ScanResult<>(res.getStringCursor(), newTuples);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example 3
Source File: RedisClusterClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public ScanResult<Entry<String, Double>> zscan(final String key, final long cursor, final ScanParams params) {
    Assert.hasText(key);
    Assert.notNull(params);

    try {
        final ScanResult<Tuple> res = cluster.zscan(key, String.valueOf(cursor), params);
        final List<Tuple> tuples = res.getResult();
        if (CollectionUtils.isEmpty(tuples)) {
            return new ScanResult<>(res.getStringCursor(), Collections.emptyList());
        }

        final List<Entry<String, Double>> newTuples = Lists.newArrayList();
        tuples.forEach(tuple -> newTuples.add(new AbstractMap.SimpleEntry<>(tuple.getElement(), tuple.getScore())));
        return new ScanResult<>(res.getStringCursor(), newTuples);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    }
}
 
Example 4
Source File: KeyTests.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void scanTest() {
    final String prefix = "scan.test-";
    final Map<String, Object> map = Maps.newHashMap();
    for (int idx = 0; idx < 100; idx++) {
        map.put(prefix + idx, idx);
    }

    redisClient.set(map);

    final ScanParams params = new ScanParams().match(prefix + '*');
    long cursor = -1;
    while (cursor == -1 || cursor > 0) {
        final ScanResult<String> res = redisClient.scan(cursor == -1 ? 0 : cursor, params);
        final String nextCursor = res.getStringCursor();
        cursor = Long.parseLong(nextCursor);
        final List<String> keys = res.getResult();
        LOGGER.debug("{}", keys);
        if (cursor > 0) {
            Assert.assertTrue(keys.size() > 0);
        }
    }

    redisClient.del(map.keySet().toArray(new String[map.size()]));
}
 
Example 5
Source File: AbstractRedisClient.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ScanResult<T> scan(final long cursor, final ScanParams params, final TypeReference<T> type) {
    final ScanResult<String> res = scan(cursor, params);
    final String nextCursor = res.getStringCursor();
    final long next = Long.parseLong(nextCursor);
    if (next == 0) {
        return new ScanResult<>(START_CURSOR, Collections.emptyList());
    }

    final List<String> values = res.getResult();
    final List<T> newValues = values.stream().map(value -> JSON.parseObject(value, type)).collect(Collectors.toList());
    return new ScanResult<>(nextCursor, newValues);
}
 
Example 6
Source File: AbstractRedisClient.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ScanResult<Entry<String, T>> hscan(final String key, long cursor, final ScanParams params, final TypeReference<T> type) {
    final ScanResult<Entry<String, String>> result = hscan(key, cursor, params);
    final List<Entry<String, String>> entrys = result.getResult();
    if (CollectionUtils.isEmpty(entrys)) {
        return new ScanResult<>(result.getStringCursor(), Collections.emptyList());
    }

    final List<Entry<String, T>> newEntrys = Lists.newArrayList();
    entrys.forEach(entry -> newEntrys.add(new AbstractMap.SimpleEntry<>(entry.getKey(), JSON.parseObject(entry.getValue(), type))));
    return new ScanResult<>(result.getStringCursor(), newEntrys);
}
 
Example 7
Source File: AbstractRedisClient.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ScanResult<T> sscan(final String key, final long cursor, final ScanParams params, final TypeReference<T> type) {
    final ScanResult<String> result = sscan(key, cursor, params);
    final List<String> values = result.getResult();
    if (CollectionUtils.isEmpty(values)) {
        return new ScanResult<>(result.getStringCursor(), Collections.emptyList());
    }

    final List<T> newValues = Lists.newArrayList();
    values.forEach(value -> newValues.add(JSON.parseObject(value, type)));
    return new ScanResult<>(result.getStringCursor(), newValues);
}
 
Example 8
Source File: AbstractRedisClient.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ScanResult<Entry<T, Double>> zscan(final String key, final long cursor, final ScanParams params, final TypeReference<T> type) {
    final ScanResult<Entry<String, Double>> result = zscan(key, cursor, params);
    final List<Entry<String, Double>> entrys = result.getResult();
    if (CollectionUtils.isEmpty(entrys)) {
        return new ScanResult<>(result.getStringCursor(), Collections.emptyList());
    }

    final List<Entry<T, Double>> newEntrys = Lists.newArrayList();
    entrys.forEach(entry -> newEntrys.add(new AbstractMap.SimpleEntry<>(JSON.parseObject(entry.getKey(), type), entry.getValue())));
    return new ScanResult<>(result.getStringCursor(), newEntrys);
}