From 02f6eb3ecb62694682b7f74de4c605f190e4d60d Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sun, 28 Jul 2024 19:13:36 +0700 Subject: [PATCH] :sparkles: feat: add a lot of functions wrapper when executing command #5 --- .../groovy/org/redis4j/common/Redis4j.java | 231 +++++++++++-- .../org/redis4j/service/Redis4jService.java | 138 +++++++- .../service/impl/Redis4jServiceImpl.java | 317 +++++++++++++++++- 3 files changed, 646 insertions(+), 40 deletions(-) diff --git a/plugin/src/main/groovy/org/redis4j/common/Redis4j.java b/plugin/src/main/groovy/org/redis4j/common/Redis4j.java index 6ef962c..01ccc08 100644 --- a/plugin/src/main/groovy/org/redis4j/common/Redis4j.java +++ b/plugin/src/main/groovy/org/redis4j/common/Redis4j.java @@ -814,6 +814,24 @@ public static void setCacheMap(String key, Map map) { e.setCacheMap(dispatch(), key, map); } + /** + * Stores a map of objects in Redis using the given RedisTemplate and key, with an optional callback + * for handling exceptions. If the dispatch template is null, the map is empty, or the key is empty or blank, + * the method does nothing. + * + * @param key The key under which the map will be stored. + * @param map The map of objects to be stored in Redis. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of objects in the map. + */ + public static void setCacheMap(String key, Map map, Redis4jWrapCallback callback) { + Redis4jService e = jProvider(); + if (e == null) { + return; + } + e.setCacheMap(dispatch(), key, map, callback); + } + /** * Stores a map of objects in Redis using the given RedisTemplate and key. * If the dispatch template is null, the map is empty, or the key is empty or blank, @@ -848,6 +866,26 @@ public static void setCacheMapSafe(String key, Pair... map) { e.setCacheMapSafe(dispatch(), key, map); } + /** + * Stores a map of objects in Redis using the given RedisTemplate and key, with an optional callback + * for handling exceptions. This method allows for a variable number of key-value pairs to be provided as arguments. + * If the dispatch template is null, the map is empty, or the key is empty or blank, + * the method does nothing. + * + * @param key The key under which the map will be stored. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param map A variable number of key-value pairs to be stored in Redis. + * @param The type of objects in the map. + */ + @SuppressWarnings({"unused", "unchecked"}) + public static void setCacheMapSafe(String key, Redis4jWrapCallback callback, Pair... map) { + Redis4jService e = jProvider(); + if (e == null) { + return; + } + e.setCacheMapSafe(dispatch(), key, callback, map); + } + /** * Stores a map of objects in Redis using the given RedisTemplate and key. * If the dispatch template is null, the map is empty, or the key is empty or blank, @@ -881,6 +919,23 @@ public static Map getCacheMap(String key) { return e.getCacheMap(dispatch(), key); } + /** + * Retrieves a map of objects from Redis using the given RedisTemplate and key, with an optional callback + * for handling exceptions. If the dispatch template is null or the key is empty or blank, + * the method returns an empty map. + * + * @param key The key under which the map is stored. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return A map of objects retrieved from Redis, or an empty map if the dispatch template or key is invalid. + */ + public static Map getCacheMap(String key, Redis4jWrapCallback callback) { + Redis4jService e = jProvider(); + if (e == null) { + return Collections.emptyMap(); + } + return e.getCacheMap(dispatch(), key, callback); + } + /** * Retrieves a map of objects from Redis using the given RedisTemplate and key. * If the dispatch template is null or the key is empty or blank, @@ -913,6 +968,25 @@ public static void setCacheMapValue(String key, String hKey, T value) { e.setCacheMapValue(dispatch(), key, hKey, value); } + /** + * Sets a value in a Redis hash using the given RedisTemplate, key, and hash key, with an optional callback + * for handling exceptions. If the dispatch template is null, the value is null, or the key or hash key is empty or blank, + * the method returns without performing any operation. + * + * @param key The key under which the hash is stored. + * @param hKey The hash key under which the value is stored. + * @param value The value to be set in the hash. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of the value being set. + */ + public static void setCacheMapValue(String key, String hKey, T value, Redis4jWrapCallback callback) { + Redis4jService e = jProvider(); + if (e == null) { + return; + } + e.setCacheMapValue(dispatch(), key, hKey, value, callback); + } + /** * Sets a value in a Redis hash using the given RedisTemplate, key, and hash key. * 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 getCacheMapValue(String key, String hKey) { return e.getCacheMapValue(dispatch(), key, hKey); } + /** + * Retrieves a value from a Redis hash using the given RedisTemplate, key, and hash key, with an optional callback + * for handling exceptions. If the dispatch template is null, or if the key or hash key is empty or blank, + * the method returns null. + * + * @param key The key under which the hash is stored. + * @param hKey The hash key under which the value is stored. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of the value to be retrieved. + * @return The value from the hash corresponding to the provided hash key, or null if not found or if inputs are invalid. + */ + public static T getCacheMapValue(String key, String hKey, Redis4jWrapCallback callback) { + Redis4jService e = jProvider(); + if (e == null) { + return null; + } + return e.getCacheMapValue(dispatch(), key, hKey, callback); + } + /** * Retrieves a value from a Redis hash using the given RedisTemplate, key, and hash key. * If the dispatch template is null, or if the key or hash key is empty or blank, @@ -982,6 +1075,31 @@ public static List getMultiCacheMapValue(String key, Collection h return e.getMultiCacheMapValue(dispatch(), key, hKeys); } + /** + * Retrieves multiple values from a Redis hash using the given RedisTemplate, key, and collection of hash keys, with an optional callback + * for handling exceptions. If the dispatch template is null, or if the key or collection of hash keys is empty or blank, + * the method returns an empty list. + * + * @param key The key under which the hash is stored. + * @param hKeys The collection of hash keys for which values need to be retrieved. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of the values to be retrieved. + * @return A list of values from the hash corresponding to the provided hash keys, or an empty list if inputs are invalid. + */ + public static List getMultiCacheMapValue(String key, Collection hKeys, Redis4jWrapCallback callback) { + Redis4jService e = jProvider(); + if (e == null) { + return Collections.emptyList(); + } + return e.getMultiCacheMapValue(dispatch(), key, hKeys, callback); + } + + /** + * Retrieves a collection of all keys from the Redis cache using the given RedisTemplate. + * Uses a wildcard pattern to match all keys. + * + * @return A collection of all keys in the Redis cache, or an empty collection if the template is null. + */ public static Collection defaultKeys() { Redis4jService e = jProvider(); if (e == null) { @@ -990,6 +1108,21 @@ public static Collection defaultKeys() { return e.defaultKeys(dispatch()); } + /** + * Retrieves a collection of all keys from the Redis cache using the given RedisTemplate, + * with an optional callback for handling exceptions. Uses a wildcard pattern to match all keys. + * + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return A collection of all keys in the Redis cache, or an empty collection if an exception occurs. + */ + public static Collection defaultKeys(Redis4jWrapCallback callback) { + Redis4jService e = jProvider(); + if (e == null) { + return Collections.emptyList(); + } + return e.defaultKeys(dispatch(), callback); + } + public static Collection canDefaultKeys() { if (!canExecuted()) { return Collections.emptyList(); @@ -1013,6 +1146,24 @@ public static boolean containsKey(String key) { return e.containsKey(dispatch(), key); } + /** + * Checks if a specific key exists in the Redis store using the given RedisTemplate, + * with an optional callback for handling exceptions. + * If the dispatch template is null, or if the key is empty or blank, the method returns false. + * Trims any whitespace from the key before checking its existence in the Redis store. + * + * @param key The key to check for existence in the Redis store. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return true if the key exists in the Redis store; false otherwise. + */ + public static boolean containsKey(String key, Redis4jWrapCallback callback) { + Redis4jService e = jProvider(); + if (e == null) { + return false; + } + return e.containsKey(dispatch(), key, callback); + } + /** * Publishes data to a specified Redis topic using the given RedisTemplate. * If the dispatch template, topic, or data is null, the method returns without performing any action. @@ -1030,6 +1181,25 @@ public static void produce(ChannelTopic topic, T data) { e.produce(dispatch(), topic, data); } + /** + * Publishes data to a specified Redis topic using the given RedisTemplate, + * with an optional callback for handling exceptions. + * If the dispatch template, topic, or data is null, the method returns without performing any action. + * Attempts to send the data to the specified topic, and logs any exceptions that occur during the operation. + * + * @param topic The Redis topic to which the data is to be sent. + * @param data The data to be sent to the topic. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of data being sent. + */ + public static void produce(ChannelTopic topic, T data, Redis4jWrapCallback callback) { + Redis4jService e = jProvider(); + if (e == null) { + return; + } + e.produce(dispatch(), topic, data, callback); + } + /** * Publishes data to a specified Redis topic using the given RedisTemplate. * 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) { return e.increaseKey(dispatch(), key); } + /** + * Increases the value of a numeric key in Redis, with an optional callback for handling exceptions. + * If the dispatch template or key is null or empty, returns -1 indicating failure. + * Uses Redis execute method to atomically increment the key value. + * Logs any exceptions that occur during the operation. + * + * @param key The key whose value is to be incremented. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return The incremented value of the key, or -1 if an error occurs. + */ + public static long increaseKey( String key, Redis4jWrapCallback callback) { + Redis4jService e = jProvider(); + if (e == null) { + return -1; + } + return e.increaseKey(dispatch(), key, callback); + } + /** * Increases the value of a numeric key in Redis. * 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) { */ @SuppressWarnings({"SpellCheckingInspection"}) public static List mget(String... keys) { - return syncCommands().mget(keys).stream() - .map(kv -> kv.hasValue() ? kv.getValue() : null) - .collect(Collectors.toList()); + return syncCommands().mget(keys).stream().map(kv -> kv.hasValue() ? kv.getValue() : null).collect(Collectors.toList()); } /** @@ -1743,13 +1929,7 @@ public static Map defaultKeysWk() { if (Collection4j.isEmpty(keys)) { return Collections.emptyMap(); } - return keys.stream() - .collect( - Collectors.toMap( - key -> key, - key -> syncCommands().type(key) - ) - ); + return keys.stream().collect(Collectors.toMap(key -> key, key -> syncCommands().type(key))); } /** @@ -1771,36 +1951,17 @@ public static WrapResponse wget(String key) { String type = command.type(key); switch (type) { case "string": - return new HttpWrapBuilder<>() - .ok(command.get(key)) - .customFields("redis_key_type_stored", "string") - .build(); + return new HttpWrapBuilder<>().ok(command.get(key)).customFields("redis_key_type_stored", "string").build(); case "list": - return new HttpWrapBuilder<>() - .ok(command.lrange(key, 0, -1)) - .customFields("redis_key_type_stored", "list") - .build(); + return new HttpWrapBuilder<>().ok(command.lrange(key, 0, -1)).customFields("redis_key_type_stored", "list").build(); case "hash": - return new HttpWrapBuilder<>() - .ok(command.hgetall(key)) - .customFields("redis_key_type_stored", "hash") - .build(); + return new HttpWrapBuilder<>().ok(command.hgetall(key)).customFields("redis_key_type_stored", "hash").build(); case "set": - return new HttpWrapBuilder<>() - .ok(command.smembers(key)) - .customFields("redis_key_type_stored", "set") - .build(); + return new HttpWrapBuilder<>().ok(command.smembers(key)).customFields("redis_key_type_stored", "set").build(); case "zset": - return new HttpWrapBuilder<>() - .ok(command.zrange(key, 0, -1)) - .customFields("redis_key_type_stored", "zset") - .build(); + return new HttpWrapBuilder<>().ok(command.zrange(key, 0, -1)).customFields("redis_key_type_stored", "zset").build(); default: - return new HttpWrapBuilder<>() - .message(String.format("unsupported type: %s", type)) - .statusCode(HttpStatusBuilder.UN_PROCESSABLE_ENTITY) - .customFields("redis_key_type_stored_unsupported", type) - .build(); + return new HttpWrapBuilder<>().message(String.format("unsupported type: %s", type)).statusCode(HttpStatusBuilder.UN_PROCESSABLE_ENTITY).customFields("redis_key_type_stored_unsupported", type).build(); } } } diff --git a/plugin/src/main/groovy/org/redis4j/service/Redis4jService.java b/plugin/src/main/groovy/org/redis4j/service/Redis4jService.java index 9f06aa4..f91b9c9 100644 --- a/plugin/src/main/groovy/org/redis4j/service/Redis4jService.java +++ b/plugin/src/main/groovy/org/redis4j/service/Redis4jService.java @@ -270,6 +270,19 @@ public interface Redis4jService { */ void setCacheMap(RedisTemplate dispatch, String key, Map map); + /** + * Stores a map of objects in Redis using the given RedisTemplate and key, with an optional callback + * for handling exceptions. If the dispatch template is null, the map is empty, or the key is empty or blank, + * the method does nothing. + * + * @param dispatch The RedisTemplate used to store the map. + * @param key The key under which the map will be stored. + * @param map The map of objects to be stored in Redis. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of objects in the map. + */ + void setCacheMap(RedisTemplate dispatch, String key, Map map, Redis4jWrapCallback callback); + /** * Stores a map of objects in Redis using the given RedisTemplate and key. * If the dispatch template is null, the map is empty, or the key is empty or blank, @@ -283,6 +296,21 @@ public interface Redis4jService { @SuppressWarnings({"unused", "unchecked"}) void setCacheMapSafe(RedisTemplate dispatch, String key, Pair... map); + /** + * Stores a map of objects in Redis using the given RedisTemplate and key, with an optional callback + * for handling exceptions. This method allows for a variable number of key-value pairs to be provided as arguments. + * If the dispatch template is null, the map is empty, or the key is empty or blank, + * the method does nothing. + * + * @param dispatch The RedisTemplate used to store the map. + * @param key The key under which the map will be stored. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param map A variable number of key-value pairs to be stored in Redis. + * @param The type of objects in the map. + */ + @SuppressWarnings({"unused", "unchecked"}) + void setCacheMapSafe(RedisTemplate dispatch, String key, Redis4jWrapCallback callback, Pair... map); + /** * Retrieves a map of objects from Redis using the given RedisTemplate and key. * If the dispatch template is null or the key is empty or blank, @@ -294,6 +322,18 @@ public interface Redis4jService { */ Map getCacheMap(RedisTemplate dispatch, String key); + /** + * Retrieves a map of objects from Redis using the given RedisTemplate and key, with an optional callback + * for handling exceptions. If the dispatch template is null or the key is empty or blank, + * the method returns an empty map. + * + * @param dispatch The RedisTemplate used to retrieve the map. + * @param key The key under which the map is stored. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return A map of objects retrieved from Redis, or an empty map if the dispatch template or key is invalid. + */ + Map getCacheMap(RedisTemplate dispatch, String key, Redis4jWrapCallback callback); + /** * Sets a value in a Redis hash using the given RedisTemplate, key, and hash key. * If the dispatch template is null, the value is null, or the key or hash key is empty or blank, @@ -306,6 +346,20 @@ public interface Redis4jService { */ void setCacheMapValue(RedisTemplate dispatch, String key, String hKey, T value); + /** + * Sets a value in a Redis hash using the given RedisTemplate, key, and hash key, with an optional callback + * for handling exceptions. If the dispatch template is null, the value is null, or the key or hash key is empty or blank, + * the method returns without performing any operation. + * + * @param dispatch The RedisTemplate used to set the hash value. + * @param key The key under which the hash is stored. + * @param hKey The hash key under which the value is stored. + * @param value The value to be set in the hash. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of the value being set. + */ + void setCacheMapValue(RedisTemplate dispatch, String key, String hKey, T value, Redis4jWrapCallback callback); + /** * Retrieves a value from a Redis hash using the given RedisTemplate, key, and hash key. * If the dispatch template is null, or if the key or hash key is empty or blank, @@ -319,6 +373,20 @@ public interface Redis4jService { */ T getCacheMapValue(RedisTemplate dispatch, String key, String hKey); + /** + * Retrieves a value from a Redis hash using the given RedisTemplate, key, and hash key, with an optional callback + * for handling exceptions. If the dispatch template is null, or if the key or hash key is empty or blank, + * the method returns null. + * + * @param dispatch The RedisTemplate used to retrieve the hash value. + * @param key The key under which the hash is stored. + * @param hKey The hash key under which the value is stored. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of the value to be retrieved. + * @return The value from the hash corresponding to the provided hash key, or null if not found or if inputs are invalid. + */ + T getCacheMapValue(RedisTemplate dispatch, String key, String hKey, Redis4jWrapCallback callback); + /** * Retrieves multiple values from a Redis hash using the given RedisTemplate, key, and collection of hash keys. * If the dispatch template is null, or if the key or collection of hash keys is empty or blank, @@ -333,12 +401,38 @@ public interface Redis4jService { List getMultiCacheMapValue(RedisTemplate dispatch, String key, Collection hKeys); /** - * Get list of basic objects of cache + * Retrieves multiple values from a Redis hash using the given RedisTemplate, key, and collection of hash keys, with an optional callback + * for handling exceptions. If the dispatch template is null, or if the key or collection of hash keys is empty or blank, + * the method returns an empty list. * - * @return object list + * @param dispatch The RedisTemplate used to retrieve the hash values. + * @param key The key under which the hash is stored. + * @param hKeys The collection of hash keys for which values need to be retrieved. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of the values to be retrieved. + * @return A list of values from the hash corresponding to the provided hash keys, or an empty list if inputs are invalid. + */ + List getMultiCacheMapValue(RedisTemplate dispatch, String key, Collection hKeys, Redis4jWrapCallback callback); + + /** + * Retrieves a collection of all keys from the Redis cache using the given RedisTemplate. + * Uses a wildcard pattern to match all keys. + * + * @param dispatch The RedisTemplate used to interact with the Redis cache. + * @return A collection of all keys in the Redis cache, or an empty collection if the template is null. */ Collection defaultKeys(RedisTemplate dispatch); + /** + * Retrieves a collection of all keys from the Redis cache using the given RedisTemplate, + * with an optional callback for handling exceptions. Uses a wildcard pattern to match all keys. + * + * @param dispatch The RedisTemplate used to interact with the Redis cache. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return A collection of all keys in the Redis cache, or an empty collection if an exception occurs. + */ + Collection defaultKeys(RedisTemplate dispatch, Redis4jWrapCallback callback); + /** * Checks if a specific key exists in the Redis store using the given RedisTemplate. * If the dispatch template is null, or if the key is empty or blank, the method returns false. @@ -350,6 +444,19 @@ public interface Redis4jService { */ boolean containsKey(RedisTemplate dispatch, String key); + /** + * Checks if a specific key exists in the Redis store using the given RedisTemplate, + * with an optional callback for handling exceptions. + * If the dispatch template is null, or if the key is empty or blank, the method returns false. + * Trims any whitespace from the key before checking its existence in the Redis store. + * + * @param dispatch The RedisTemplate used to check the existence of the key. + * @param key The key to check for existence in the Redis store. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return true if the key exists in the Redis store; false otherwise. + */ + boolean containsKey(RedisTemplate dispatch, String key, Redis4jWrapCallback callback); + /** * Publishes data to a specified Redis topic using the given RedisTemplate. * If the dispatch template, topic, or data is null, the method returns without performing any action. @@ -362,6 +469,20 @@ public interface Redis4jService { */ void produce(RedisTemplate dispatch, ChannelTopic topic, T data); + /** + * Publishes data to a specified Redis topic using the given RedisTemplate, + * with an optional callback for handling exceptions. + * If the dispatch template, topic, or data is null, the method returns without performing any action. + * Attempts to send the data to the specified topic, and logs any exceptions that occur during the operation. + * + * @param dispatch The RedisTemplate used to send the data. + * @param topic The Redis topic to which the data is to be sent. + * @param data The data to be sent to the topic. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of data being sent. + */ + void produce(RedisTemplate dispatch, ChannelTopic topic, T data, Redis4jWrapCallback callback); + /** * Increases the value of a numeric key in Redis. * If the dispatch template or key is null or empty, returns -1 indicating failure. @@ -374,6 +495,19 @@ public interface Redis4jService { */ long increaseKey(RedisTemplate dispatch, String key); + /** + * Increases the value of a numeric key in Redis, with an optional callback for handling exceptions. + * If the dispatch template or key is null or empty, returns -1 indicating failure. + * Uses Redis execute method to atomically increment the key value. + * Logs any exceptions that occur during the operation. + * + * @param dispatch The RedisTemplate used to execute the operation. + * @param key The key whose value is to be incremented. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return The incremented value of the key, or -1 if an error occurs. + */ + long increaseKey(RedisTemplate dispatch, String key, Redis4jWrapCallback callback); + /** * Decreases the value of a numeric key in Redis. * If the dispatch template or key is null or empty, returns -1 indicating failure. diff --git a/plugin/src/main/groovy/org/redis4j/service/impl/Redis4jServiceImpl.java b/plugin/src/main/groovy/org/redis4j/service/impl/Redis4jServiceImpl.java index 1b9164e..9ec4eb5 100644 --- a/plugin/src/main/groovy/org/redis4j/service/impl/Redis4jServiceImpl.java +++ b/plugin/src/main/groovy/org/redis4j/service/impl/Redis4jServiceImpl.java @@ -617,6 +617,36 @@ public void setCacheMap(RedisTemplate dispatch, String key, } } + /** + * Stores a map of objects in Redis using the given RedisTemplate and key, with an optional callback + * for handling exceptions. If the dispatch template is null, the map is empty, or the key is empty or blank, + * the method does nothing. + * + * @param dispatch The RedisTemplate used to store the map. + * @param key The key under which the map will be stored. + * @param map The map of objects to be stored in Redis. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of objects in the map. + */ + @Override + public void setCacheMap(RedisTemplate dispatch, String key, Map map, Redis4jWrapCallback callback) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + try { + this.setCacheMap(dispatch, key, map); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("setting redis key map failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_key", key) + .customFields("redis_value", map); + } + if (callback != null) { + callback.onCallback(response.build()); + } + } + /** * Stores a map of objects in Redis using the given RedisTemplate and key. * If the dispatch template is null, the map is empty, or the key is empty or blank, @@ -635,6 +665,38 @@ public final void setCacheMapSafe(RedisTemplate dispatch, St this.setCacheMap(dispatch, key, Collection4j.mapOf(map)); } + /** + * Stores a map of objects in Redis using the given RedisTemplate and key, with an optional callback + * for handling exceptions. This method allows for a variable number of key-value pairs to be provided as arguments. + * If the dispatch template is null, the map is empty, or the key is empty or blank, + * the method does nothing. + * + * @param dispatch The RedisTemplate used to store the map. + * @param key The key under which the map will be stored. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param map A variable number of key-value pairs to be stored in Redis. + * @param The type of objects in the map. + */ + @SafeVarargs + @Override + public final void setCacheMapSafe(RedisTemplate dispatch, String key, Redis4jWrapCallback callback, Pair... map) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + try { + this.setCacheMapSafe(dispatch, key, map); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("setting redis key map safe failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_key", key) + .customFields("redis_value", map); + } + if (callback != null) { + callback.onCallback(response.build()); + } + } + /** * Retrieves a map of objects from Redis using the given RedisTemplate and key. * If the dispatch template is null or the key is empty or blank, @@ -656,6 +718,36 @@ public Map getCacheMap(RedisTemplate dispatch, S return dispatch.opsForHash().entries(key); } + /** + * Retrieves a map of objects from Redis using the given RedisTemplate and key, with an optional callback + * for handling exceptions. If the dispatch template is null or the key is empty or blank, + * the method returns an empty map. + * + * @param dispatch The RedisTemplate used to retrieve the map. + * @param key The key under which the map is stored. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return A map of objects retrieved from Redis, or an empty map if the dispatch template or key is invalid. + */ + @Override + public Map getCacheMap(RedisTemplate dispatch, String key, Redis4jWrapCallback callback) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + Map map = new HashMap<>(); + try { + map = this.getCacheMap(dispatch, key); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("getting redis key map failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_key", key); + } + if (callback != null) { + callback.onCallback(response.build()); + } + return map; + } + /** * Sets a value in a Redis hash using the given RedisTemplate, key, and hash key. * If the dispatch template is null, the value is null, or the key or hash key is empty or blank, @@ -685,6 +777,38 @@ public void setCacheMapValue(RedisTemplate dispatch, String } } + /** + * Sets a value in a Redis hash using the given RedisTemplate, key, and hash key, with an optional callback + * for handling exceptions. If the dispatch template is null, the value is null, or the key or hash key is empty or blank, + * the method returns without performing any operation. + * + * @param dispatch The RedisTemplate used to set the hash value. + * @param key The key under which the hash is stored. + * @param hKey The hash key under which the value is stored. + * @param value The value to be set in the hash. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of the value being set. + */ + @Override + public void setCacheMapValue(RedisTemplate dispatch, String key, String hKey, T value, Redis4jWrapCallback callback) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + try { + this.setCacheMapValue(dispatch, key, hKey, value); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("setting redis key map failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_key", key) + .customFields("redis_value", value) + .customFields("redis_hash_key", hKey); + } + if (callback != null) { + callback.onCallback(response.build()); + } + } + /** * Retrieves a value from a Redis hash using the given RedisTemplate, key, and hash key. * If the dispatch template is null, or if the key or hash key is empty or blank, @@ -712,6 +836,39 @@ public T getCacheMapValue(RedisTemplate dispatch, String key return ops.get(key, hKey); } + /** + * Retrieves a value from a Redis hash using the given RedisTemplate, key, and hash key, with an optional callback + * for handling exceptions. If the dispatch template is null, or if the key or hash key is empty or blank, + * the method returns null. + * + * @param dispatch The RedisTemplate used to retrieve the hash value. + * @param key The key under which the hash is stored. + * @param hKey The hash key under which the value is stored. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of the value to be retrieved. + * @return The value from the hash corresponding to the provided hash key, or null if not found or if inputs are invalid. + */ + @Override + public T getCacheMapValue(RedisTemplate dispatch, String key, String hKey, Redis4jWrapCallback callback) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + T data = null; + try { + data = this.getCacheMapValue(dispatch, key, hKey); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("getting redis key map failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_key", key) + .customFields("redis_hash_key", hKey); + } + if (callback != null) { + callback.onCallback(response.build()); + } + return data; + } + /** * Retrieves multiple values from a Redis hash using the given RedisTemplate, key, and collection of hash keys. * If the dispatch template is null, or if the key or collection of hash keys is empty or blank, @@ -737,16 +894,78 @@ public List getMultiCacheMapValue(RedisTemplate dispatch, } /** - * Get list of basic objects of cache + * Retrieves multiple values from a Redis hash using the given RedisTemplate, key, and collection of hash keys, with an optional callback + * for handling exceptions. If the dispatch template is null, or if the key or collection of hash keys is empty or blank, + * the method returns an empty list. * - * @param dispatch the Redis template, class {@link RedisTemplate} - * @return object list + * @param dispatch The RedisTemplate used to retrieve the hash values. + * @param key The key under which the hash is stored. + * @param hKeys The collection of hash keys for which values need to be retrieved. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of the values to be retrieved. + * @return A list of values from the hash corresponding to the provided hash keys, or an empty list if inputs are invalid. + */ + @Override + public List getMultiCacheMapValue(RedisTemplate dispatch, String key, Collection hKeys, Redis4jWrapCallback callback) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + List data = new ArrayList<>(); + try { + data = this.getMultiCacheMapValue(dispatch, key, hKeys); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("getting redis multiple keys map failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_key", key) + .customFields("redis_hash_keys", hKeys); + } + if (callback != null) { + callback.onCallback(response.build()); + } + return data; + } + + /** + * Retrieves a collection of all keys from the Redis cache using the given RedisTemplate. + * Uses a wildcard pattern to match all keys. + * + * @param dispatch The RedisTemplate used to interact with the Redis cache. + * @return A collection of all keys in the Redis cache, or an empty collection if the template is null. */ @Override public Collection defaultKeys(RedisTemplate dispatch) { return keys(dispatch, "*"); } + /** + * Retrieves a collection of all keys from the Redis cache using the given RedisTemplate, + * with an optional callback for handling exceptions. Uses a wildcard pattern to match all keys. + * + * @param dispatch The RedisTemplate used to interact with the Redis cache. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return A collection of all keys in the Redis cache, or an empty collection if an exception occurs. + */ + @Override + public Collection defaultKeys(RedisTemplate dispatch, Redis4jWrapCallback callback) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + Collection list = new ArrayList<>(); + try { + list = this.defaultKeys(dispatch); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("getting redis all keys failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_keys", "*"); + } + if (callback != null) { + callback.onCallback(response.build()); + } + return list; + } + /** * Checks if a specific key exists in the Redis store using the given RedisTemplate. * If the dispatch template is null, or if the key is empty or blank, the method returns false. @@ -769,6 +988,37 @@ public boolean containsKey(RedisTemplate dispatch, String key) { return Collection4j.isNotEmpty(collection) && collection.contains(key); } + /** + * Checks if a specific key exists in the Redis store using the given RedisTemplate, + * with an optional callback for handling exceptions. + * If the dispatch template is null, or if the key is empty or blank, the method returns false. + * Trims any whitespace from the key before checking its existence in the Redis store. + * + * @param dispatch The RedisTemplate used to check the existence of the key. + * @param key The key to check for existence in the Redis store. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return true if the key exists in the Redis store; false otherwise. + */ + @Override + public boolean containsKey(RedisTemplate dispatch, String key, Redis4jWrapCallback callback) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + boolean exists = false; + try { + exists = this.containsKey(dispatch, key); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("checking exists redis key failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_key", key); + } + if (callback != null) { + callback.onCallback(response.build()); + } + return exists; + } + /** * Publishes data to a specified Redis topic using the given RedisTemplate. * If the dispatch template, topic, or data is null, the method returns without performing any action. @@ -796,6 +1046,36 @@ public void produce(RedisTemplate dispatch, ChannelTopic top } } + /** + * Publishes data to a specified Redis topic using the given RedisTemplate, + * with an optional callback for handling exceptions. + * If the dispatch template, topic, or data is null, the method returns without performing any action. + * Attempts to send the data to the specified topic, and logs any exceptions that occur during the operation. + * + * @param dispatch The RedisTemplate used to send the data. + * @param topic The Redis topic to which the data is to be sent. + * @param data The data to be sent to the topic. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @param The type of data being sent. + */ + @Override + public void produce(RedisTemplate dispatch, ChannelTopic topic, T data, Redis4jWrapCallback callback) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + try { + this.produce(dispatch, topic, data); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("producing redis topic failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_topic", topic.getTopic()); + } + if (callback != null) { + callback.onCallback(response.build()); + } + } + /** * Increases the value of a numeric key in Redis. * If the dispatch template or key is null or empty, returns -1 indicating failure. @@ -826,6 +1106,37 @@ public long increaseKey(RedisTemplate dispatch, String key) { } } + /** + * Increases the value of a numeric key in Redis, with an optional callback for handling exceptions. + * If the dispatch template or key is null or empty, returns -1 indicating failure. + * Uses Redis execute method to atomically increment the key value. + * Logs any exceptions that occur during the operation. + * + * @param dispatch The RedisTemplate used to execute the operation. + * @param key The key whose value is to be incremented. + * @param callback An optional callback for handling exceptions, an instance of {@link Redis4jWrapCallback}. + * @return The incremented value of the key, or -1 if an error occurs. + */ + @Override + public long increaseKey(RedisTemplate dispatch, String key, Redis4jWrapCallback callback) { + HttpWrapBuilder response = new HttpWrapBuilder<>().ok(null).requestId(Redis4j.getCurrentSessionId()); + long value = 0; + try { + value = this.increaseKey(dispatch, key); + } catch (Exception e) { + response + .statusCode(HttpStatusBuilder.INTERNAL_SERVER_ERROR) + .message("increasing redis key failed") + .debug("cause", e.getMessage()) + .errors(e) + .customFields("redis_key", key); + } + if (callback != null) { + callback.onCallback(response.build()); + } + return value; + } + /** * Decreases the value of a numeric key in Redis. * If the dispatch template or key is null or empty, returns -1 indicating failure.