redis 根据key前缀搜索

/**
     * 通过 key 获取 value
     * pattern:message:xxx:yyy:id:
     * limit:每次限制筛选的数量,不建议 Integer.MAX_VALUE
     */
    public List<String> assembleScanValues(String pattern, Long limit) {
        List<String> values = assembleScanKeys(pattern, limit);
        return redisTemplate.opsForValue().multiGet(values).stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
    }

    /**
     * 组装 scan 的结果集
     */
    public List<String> assembleScanKeys(String pattern, Long limit) {
        HashSet<String> set = new HashSet<>();
        Cursor<String> cursor = scan(pattern, limit);
        while (cursor.hasNext()) {
            set.add(cursor.next());
        }
        try {
            cursor.close();
        } catch (Exception e) {
            log.error("关闭 redis connection 失败");
        }
        return set.stream().map(String::valueOf).collect(Collectors.toList());
    }

    /**
     * 自定义 redis scan 操作
     */
    private Cursor<String> scan(String pattern, Long limit) {
        ScanOptions options = ScanOptions.scanOptions().match(pattern).count(limit).build();
        RedisSerializer<String> redisSerializer = (RedisSerializer<String>) redisTemplate.getKeySerializer();
        return (Cursor) redisTemplate.executeWithStickyConnection(new RedisCallback() {
            @Override
            public Object doInRedis(RedisConnection redisConnection)
                    throws org.springframework.dao.DataAccessException {
                return new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize);
            }
        });
    }

    /**
     * 组装 scan 的结果集
     */
    public List<Map<String, String>> assembleScanKeyValues(String pattern, Long limit) {
        return (List<Map<String, String>>) redisTemplate.execute(new RedisCallback<List<Map<String, String>>>() {
            @Override
            public List<Map<String, String>> doInRedis(RedisConnection connection) throws DataAccessException {
                List<Map<String, String>> list = new ArrayList<Map<String, String>>();
                try (Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder()
                        .match(pattern).count(limit).build())) {
                    while (cursor.hasNext()) {
                        byte[] next = cursor.next();
                        byte[] bytes = connection.get(next);
                        String key = String.valueOf(redisTemplate.getValueSerializer().deserialize(next));
                        String value = String.valueOf(redisTemplate.getValueSerializer().deserialize(bytes));
                        Map map = new HashMap();
                        map.put(key, value);
                        list.add(map);
                    }
                } catch (IOException e) {
                    log.error(String.format("get cursor close {%s}", e));
                }
                return list;
            }
        });
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值