下面列出了怎么用org.springframework.data.redis.core.ConvertingCursor的API类实例代码及写法,或者点击链接到github查看源代码。
public static Set<String> scan(RedisTemplate<String, ?> redisTemplate, String pattern, int count) {
ScanOptions scanOptions;
if (count > -1) {
scanOptions = ScanOptions.scanOptions().match(pattern).count(count).build();
} else {
scanOptions = ScanOptions.scanOptions().match(pattern).build();
}
ConvertingCursor<byte[], String> cursor = redisTemplate.executeWithStickyConnection((redisConnection) ->
new ConvertingCursor<>(redisConnection.scan(scanOptions),
new StringRedisSerializer()::deserialize)
);
if (cursor != null) {
Set<String> set = Sets.newHashSet();
cursor.forEachRemaining(set::add);
return set;
}
return Collections.emptySet();
}
@Override
public Cursor<V> scan(K key, final ScanOptions options) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Cursor<V>>() {
@Override
public Cursor<V> doInRedis(RedisConnection connection) throws DataAccessException {
return new ConvertingCursor<byte[], V>(connection.sScan(rawKey, options), new Converter<byte[], V>() {
@Override
public V convert(byte[] source) {
return deserializeValue(source);
}
});
}
}, true);
}
@Override
public Cursor<TypedTuple<V>> scan(K key, final ScanOptions options) {
final byte[] rawKey = rawKey(key);
Cursor<Tuple> cursor = execute(new RedisCallback<Cursor<Tuple>>() {
@Override
public Cursor<Tuple> doInRedis(RedisConnection connection) throws DataAccessException {
connection.select(dbIndex);
return connection.zScan(rawKey, options);
}
}, true);
return new ConvertingCursor<Tuple, TypedTuple<V>>(cursor, new Converter<Tuple, TypedTuple<V>>() {
@Override
public TypedTuple<V> convert(Tuple source) {
return deserializeTuple(source);
}
});
}
@Override
public Cursor<Entry<HK, HV>> scan(K key, final ScanOptions options) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Cursor<Map.Entry<HK, HV>>>() {
@Override
public Cursor<Entry<HK, HV>> doInRedis(RedisConnection connection) throws DataAccessException {
return new ConvertingCursor<Map.Entry<byte[], byte[]>, Map.Entry<HK, HV>>(connection.hScan(rawKey, options),
new Converter<Map.Entry<byte[], byte[]>, Map.Entry<HK, HV>>() {
@Override
public Entry<HK, HV> convert(final Entry<byte[], byte[]> source) {
return new Map.Entry<HK, HV>() {
@Override
public HK getKey() {
return deserializeHashKey(source.getKey());
}
@Override
public HV getValue() {
return deserializeHashValue(source.getValue());
}
@Override
public HV setValue(HV value) {
throw new UnsupportedOperationException("Values cannot be set when scanning through entries.");
}
};
}
});
}
}, true);
}