|
15 | 15 | import glide.managers.CommandManager;
|
16 | 16 | import glide.managers.ConnectionManager;
|
17 | 17 | import glide.managers.models.Command;
|
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
18 | 20 | import java.util.List;
|
19 | 21 | import java.util.Map;
|
20 | 22 | import java.util.concurrent.CompletableFuture;
|
@@ -201,4 +203,142 @@ public CompletableFuture<String> set(String key, String value, SetOptions option
|
201 | 203 | .build();
|
202 | 204 | return commandManager.submitNewCommand(command, BaseClient::handleStringResponse);
|
203 | 205 | }
|
| 206 | + |
| 207 | + /** |
| 208 | + * Decrements the number stored at `key` by one. If `key` does not exist, it is set to 0 before |
| 209 | + * performing the operation. |
| 210 | + * |
| 211 | + * @see <a href="https://redis.io/commands/decr/">redis.io</a> for details. |
| 212 | + * @param key - The key to decrement its value. |
| 213 | + * @return the value of `key` after the decrement. An error is raised if `key` contains a value of |
| 214 | + * the wrong type or contains a string that can not be represented as integer. |
| 215 | + */ |
| 216 | + @Override |
| 217 | + public CompletableFuture<Long> decr(String key) { |
| 218 | + Command command = |
| 219 | + Command.builder() |
| 220 | + .requestType(Command.RequestType.DECR) |
| 221 | + .arguments(new String[] {key}) |
| 222 | + .build(); |
| 223 | + return commandManager.submitNewCommand(command, BaseClient::handleLongResponse); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Decrements the number stored at `key` by `amount`. If `key` does not exist, it is set to 0 |
| 228 | + * before performing the operation. |
| 229 | + * |
| 230 | + * @see <a href="https://redis.io/commands/decrby/">redis.io</a> for details. |
| 231 | + * @param key - The key to decrement its value. |
| 232 | + * @param amount - The amount to decrement. |
| 233 | + * @return the value of `key` after the decrement. An error is raised if `key` contains a value of |
| 234 | + * the wrong type or contains a string that can not be represented as integer. |
| 235 | + */ |
| 236 | + @Override |
| 237 | + public CompletableFuture<Long> decrBy(String key, long amount) { |
| 238 | + Command command = |
| 239 | + Command.builder() |
| 240 | + .requestType(Command.RequestType.DECR_BY) |
| 241 | + .arguments(new String[] {key, Long.toString(amount)}) |
| 242 | + .build(); |
| 243 | + return commandManager.submitNewCommand(command, BaseClient::handleLongResponse); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Increments the number stored at `key` by one. If `key` does not exist, it is set to 0 before |
| 248 | + * performing the operation. |
| 249 | + * |
| 250 | + * @see <a href="https://redis.io/commands/incr/">redis.io</a> for details. |
| 251 | + * @param key - The key to increment its value. |
| 252 | + * @return the value of `key` after the increment, An error is raised if `key` contains a value of |
| 253 | + * the wrong type or contains a string that can not be represented as integer. |
| 254 | + */ |
| 255 | + @Override |
| 256 | + public CompletableFuture<Long> incr(String key) { |
| 257 | + Command command = |
| 258 | + Command.builder() |
| 259 | + .requestType(Command.RequestType.INCR) |
| 260 | + .arguments(new String[] {key}) |
| 261 | + .build(); |
| 262 | + return commandManager.submitNewCommand(command, BaseClient::handleLongResponse); |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Increments the number stored at `key` by `amount`. If `key` does not exist, it is set to 0 |
| 267 | + * before performing the operation. |
| 268 | + * |
| 269 | + * @see <a href="https://redis.io/commands/incrby/">redis.io</a> for details. |
| 270 | + * @param key - The key to increment its value. |
| 271 | + * @param amount - The amount to increment. |
| 272 | + * @returns the value of `key` after the increment, An error is raised if `key` contains a value |
| 273 | + * of the wrong type or contains a string that can not be represented as integer. |
| 274 | + */ |
| 275 | + @Override |
| 276 | + public CompletableFuture<Long> incrBy(String key, long amount) { |
| 277 | + Command command = |
| 278 | + Command.builder() |
| 279 | + .requestType(Command.RequestType.INCR_BY) |
| 280 | + .arguments(new String[] {key, Long.toString(amount)}) |
| 281 | + .build(); |
| 282 | + return commandManager.submitNewCommand(command, BaseClient::handleLongResponse); |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * Increment the string representing a floating point number stored at `key` by `amount`. By using |
| 287 | + * a negative increment value, the result is that the value stored at `key` is decremented. If |
| 288 | + * `key` does not exist, it is set to 0 before performing the operation. |
| 289 | + * |
| 290 | + * @see <a href="https://redis.io/commands/incrbyfloat/">redis.io</a> for details. |
| 291 | + * @param key - The key to increment its value. |
| 292 | + * @param amount - The amount to increment. |
| 293 | + * @returns the value of `key` after the increment. An error is raised if `key` contains a value |
| 294 | + * of the wrong type, or the current key content is not parsable as a double precision |
| 295 | + * floating point number. |
| 296 | + */ |
| 297 | + @Override |
| 298 | + public CompletableFuture<Double> incrByFloat(String key, double amount) { |
| 299 | + Command command = |
| 300 | + Command.builder() |
| 301 | + .requestType(Command.RequestType.INCR_BY_FLOAT) |
| 302 | + .arguments(new String[] {key, Double.toString(amount)}) |
| 303 | + .build(); |
| 304 | + return commandManager.submitNewCommand(command, BaseClient::handleDoubleResponse); |
| 305 | + } |
| 306 | + |
| 307 | + /** |
| 308 | + * Retrieve the values of multiple keys. |
| 309 | + * |
| 310 | + * @see <a href="https://redis.io/commands/mget/">redis.io</a> for details. |
| 311 | + * @param keys - A list of keys to retrieve values for. |
| 312 | + * @returns A list of values corresponding to the provided keys. If a key is not found, its |
| 313 | + * corresponding value in the list will be null. |
| 314 | + */ |
| 315 | + @Override |
| 316 | + public CompletableFuture<Object[]> mget(String[] keys) { |
| 317 | + Command command = |
| 318 | + Command.builder().requestType(Command.RequestType.MGET).arguments(keys).build(); |
| 319 | + return commandManager.submitNewCommand(command, BaseClient::handleObjectArrayResponse); |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * Set multiple keys to multiple values in a single operation. |
| 324 | + * |
| 325 | + * @see <a href="https://redis.io/commands/mset/">redis.io</a> for details. |
| 326 | + * @param keyValueMap - A key-value map consisting of keys and their respective values to set. |
| 327 | + * @returns null |
| 328 | + */ |
| 329 | + @Override |
| 330 | + public CompletableFuture<Void> mset(HashMap<String, String> keyValueMap) { |
| 331 | + List<String> flatMap = new ArrayList<>(); |
| 332 | + |
| 333 | + for (Map.Entry<String, String> entry : keyValueMap.entrySet()) { |
| 334 | + flatMap.add(entry.getKey()); |
| 335 | + flatMap.add(entry.getValue()); |
| 336 | + } |
| 337 | + |
| 338 | + String[] args = flatMap.toArray(new String[0]); |
| 339 | + |
| 340 | + Command command = |
| 341 | + Command.builder().requestType(Command.RequestType.MSET).arguments(args).build(); |
| 342 | + return commandManager.submitNewCommand(command, BaseClient::handleVoidResponse); |
| 343 | + } |
204 | 344 | }
|
0 commit comments