Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RedisItemWriter and RedisItemReader logic #4651

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,48 @@
* @param <K> type of keys
* @param <V> type of values
*/
public class RedisItemReader<K, V> implements ItemStreamReader<V> {
public class RedisItemReader<K, V> implements ItemStreamReader<K> {

private final RedisTemplate<K, V> redisTemplate;
public static class KeyValue<K, V> {

private final ScanOptions scanOptions;
K key;
V value;

private Cursor<K> cursor;
KeyValue(K key, V value) {
this.key = key;
this.value = value;
}
}

public RedisItemReader(RedisTemplate<K, V> redisTemplate, ScanOptions scanOptions) {
Assert.notNull(redisTemplate, "redisTemplate must not be null");
Assert.notNull(scanOptions, "scanOptions must no be null");
this.redisTemplate = redisTemplate;
this.scanOptions = scanOptions;
}
private final RedisTemplate<K, V> redisTemplate;

@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
this.cursor = this.redisTemplate.scan(this.scanOptions);
}
private final ScanOptions scanOptions;

@Override
public V read() throws Exception {
if (this.cursor.hasNext()) {
K nextKey = this.cursor.next();
return this.redisTemplate.opsForValue().get(nextKey);
}
else {
return null;
}
}
private Cursor<K> cursor;

@Override
public void close() throws ItemStreamException {
this.cursor.close();
}
public RedisItemReader(RedisTemplate<K, V> redisTemplate, ScanOptions scanOptions) {
Assert.notNull(redisTemplate, "redisTemplate must not be null");
Assert.notNull(scanOptions, "scanOptions must no be null");
this.redisTemplate = redisTemplate;
this.scanOptions = scanOptions;
}

@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
this.cursor = this.redisTemplate.scan(this.scanOptions);
}

@Override
public K read() throws Exception {
if (this.cursor.hasNext()) {
return this.cursor.next();
} else {
return null;
}
}

@Override
public void close() throws ItemStreamException {
this.cursor.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@
* @author Mahmoud Ben Hassine
* @since 5.1
*/
public class RedisItemWriter<K, T> extends KeyValueItemWriter<K, T> {
public class RedisItemWriter<T, K> extends KeyValueItemWriter<T, K> {

private RedisTemplate<K, T> redisTemplate;

@Override
protected void writeKeyValue(K key, T value) {
protected void writeKeyValue(T value, K key) {
if (this.delete) {
this.redisTemplate.delete(key);
}
else {
} else {
this.redisTemplate.opsForValue().set(key, value);
}
}
Expand All @@ -51,10 +50,10 @@ protected void init() {

/**
* Set the {@link RedisTemplate} to use.
*
* @param redisTemplate the template to use
*/
public void setRedisTemplate(RedisTemplate<K, T> redisTemplate) {
this.redisTemplate = redisTemplate;
}

}