Skip to content

Commit 21ff684

Browse files
authored
Merge pull request #6016 from microsoft/fix-redis
fix errors in redis explorer.
2 parents 618f7e9 + fc9e4a8 commit 21ff684

File tree

3 files changed

+24
-19
lines changed
  • PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-redis/src/main/java/com/microsoft/azure/toolkit/intellij/redis/explorer
  • Utils

3 files changed

+24
-19
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-redis/src/main/java/com/microsoft/azure/toolkit/intellij/redis/explorer/RedisCacheExplorer.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Map;
2929
import java.util.Objects;
3030
import java.util.Set;
31+
import java.util.function.Function;
3132

3233
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START;
3334

@@ -37,7 +38,6 @@ public class RedisCacheExplorer extends BaseEditor {
3738
public static final String ID = "com.microsoft.intellij.helpers.rediscache.RedisCacheExplorer";
3839
public static final String INSIGHT_NAME = "AzurePlugin.IntelliJ.Editor.RedisCacheExplorer";
3940
private final RedisCache redis;
40-
private Jedis jedis;
4141

4242
private String currentCursor;
4343
private String lastChosenKey;
@@ -96,7 +96,7 @@ public RedisCacheExplorer(RedisCache redis, @Nonnull final VirtualFile virtualFi
9696
tblInnerValue.getTableHeader().setFont(valueFont);
9797
txtStringValue.setFont(valueFont);
9898
final DefaultTableCellRenderer cellRenderer = (DefaultTableCellRenderer) tblInnerValue.getTableHeader()
99-
.getDefaultRenderer();
99+
.getDefaultRenderer();
100100
cellRenderer.setHorizontalAlignment(JLabel.LEFT);
101101
pnlInnerValue.setBackground(lstKey.getBackground());
102102

@@ -119,7 +119,7 @@ public RedisCacheExplorer(RedisCache redis, @Nonnull final VirtualFile virtualFi
119119
RedisCacheExplorer.this.setWidgetEnableStatus(false);
120120
lastChosenKey = selectedKey;
121121
manager.runOnPooledThread(() -> {
122-
final Pair<String, ArrayList<String[]>> data = getValueByKey(this.jedis, selectedKey);
122+
final Pair<String, ArrayList<String[]>> data = doWithRedis(jedis -> getValueByKey(jedis, selectedKey));
123123
manager.runLater(() -> RedisCacheExplorer.this.showContent(data));
124124
});
125125
});
@@ -129,7 +129,8 @@ public RedisCacheExplorer(RedisCache redis, @Nonnull final VirtualFile virtualFi
129129
btnScanMore.addActionListener(event -> {
130130
RedisCacheExplorer.this.setWidgetEnableStatus(false);
131131
manager.runOnPooledThread(() -> {
132-
final ScanResult<String> r = this.jedis.scan(currentCursor, new ScanParams().match(txtKeyPattern.getText()).count(DEFAULT_KEY_COUNT));
132+
final ScanResult<String> r = doWithRedis(jedis -> jedis.scan(currentCursor, new ScanParams()
133+
.match(txtKeyPattern.getText()).count(DEFAULT_KEY_COUNT)));
133134
manager.runLater(() -> RedisCacheExplorer.this.showScanResult(r));
134135
});
135136
});
@@ -146,12 +147,17 @@ public RedisCacheExplorer(RedisCache redis, @Nonnull final VirtualFile virtualFi
146147
});
147148

148149
manager.runOnPooledThread(() -> {
149-
this.jedis = this.redis.getJedisPool().getResource();
150-
final int num = getDbNumber(this.jedis);
150+
final int num = doWithRedis(RedisCacheExplorer::getDbNumber);
151151
AzureTaskManager.getInstance().runLater(() -> this.renderDbCombo(num));
152152
});
153153
}
154154

155+
private <T> T doWithRedis(Function<Jedis, T> func) {
156+
try (final Jedis jedis = this.redis.getJedisPool().getResource()) {
157+
return func.apply(jedis);
158+
}
159+
}
160+
155161
@Nonnull
156162
@Override
157163
public JComponent getComponent() {
@@ -165,9 +171,6 @@ public String getName() {
165171
}
166172

167173
public void dispose() {
168-
if (Objects.nonNull(this.jedis)) {
169-
this.jedis.close();
170-
}
171174
}
172175

173176
public void renderDbCombo(int num) {
@@ -248,8 +251,10 @@ public void getKeyFail() {
248251
private void onDataBaseSelect() {
249252
final AzureTaskManager manager = AzureTaskManager.getInstance();
250253
manager.runOnPooledThread(() -> {
251-
this.jedis.select(cbDatabase.getSelectedIndex());
252-
final ScanResult<String> r = this.jedis.scan(SCAN_POINTER_START, new ScanParams().match(DEFAULT_SCAN_PATTERN).count(DEFAULT_KEY_COUNT));
254+
final ScanResult<String> r = doWithRedis(jedis -> {
255+
jedis.select(cbDatabase.getSelectedIndex());
256+
return jedis.scan(SCAN_POINTER_START, new ScanParams().match(DEFAULT_SCAN_PATTERN).count(DEFAULT_KEY_COUNT));
257+
});
253258
manager.runLater(() -> RedisCacheExplorer.this.showScanResult(r));
254259
});
255260
}
@@ -285,23 +290,23 @@ private void onBtnSearchClick() {
285290
if (Objects.equals(actionType, ACTION_GET)) {
286291
final AzureTaskManager manager = AzureTaskManager.getInstance();
287292
manager.runOnPooledThread(() -> {
288-
final boolean isExist = this.jedis.exists(key);
289-
final Pair<String, ArrayList<String[]>> result = isExist ? getValueByKey(this.jedis, key) : null;
293+
final Pair<String, ArrayList<String[]>> result = doWithRedis(jedis ->
294+
jedis.exists(key) ? getValueByKey(jedis, key) : Pair.of("", new ArrayList<>()));
290295
this.updateKeyList();
291296
this.showContent(result);
292297
});
293298
} else if (Objects.equals(actionType, ACTION_SCAN)) {
294299
final AzureTaskManager manager = AzureTaskManager.getInstance();
295300
manager.runOnPooledThread(() -> {
296-
final ScanResult<String> r = this.jedis.scan(SCAN_POINTER_START, new ScanParams().match(key).count(DEFAULT_KEY_COUNT));
301+
final ScanResult<String> r = doWithRedis(jedis -> jedis.scan(SCAN_POINTER_START, new ScanParams().match(key).count(DEFAULT_KEY_COUNT)));
297302
manager.runLater(() -> RedisCacheExplorer.this.showScanResult(r));
298303
});
299304
currentCursor = SCAN_POINTER_START;
300305
}
301306
lastChosenKey = "";
302307
}
303308

304-
private class ReadOnlyTableModel extends DefaultTableModel {
309+
private static class ReadOnlyTableModel extends DefaultTableModel {
305310
ReadOnlyTableModel(Object[][] data, String[] columnNames) {
306311
super(data, columnNames);
307312
}
@@ -355,7 +360,7 @@ private static Pair<String, ArrayList<String[]>> getValueByKey(Jedis jedis, Stri
355360
case "LIST":
356361
final long listLength = jedis.llen(key);
357362
final List<String> listVal = jedis.lrange(key, DEFAULT_RANGE_START,
358-
listLength < DEFAULT_VAL_COUNT ? listLength : DEFAULT_VAL_COUNT);
363+
listLength < DEFAULT_VAL_COUNT ? listLength : DEFAULT_VAL_COUNT);
359364
for (int i = 0; i < listVal.size(); i++) {
360365
columnData.add(new String[]{String.valueOf(i + 1), listVal.get(i)});
361366
}
@@ -369,7 +374,7 @@ private static Pair<String, ArrayList<String[]>> getValueByKey(Jedis jedis, Stri
369374
case "ZSET":
370375
final long zsetLength = jedis.zcard(key);
371376
final Set<Tuple> zsetVal = jedis.zrangeWithScores(key, DEFAULT_RANGE_START,
372-
zsetLength < DEFAULT_VAL_COUNT ? zsetLength : DEFAULT_VAL_COUNT);
377+
zsetLength < DEFAULT_VAL_COUNT ? zsetLength : DEFAULT_VAL_COUNT);
373378
for (final Tuple tuple : zsetVal) {
374379
columnData.add(new String[]{String.valueOf(tuple.getScore()), tuple.getElement()});
375380
}

Utils/azuretools-core/src/com/microsoft/azuretools/core/mvp/ui/rediscache/RedisScanResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class RedisScanResult {
1717

1818
public RedisScanResult(ScanResult<String> result) {
1919
this.keys = result.getResult();
20-
this.nextCursor = result.getStringCursor();
20+
this.nextCursor = result.getCursor();
2121
}
2222

2323
public String getNextCursor() {

Utils/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266
<dependency>
267267
<groupId>redis.clients</groupId>
268268
<artifactId>jedis</artifactId>
269-
<version>2.10.2</version>
269+
<version>3.6.3</version>
270270
</dependency>
271271
<dependency>
272272
<groupId>io.reactivex</groupId>

0 commit comments

Comments
 (0)