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 scan(String match, Integer count) {
    List result = new ArrayList<>();
    try {
        Cursor 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;
}