Example usage for org.springframework.data.redis.core Cursor next

List of usage examples for org.springframework.data.redis.core Cursor next

Introduction

In this page you can find the example usage for org.springframework.data.redis.core Cursor next.

Prototype

E next();

Source Link

Document

Returns the next element in the iteration.

Usage

From source file:io.gravitee.repository.redis.management.internal.impl.ApplicationRedisRepositoryImpl.java

@Override
public Set<RedisApplication> findByName(final String partialName) {

    List<String> matchedNames = redisTemplate.execute(new RedisCallback<List<String>>() {
        @Override// w  w  w .j av  a2 s  .  c  om
        public List<String> doInRedis(RedisConnection redisConnection) throws DataAccessException {
            ScanOptions options = ScanOptions.scanOptions()
                    .match(REDIS_KEY + ":search-by:name:*" + partialName.toUpperCase() + "*").build();
            Cursor<byte[]> cursor = redisConnection.scan(options);
            List<String> result = new ArrayList<>();
            if (cursor != null) {
                while (cursor.hasNext()) {
                    result.add(new String(cursor.next()));
                }
            }
            return result;
        }
    });

    if (matchedNames == null || matchedNames.isEmpty()) {
        return Collections.emptySet();
    }
    Set<Object> applicationIds = new HashSet<>();
    matchedNames.forEach(matchedName -> applicationIds.addAll(redisTemplate.opsForSet().members(matchedName)));

    return find(applicationIds.stream().map(Object::toString).collect(Collectors.toList()));
}

From source file:com.hurence.logisland.redis.service.RedisKeyValueCacheService.java

public long removeByPattern(final java.lang.String regex) throws IOException {
    return withConnection(redisConnection -> {
        long deletedCount = 0;
        final List<byte[]> batchKeys = new ArrayList<>();

        // delete keys in batches of 1000 using the cursor
        final Cursor<byte[]> cursor = redisConnection
                .scan(ScanOptions.scanOptions().count(100).match(regex).build());
        while (cursor.hasNext()) {
            batchKeys.add(cursor.next());

            if (batchKeys.size() == 1000) {
                deletedCount += redisConnection.del(getKeys(batchKeys));
                batchKeys.clear();//  ww w.j a  v  a 2  s.c o  m
            }
        }

        // delete any left-over keys if some were added to the batch but never reached 1000
        if (batchKeys.size() > 0) {
            deletedCount += redisConnection.del(getKeys(batchKeys));
            batchKeys.clear();
        }

        return deletedCount;
    });
}