Java Code Examples for org.springframework.data.redis.connection.RedisConnection#scan()

The following examples show how to use org.springframework.data.redis.connection.RedisConnection#scan() . 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: RedisUtils.java    From sk-admin with Apache License 2.0 6 votes vote down vote up
/**
 * 查找匹配key
 *
 * @param pattern key
 * @return /
 */
public List<String> scan(String pattern) {
    ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
    RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
    RedisConnection rc = Objects.requireNonNull(factory).getConnection();
    Cursor<byte[]> cursor = rc.scan(options);
    List<String> result = new ArrayList<>();
    while (cursor.hasNext()) {
        result.add(new String(cursor.next()));
    }
    try {
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 2
Source File: RedisUtils.java    From yshopmall with Apache License 2.0 6 votes vote down vote up
/**
 * 查找匹配key
 * @param pattern key
 * @return /
 */
public List<String> scan(String pattern) {
    ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
    RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
    RedisConnection rc = Objects.requireNonNull(factory).getConnection();
    Cursor<byte[]> cursor = rc.scan(options);
    List<String> result = new ArrayList<>();
    while (cursor.hasNext()) {
        result.add(new String(cursor.next()));
    }
    try {
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 3
Source File: RedisUtils.java    From eladmin with Apache License 2.0 6 votes vote down vote up
/**
 * 查找匹配key
 *
 * @param pattern key
 * @return /
 */
public List<String> scan(String pattern) {
    ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
    RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
    RedisConnection rc = Objects.requireNonNull(factory).getConnection();
    Cursor<byte[]> cursor = rc.scan(options);
    List<String> result = new ArrayList<>();
    while (cursor.hasNext()) {
        result.add(new String(cursor.next()));
    }
    try {
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return result;
}
 
Example 4
Source File: IllustHistoryService.java    From Pixiv-Illustration-Collection-Backend with Apache License 2.0 6 votes vote down vote up
@Scheduled(cron = "0 10 2 * * ?")
@Transactional(rollbackFor = Exception.class)
public void clear() {
    System.out.println("开始清理收藏");
    //获取keylist
    final ScanOptions scanOptions = ScanOptions.scanOptions().match(RedisKeyConstant.ILLUST_BROWSING_HISTORY_REDIS_PRE + "*").build();
    RedisConnection connection = stringRedisTemplate.getConnectionFactory().getConnection();
    Cursor<byte[]> cursor = connection.scan(scanOptions);
    int i = 0;
    while (cursor.hasNext()) {
        connection.zRemRangeByScore(cursor.next(), LocalDateTime.now().plusDays(-3).toEpochSecond(ZoneOffset.of("+8")), -Integer.MAX_VALUE);
        i++;
    }
    illustHistoryMapper.deleteIllustHistory();
    illustHistoryMapper.tempToIllustHistory();
    illustHistoryMapper.truncateTemp();
}
 
Example 5
Source File: RedisUtils.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
/**
 * 分页查询 key
 *
 * @param patternKey key
 * @param page       页码
 * @param size       每页数目
 * @return /
 */
public List<String> findKeysForPage(String patternKey, int page, int size) {
    ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
    RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
    RedisConnection rc = Objects.requireNonNull(factory).getConnection();
    Cursor<byte[]> cursor = rc.scan(options);
    List<String> result = new ArrayList<>(size);
    int tmpIndex = 0;
    int fromIndex = page * size;
    int toIndex = page * size + size;
    while (cursor.hasNext()) {
        if (tmpIndex >= fromIndex && tmpIndex < toIndex) {
            result.add(new String(cursor.next()));
            tmpIndex++;
            continue;
        }
        // 获取到满足条件的数据后,就可以退出了
        if (tmpIndex >= toIndex) {
            break;
        }
        tmpIndex++;
        cursor.next();
    }
    try {
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 6
Source File: TestRedisTemplate.java    From ad with Apache License 2.0 5 votes vote down vote up
@Test
public void testScanCommand() {
    RedisConnection connection = Objects.requireNonNull(stringRedisTemplate.getConnectionFactory()).getConnection();
    Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().match("*").count(Integer.MAX_VALUE).build());
    while (cursor.hasNext()) {
        byte[] next = cursor.next();
        log.info(new String(next));
    }
}
 
Example 7
Source File: RedisUtil.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 分页获取指定格式key,使用 scan 命令代替 keys 命令,在大数据量的情况下可以提高查询效率
 *
 * @param patternKey  key格式
 * @param currentPage 当前页码
 * @param pageSize    每页条数
 * @return 分页获取指定格式key
 */
public PageResult<String> findKeysForPage(String patternKey, int currentPage, int pageSize) {
    ScanOptions options = ScanOptions.scanOptions()
            .match(patternKey)
            .build();
    RedisConnectionFactory factory = stringRedisTemplate.getConnectionFactory();
    RedisConnection rc = factory.getConnection();
    Cursor<byte[]> cursor = rc.scan(options);

    List<String> result = Lists.newArrayList();

    long tmpIndex = 0;
    int startIndex = (currentPage - 1) * pageSize;
    int end = currentPage * pageSize;
    while (cursor.hasNext()) {
        String key = new String(cursor.next());
        if (tmpIndex >= startIndex && tmpIndex < end) {
            result.add(key);
        }
        tmpIndex++;
    }

    try {
        cursor.close();
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        log.warn("Redis连接关闭异常,", e);
    }

    return new PageResult<>(result, tmpIndex);
}
 
Example 8
Source File: RedisUtils.java    From yshopmall with Apache License 2.0 5 votes vote down vote up
/**
 * 分页查询 key
 * @param patternKey key
 * @param page 页码
 * @param size 每页数目
 * @return /
 */
public List<String> findKeysForPage(String patternKey, int page, int size) {
    ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
    RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
    RedisConnection rc = Objects.requireNonNull(factory).getConnection();
    Cursor<byte[]> cursor = rc.scan(options);
    List<String> result = new ArrayList<>(size);
    int tmpIndex = 0;
    int fromIndex = page * size;
    int toIndex = page * size + size;
    while (cursor.hasNext()) {
        if (tmpIndex >= fromIndex && tmpIndex < toIndex) {
            result.add(new String(cursor.next()));
            tmpIndex++;
            continue;
        }
        // 获取到满足条件的数据后,就可以退出了
        if(tmpIndex >=toIndex) {
            break;
        }
        tmpIndex++;
        cursor.next();
    }
    try {
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 9
Source File: RedisUtils.java    From eladmin with Apache License 2.0 5 votes vote down vote up
/**
 * 分页查询 key
 *
 * @param patternKey key
 * @param page       页码
 * @param size       每页数目
 * @return /
 */
public List<String> findKeysForPage(String patternKey, int page, int size) {
    ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
    RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
    RedisConnection rc = Objects.requireNonNull(factory).getConnection();
    Cursor<byte[]> cursor = rc.scan(options);
    List<String> result = new ArrayList<>(size);
    int tmpIndex = 0;
    int fromIndex = page * size;
    int toIndex = page * size + size;
    while (cursor.hasNext()) {
        if (tmpIndex >= fromIndex && tmpIndex < toIndex) {
            result.add(new String(cursor.next()));
            tmpIndex++;
            continue;
        }
        // 获取到满足条件的数据后,就可以退出了
        if (tmpIndex >= toIndex) {
            break;
        }
        tmpIndex++;
        cursor.next();
    }
    try {
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return result;
}
 
Example 10
Source File: RedisUtil.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 分页获取指定格式key,使用 scan 命令代替 keys 命令,在大数据量的情况下可以提高查询效率
 *
 * @param patternKey  key格式
 * @param currentPage 当前页码
 * @param pageSize    每页条数
 * @return 分页获取指定格式key
 */
public PageResult<String> findKeysForPage(String patternKey, int currentPage, int pageSize) {
    ScanOptions options = ScanOptions.scanOptions()
            .match(patternKey)
            .build();
    RedisConnectionFactory factory = stringRedisTemplate.getConnectionFactory();
    RedisConnection rc = factory.getConnection();
    Cursor<byte[]> cursor = rc.scan(options);

    List<String> result = Lists.newArrayList();

    long tmpIndex = 0;
    int startIndex = (currentPage - 1) * pageSize;
    int end = currentPage * pageSize;
    while (cursor.hasNext()) {
        String key = new String(cursor.next());
        if (tmpIndex >= startIndex && tmpIndex < end) {
            result.add(key);
        }
        tmpIndex++;
    }

    try {
        cursor.close();
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        log.warn("Redis连接关闭异常,", e);
    }

    return new PageResult<>(result, tmpIndex);
}
 
Example 11
Source File: RedisUtil.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 分页获取指定格式key,使用 scan 命令代替 keys 命令,在大数据量的情况下可以提高查询效率
 *
 * @param patternKey  key格式
 * @param currentPage 当前页码
 * @param pageSize    每页条数
 * @return 分页获取指定格式key
 */
public PageResult<String> findKeysForPage(String patternKey, int currentPage, int pageSize) {
    ScanOptions options = ScanOptions.scanOptions()
            .match(patternKey)
            .build();
    RedisConnectionFactory factory = stringRedisTemplate.getConnectionFactory();
    RedisConnection rc = factory.getConnection();
    Cursor<byte[]> cursor = rc.scan(options);

    List<String> result = Lists.newArrayList();

    long tmpIndex = 0;
    int startIndex = (currentPage - 1) * pageSize;
    int end = currentPage * pageSize;
    while (cursor.hasNext()) {
        String key = new String(cursor.next());
        if (tmpIndex >= startIndex && tmpIndex < end) {
            result.add(key);
        }
        tmpIndex++;
    }

    try {
        cursor.close();
        RedisConnectionUtils.releaseConnection(rc, factory);
    } catch (Exception e) {
        log.warn("Redis连接关闭异常,", e);
    }

    return new PageResult<>(result, tmpIndex);
}