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

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

Introduction

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

Prototype

boolean hasNext();

Source Link

Document

Returns true if the iteration has more elements.

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/*from w  w  w .ja va2 s. co m*/
        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();/*from  ww  w  .j a va  2 s  .co  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;
    });
}