Skip to content

Commit 02f6eb3

Browse files
committed
✨ feat: add a lot of functions wrapper when executing command #5
1 parent 05eae6e commit 02f6eb3

File tree

3 files changed

+646
-40
lines changed

3 files changed

+646
-40
lines changed

plugin/src/main/groovy/org/redis4j/common/Redis4j.java

Lines changed: 196 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,24 @@ public static <T> void setCacheMap(String key, Map<String, T> map) {
814814
e.setCacheMap(dispatch(), key, map);
815815
}
816816

817+
/**
818+
* Stores a map of objects in Redis using the given RedisTemplate and key, with an optional callback
819+
* for handling exceptions. If the dispatch template is null, the map is empty, or the key is empty or blank,
820+
* the method does nothing.
821+
*
822+
* @param key The key under which the map will be stored.
823+
* @param map The map of objects to be stored in Redis.
824+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
825+
* @param <T> The type of objects in the map.
826+
*/
827+
public static <T> void setCacheMap(String key, Map<String, T> map, Redis4jWrapCallback callback) {
828+
Redis4jService e = jProvider();
829+
if (e == null) {
830+
return;
831+
}
832+
e.setCacheMap(dispatch(), key, map, callback);
833+
}
834+
817835
/**
818836
* Stores a map of objects in Redis using the given RedisTemplate and key.
819837
* If the dispatch template is null, the map is empty, or the key is empty or blank,
@@ -848,6 +866,26 @@ public static <T> void setCacheMapSafe(String key, Pair<String, T>... map) {
848866
e.setCacheMapSafe(dispatch(), key, map);
849867
}
850868

869+
/**
870+
* Stores a map of objects in Redis using the given RedisTemplate and key, with an optional callback
871+
* for handling exceptions. This method allows for a variable number of key-value pairs to be provided as arguments.
872+
* If the dispatch template is null, the map is empty, or the key is empty or blank,
873+
* the method does nothing.
874+
*
875+
* @param key The key under which the map will be stored.
876+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
877+
* @param map A variable number of key-value pairs to be stored in Redis.
878+
* @param <T> The type of objects in the map.
879+
*/
880+
@SuppressWarnings({"unused", "unchecked"})
881+
public static <T> void setCacheMapSafe(String key, Redis4jWrapCallback callback, Pair<String, T>... map) {
882+
Redis4jService e = jProvider();
883+
if (e == null) {
884+
return;
885+
}
886+
e.setCacheMapSafe(dispatch(), key, callback, map);
887+
}
888+
851889
/**
852890
* Stores a map of objects in Redis using the given RedisTemplate and key.
853891
* If the dispatch template is null, the map is empty, or the key is empty or blank,
@@ -881,6 +919,23 @@ public static Map<Object, Object> getCacheMap(String key) {
881919
return e.getCacheMap(dispatch(), key);
882920
}
883921

922+
/**
923+
* Retrieves a map of objects from Redis using the given RedisTemplate and key, with an optional callback
924+
* for handling exceptions. If the dispatch template is null or the key is empty or blank,
925+
* the method returns an empty map.
926+
*
927+
* @param key The key under which the map is stored.
928+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
929+
* @return A map of objects retrieved from Redis, or an empty map if the dispatch template or key is invalid.
930+
*/
931+
public static Map<Object, Object> getCacheMap(String key, Redis4jWrapCallback callback) {
932+
Redis4jService e = jProvider();
933+
if (e == null) {
934+
return Collections.emptyMap();
935+
}
936+
return e.getCacheMap(dispatch(), key, callback);
937+
}
938+
884939
/**
885940
* Retrieves a map of objects from Redis using the given RedisTemplate and key.
886941
* If the dispatch template is null or the key is empty or blank,
@@ -913,6 +968,25 @@ public static <T> void setCacheMapValue(String key, String hKey, T value) {
913968
e.setCacheMapValue(dispatch(), key, hKey, value);
914969
}
915970

971+
/**
972+
* Sets a value in a Redis hash using the given RedisTemplate, key, and hash key, with an optional callback
973+
* for handling exceptions. If the dispatch template is null, the value is null, or the key or hash key is empty or blank,
974+
* the method returns without performing any operation.
975+
*
976+
* @param key The key under which the hash is stored.
977+
* @param hKey The hash key under which the value is stored.
978+
* @param value The value to be set in the hash.
979+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
980+
* @param <T> The type of the value being set.
981+
*/
982+
public static <T> void setCacheMapValue(String key, String hKey, T value, Redis4jWrapCallback callback) {
983+
Redis4jService e = jProvider();
984+
if (e == null) {
985+
return;
986+
}
987+
e.setCacheMapValue(dispatch(), key, hKey, value, callback);
988+
}
989+
916990
/**
917991
* Sets a value in a Redis hash using the given RedisTemplate, key, and hash key.
918992
* If the dispatch template is null, the value is null, or the key or hash key is empty or blank,
@@ -947,6 +1021,25 @@ public static <T> T getCacheMapValue(String key, String hKey) {
9471021
return e.getCacheMapValue(dispatch(), key, hKey);
9481022
}
9491023

1024+
/**
1025+
* Retrieves a value from a Redis hash using the given RedisTemplate, key, and hash key, with an optional callback
1026+
* for handling exceptions. If the dispatch template is null, or if the key or hash key is empty or blank,
1027+
* the method returns null.
1028+
*
1029+
* @param key The key under which the hash is stored.
1030+
* @param hKey The hash key under which the value is stored.
1031+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
1032+
* @param <T> The type of the value to be retrieved.
1033+
* @return The value from the hash corresponding to the provided hash key, or null if not found or if inputs are invalid.
1034+
*/
1035+
public static <T> T getCacheMapValue(String key, String hKey, Redis4jWrapCallback callback) {
1036+
Redis4jService e = jProvider();
1037+
if (e == null) {
1038+
return null;
1039+
}
1040+
return e.getCacheMapValue(dispatch(), key, hKey, callback);
1041+
}
1042+
9501043
/**
9511044
* Retrieves a value from a Redis hash using the given RedisTemplate, key, and hash key.
9521045
* If the dispatch template is null, or if the key or hash key is empty or blank,
@@ -982,6 +1075,31 @@ public static <T> List<T> getMultiCacheMapValue(String key, Collection<Object> h
9821075
return e.getMultiCacheMapValue(dispatch(), key, hKeys);
9831076
}
9841077

1078+
/**
1079+
* Retrieves multiple values from a Redis hash using the given RedisTemplate, key, and collection of hash keys, with an optional callback
1080+
* for handling exceptions. If the dispatch template is null, or if the key or collection of hash keys is empty or blank,
1081+
* the method returns an empty list.
1082+
*
1083+
* @param key The key under which the hash is stored.
1084+
* @param hKeys The collection of hash keys for which values need to be retrieved.
1085+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
1086+
* @param <T> The type of the values to be retrieved.
1087+
* @return A list of values from the hash corresponding to the provided hash keys, or an empty list if inputs are invalid.
1088+
*/
1089+
public static <T> List<T> getMultiCacheMapValue(String key, Collection<Object> hKeys, Redis4jWrapCallback callback) {
1090+
Redis4jService e = jProvider();
1091+
if (e == null) {
1092+
return Collections.emptyList();
1093+
}
1094+
return e.getMultiCacheMapValue(dispatch(), key, hKeys, callback);
1095+
}
1096+
1097+
/**
1098+
* Retrieves a collection of all keys from the Redis cache using the given RedisTemplate.
1099+
* Uses a wildcard pattern to match all keys.
1100+
*
1101+
* @return A collection of all keys in the Redis cache, or an empty collection if the template is null.
1102+
*/
9851103
public static Collection<String> defaultKeys() {
9861104
Redis4jService e = jProvider();
9871105
if (e == null) {
@@ -990,6 +1108,21 @@ public static Collection<String> defaultKeys() {
9901108
return e.defaultKeys(dispatch());
9911109
}
9921110

1111+
/**
1112+
* Retrieves a collection of all keys from the Redis cache using the given RedisTemplate,
1113+
* with an optional callback for handling exceptions. Uses a wildcard pattern to match all keys.
1114+
*
1115+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
1116+
* @return A collection of all keys in the Redis cache, or an empty collection if an exception occurs.
1117+
*/
1118+
public static Collection<String> defaultKeys(Redis4jWrapCallback callback) {
1119+
Redis4jService e = jProvider();
1120+
if (e == null) {
1121+
return Collections.emptyList();
1122+
}
1123+
return e.defaultKeys(dispatch(), callback);
1124+
}
1125+
9931126
public static Collection<String> canDefaultKeys() {
9941127
if (!canExecuted()) {
9951128
return Collections.emptyList();
@@ -1013,6 +1146,24 @@ public static boolean containsKey(String key) {
10131146
return e.containsKey(dispatch(), key);
10141147
}
10151148

1149+
/**
1150+
* Checks if a specific key exists in the Redis store using the given RedisTemplate,
1151+
* with an optional callback for handling exceptions.
1152+
* If the dispatch template is null, or if the key is empty or blank, the method returns false.
1153+
* Trims any whitespace from the key before checking its existence in the Redis store.
1154+
*
1155+
* @param key The key to check for existence in the Redis store.
1156+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
1157+
* @return true if the key exists in the Redis store; false otherwise.
1158+
*/
1159+
public static boolean containsKey(String key, Redis4jWrapCallback callback) {
1160+
Redis4jService e = jProvider();
1161+
if (e == null) {
1162+
return false;
1163+
}
1164+
return e.containsKey(dispatch(), key, callback);
1165+
}
1166+
10161167
/**
10171168
* Publishes data to a specified Redis topic using the given RedisTemplate.
10181169
* If the dispatch template, topic, or data is null, the method returns without performing any action.
@@ -1030,6 +1181,25 @@ public static <T> void produce(ChannelTopic topic, T data) {
10301181
e.produce(dispatch(), topic, data);
10311182
}
10321183

1184+
/**
1185+
* Publishes data to a specified Redis topic using the given RedisTemplate,
1186+
* with an optional callback for handling exceptions.
1187+
* If the dispatch template, topic, or data is null, the method returns without performing any action.
1188+
* Attempts to send the data to the specified topic, and logs any exceptions that occur during the operation.
1189+
*
1190+
* @param topic The Redis topic to which the data is to be sent.
1191+
* @param data The data to be sent to the topic.
1192+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
1193+
* @param <T> The type of data being sent.
1194+
*/
1195+
public static <T> void produce(ChannelTopic topic, T data, Redis4jWrapCallback callback) {
1196+
Redis4jService e = jProvider();
1197+
if (e == null) {
1198+
return;
1199+
}
1200+
e.produce(dispatch(), topic, data, callback);
1201+
}
1202+
10331203
/**
10341204
* Publishes data to a specified Redis topic using the given RedisTemplate.
10351205
* If the dispatch template, topic, or data is null, the method returns without performing any action.
@@ -1063,6 +1233,24 @@ public static long increaseKey(String key) {
10631233
return e.increaseKey(dispatch(), key);
10641234
}
10651235

1236+
/**
1237+
* Increases the value of a numeric key in Redis, with an optional callback for handling exceptions.
1238+
* If the dispatch template or key is null or empty, returns -1 indicating failure.
1239+
* Uses Redis execute method to atomically increment the key value.
1240+
* Logs any exceptions that occur during the operation.
1241+
*
1242+
* @param key The key whose value is to be incremented.
1243+
* @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}.
1244+
* @return The incremented value of the key, or -1 if an error occurs.
1245+
*/
1246+
public static long increaseKey( String key, Redis4jWrapCallback callback) {
1247+
Redis4jService e = jProvider();
1248+
if (e == null) {
1249+
return -1;
1250+
}
1251+
return e.increaseKey(dispatch(), key, callback);
1252+
}
1253+
10661254
/**
10671255
* Increases the value of a numeric key in Redis.
10681256
* If the dispatch template or key is null or empty, returns -1 indicating failure.
@@ -1634,9 +1822,7 @@ public static Long append(String key, String value) {
16341822
*/
16351823
@SuppressWarnings({"SpellCheckingInspection"})
16361824
public static List<String> mget(String... keys) {
1637-
return syncCommands().mget(keys).stream()
1638-
.map(kv -> kv.hasValue() ? kv.getValue() : null)
1639-
.collect(Collectors.toList());
1825+
return syncCommands().mget(keys).stream().map(kv -> kv.hasValue() ? kv.getValue() : null).collect(Collectors.toList());
16401826
}
16411827

16421828
/**
@@ -1743,13 +1929,7 @@ public static Map<String, String> defaultKeysWk() {
17431929
if (Collection4j.isEmpty(keys)) {
17441930
return Collections.emptyMap();
17451931
}
1746-
return keys.stream()
1747-
.collect(
1748-
Collectors.toMap(
1749-
key -> key,
1750-
key -> syncCommands().type(key)
1751-
)
1752-
);
1932+
return keys.stream().collect(Collectors.toMap(key -> key, key -> syncCommands().type(key)));
17531933
}
17541934

17551935
/**
@@ -1771,36 +1951,17 @@ public static WrapResponse<?> wget(String key) {
17711951
String type = command.type(key);
17721952
switch (type) {
17731953
case "string":
1774-
return new HttpWrapBuilder<>()
1775-
.ok(command.get(key))
1776-
.customFields("redis_key_type_stored", "string")
1777-
.build();
1954+
return new HttpWrapBuilder<>().ok(command.get(key)).customFields("redis_key_type_stored", "string").build();
17781955
case "list":
1779-
return new HttpWrapBuilder<>()
1780-
.ok(command.lrange(key, 0, -1))
1781-
.customFields("redis_key_type_stored", "list")
1782-
.build();
1956+
return new HttpWrapBuilder<>().ok(command.lrange(key, 0, -1)).customFields("redis_key_type_stored", "list").build();
17831957
case "hash":
1784-
return new HttpWrapBuilder<>()
1785-
.ok(command.hgetall(key))
1786-
.customFields("redis_key_type_stored", "hash")
1787-
.build();
1958+
return new HttpWrapBuilder<>().ok(command.hgetall(key)).customFields("redis_key_type_stored", "hash").build();
17881959
case "set":
1789-
return new HttpWrapBuilder<>()
1790-
.ok(command.smembers(key))
1791-
.customFields("redis_key_type_stored", "set")
1792-
.build();
1960+
return new HttpWrapBuilder<>().ok(command.smembers(key)).customFields("redis_key_type_stored", "set").build();
17931961
case "zset":
1794-
return new HttpWrapBuilder<>()
1795-
.ok(command.zrange(key, 0, -1))
1796-
.customFields("redis_key_type_stored", "zset")
1797-
.build();
1962+
return new HttpWrapBuilder<>().ok(command.zrange(key, 0, -1)).customFields("redis_key_type_stored", "zset").build();
17981963
default:
1799-
return new HttpWrapBuilder<>()
1800-
.message(String.format("unsupported type: %s", type))
1801-
.statusCode(HttpStatusBuilder.UN_PROCESSABLE_ENTITY)
1802-
.customFields("redis_key_type_stored_unsupported", type)
1803-
.build();
1964+
return new HttpWrapBuilder<>().message(String.format("unsupported type: %s", type)).statusCode(HttpStatusBuilder.UN_PROCESSABLE_ENTITY).customFields("redis_key_type_stored_unsupported", type).build();
18041965
}
18051966
}
18061967
}

0 commit comments

Comments
 (0)