jedis scan实现keys功能

/**
     * key模糊查找
     *
     * @param keys 要查找的key 例: aaa* ,aaa开头的所有key
     * @return 匹配到的key集合
     */
    public Set<String> keys(String keys) {
        TreeSet<String> set = new TreeSet<>();
        try (Jedis jedis = getJedis()) {
            String cursor = ScanParams.SCAN_POINTER_START;
            ScanParams scanParams = new ScanParams().match(keys).count(10000);
            do {
                ScanResult<String> scan = jedis.scan(cursor, scanParams);
                set.addAll(scan.getResult());
                cursor = scan.getCursor();
            } while (!ScanParams.SCAN_POINTER_START.equals(cursor));
        } catch (Exception e) {
            log.error(e);
        }
        return set;
    }

 

posted on 2020-11-04 10:19  SunEn  阅读(365)  评论(0编辑  收藏  举报

导航