@Override
    public Stream<String> scan(final String pattern, final Long count) {
        var scanOptions = ScanOptions.scanOptions().match(pattern);
        if (count != null && count > 0) {
            scanOptions = scanOptions.count(count);
        }
        val connection = Objects.requireNonNull(getConnectionFactory()).getConnection();
        val cursor = connection.keyCommands().scan(scanOptions.build());
        var resultingStream = StreamSupport
            .stream(Spliterators.spliteratorUnknownSize(cursor, Spliterator.ORDERED), false)
            .onClose(() -> {
                IOUtils.closeQuietly(cursor);
                connection.close();
            })
            .map(key -> (String) getKeySerializer().deserialize(key))
            .distinct();
        if (count != null && count > 0) {
            resultingStream = resultingStream.limit(count);
        }
        return resultingStream;
    }
This is {@link DefaultCasRedisTemplate}.

@author Misagh Moayyed
@since 6.5.0
support/cas-server-support-redis-core/src/main/java/org/apereo/cas/redis/core/DefaultCasRedisTemplate.java