Redis 常用指令

命令

SCAN cursor [MATCH pattern] [COUNT count]

scan 0 MATCH key* COUNT 1

其他

  • SCAN 命令用于迭代当前数据库中的数据库键
  • SSCAN 命令用于迭代集合键(Set)中的元素
  • HSCAN 命令用于迭代哈希键(Hash)中的键值对
  • ZSCAN 命令用于迭代有序集合(Sorted Set)中的元素(包括元素成员和元素分值)

java代码

RedisTemplate

public List<String> scan(String match, Integer count) {
    List<String> result = new ArrayList<>();
    try {
        Cursor<byte[]> cursor = Objects.requireNonNull(redisTemplate.getConnectionFactory()).getConnection().scan(
                ScanOptions.scanOptions().match(match).count(count).build()
        );

        while (cursor.hasNext()) {
            result.add(new String(cursor.next()));
        }
        cursor.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}
posted @ 2022-05-27 14:42  itwetouch  阅读(33)  评论(0编辑  收藏  举报