List of usage examples for org.springframework.data.redis.core ScanOptions scanOptions
scanOptions
From source file:example.springdata.redis.commands.KeyOperationsTests.java
/** * Uses {@code SCAN} command for loading all matching keys. <br /> * {@code SCAN} uses a cursor on server side returning only a subset of the available data with the possibility to * ripple load further elements using the cursors position. <br /> * All keys will be loaded using <strong>multiple</strong> operations. *///from w ww . j a va 2 s . co m @Test public void iterateOverKeysMatchingPrefixUsingScanCommand() { generateRandomKeys(1000); Cursor<byte[]> cursor = this.connection.scan(ScanOptions.scanOptions().match(KEY_PATTERN).build()); printKeys(cursor); }
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/* ww w . ja va 2 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 w w w . java 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; }); }