-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #374 from Bit-Quill/java/dev_cyip10_getex
Java: implement GETEX
- Loading branch information
Showing
10 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,6 +220,7 @@ enum RequestType { | |
LPos = 180; | ||
LCS = 181; | ||
GeoSearch = 182; | ||
GetEx = 183; | ||
} | ||
|
||
message Command { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
java/client/src/main/java/glide/api/models/commands/GetExOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api.models.commands; | ||
|
||
import static glide.api.models.commands.GetExOptions.ExpiryType.MILLISECONDS; | ||
import static glide.api.models.commands.GetExOptions.ExpiryType.PERSIST; | ||
import static glide.api.models.commands.GetExOptions.ExpiryType.SECONDS; | ||
import static glide.api.models.commands.GetExOptions.ExpiryType.UNIX_MILLISECONDS; | ||
import static glide.api.models.commands.GetExOptions.ExpiryType.UNIX_SECONDS; | ||
|
||
import glide.api.commands.StringBaseCommands; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* Optional arguments to {@link StringBaseCommands#getex(String, GetExOptions)} command. | ||
* | ||
* @see <a href="https://redis.io/docs/latest/commands/getex/">redis.io</a> | ||
*/ | ||
public class GetExOptions { | ||
|
||
/** Expiry type for the time to live */ | ||
private final ExpiryType type; | ||
|
||
/** The amount of time to live before the key expires. */ | ||
private Long count; | ||
|
||
private GetExOptions(ExpiryType type) { | ||
this.type = type; | ||
} | ||
|
||
private GetExOptions(ExpiryType type, Long count) { | ||
this.type = type; | ||
this.count = count; | ||
} | ||
|
||
/** | ||
* Set the specified expire time, in seconds. Equivalent to <code>EX</code> in the Redis API. | ||
* | ||
* @param seconds The time to expire, in seconds. | ||
* @return The options specifying the given expiry. | ||
*/ | ||
public static GetExOptions Seconds(Long seconds) { | ||
return new GetExOptions(SECONDS, seconds); | ||
} | ||
|
||
/** | ||
* Set the specified expire time, in milliseconds. Equivalent to <code>PX</code> in the Redis API. | ||
* | ||
* @param milliseconds The time to expire, in milliseconds. | ||
* @return The options specifying the given expiry. | ||
*/ | ||
public static GetExOptions Milliseconds(Long milliseconds) { | ||
return new GetExOptions(MILLISECONDS, milliseconds); | ||
} | ||
|
||
/** | ||
* Set the specified Unix time at which the key will expire, in seconds. Equivalent to <code> | ||
* EXAT</code> in the Redis API. | ||
* | ||
* @param unixSeconds The <code>UNIX TIME</code> to expire, in seconds. | ||
* @return The options specifying the given expiry. | ||
*/ | ||
public static GetExOptions UnixSeconds(Long unixSeconds) { | ||
return new GetExOptions(UNIX_SECONDS, unixSeconds); | ||
} | ||
|
||
/** | ||
* Set the specified Unix time at which the key will expire, in milliseconds. Equivalent to <code> | ||
* PXAT</code> in the Redis API. | ||
* | ||
* @param unixMilliseconds The <code>UNIX TIME</code> to expire, in milliseconds. | ||
* @return The options specifying the given expiry. | ||
*/ | ||
public static GetExOptions UnixMilliseconds(Long unixMilliseconds) { | ||
return new GetExOptions(UNIX_MILLISECONDS, unixMilliseconds); | ||
} | ||
|
||
/** Remove the time to live associated with the key. */ | ||
public static GetExOptions Persist() { | ||
return new GetExOptions(PERSIST); | ||
} | ||
|
||
/** Types of value expiration configuration. */ | ||
@RequiredArgsConstructor | ||
protected enum ExpiryType { | ||
SECONDS("EX"), | ||
MILLISECONDS("PX"), | ||
UNIX_SECONDS("EXAT"), | ||
UNIX_MILLISECONDS("PXAT"), | ||
PERSIST("PERSIST"); | ||
|
||
private final String redisApi; | ||
} | ||
|
||
/** | ||
* Converts GetExOptions into a String[] to pass to the <code>GETEX</code> command. | ||
* | ||
* @return String[] | ||
*/ | ||
public String[] toArgs() { | ||
List<String> optionArgs = new ArrayList<>(); | ||
|
||
optionArgs.add(type.redisApi); | ||
if (count != null) { | ||
optionArgs.add(String.valueOf(count)); | ||
} | ||
|
||
return optionArgs.toArray(new String[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.