DataHandler.java getKeysAll

This commit is contained in:
unurled 2024-03-14 12:13:33 +01:00
parent 106efc3ed8
commit 75d89e7974
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
2 changed files with 33 additions and 0 deletions

View file

@ -1,5 +1,6 @@
package me.unurled.sacredrealms.sr.data;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -35,4 +36,11 @@ public interface DataHandler {
* @return If the key exists
*/
boolean exists(@NotNull String key);
/**
* Get all keys from the data source
*
* @return All keys
*/
List<String> getKeysAll(@NotNull String key);
}

View file

@ -2,9 +2,13 @@ package me.unurled.sacredrealms.sr.data;
import static me.unurled.sacredrealms.sr.utils.Logger.error;
import java.util.ArrayList;
import java.util.List;
import me.unurled.sacredrealms.sr.SR;
import org.jetbrains.annotations.NotNull;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;
public class Redis implements DataHandler {
@ -69,4 +73,25 @@ public class Redis implements DataHandler {
public boolean exists(@NotNull String key) {
return client.exists(key);
}
/**
* Get all keys from the data source
*
* @param key
* @return All keys
*/
@Override
public List<String> getKeysAll(@NotNull String key) {
String cursor = "0";
ScanParams params = new ScanParams().match(key);
List<String> keys = new ArrayList<>();
do {
ScanResult<String> result = client.scan(cursor, params);
cursor = result.getCursor();
keys.addAll(result.getResult());
} while (!cursor.equals("0"));
return keys;
}
}