Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support PXAT/EXAT arguments to SET command #2410

Merged
merged 2 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/main/java/redis/clients/jedis/params/SetParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class SetParams extends Params {
private static final String NX = "nx";
private static final String PX = "px";
private static final String EX = "ex";
private static final String EXAT = "exat";
private static final String PXAT = "pxat";
private static final String KEEPTTL = "keepttl";
private static final String GET = "get";

Expand Down Expand Up @@ -71,6 +73,26 @@ public SetParams xx() {
return this;
}

/**
* Set the specified Unix time at which the key will expire, in seconds.
* @param seconds
* @return SetParams
*/
public SetParams exat(long seconds) {
dengliming marked this conversation as resolved.
Show resolved Hide resolved
addParam(EXAT, seconds);
return this;
}

/**
* Set the specified Unix time at which the key will expire, in milliseconds.
* @param milliseconds
* @return SetParams
*/
public SetParams pxat(long milliseconds) {
dengliming marked this conversation as resolved.
Show resolved Hide resolved
addParam(PXAT, milliseconds);
return this;
}

/**
* Retain the time to live associated with the key.
* @return SetParams
Expand All @@ -79,7 +101,7 @@ public SetParams keepttl() {
addParam(KEEPTTL);
return this;
}

/**
* Return the old value stored at key, or nil when key did not exist.
* @return SetParams
Expand Down Expand Up @@ -108,6 +130,14 @@ public byte[][] getByteParams(byte[]... args) {
byteParams.add(SafeEncoder.encode(PX));
byteParams.add(Protocol.toByteArray((long) getParam(PX)));
}
if (contains(EXAT)) {
byteParams.add(SafeEncoder.encode(EXAT));
byteParams.add(Protocol.toByteArray((long) getParam(EXAT)));
}
if (contains(PXAT)) {
byteParams.add(SafeEncoder.encode(PXAT));
byteParams.add(Protocol.toByteArray((long) getParam(PXAT)));
}
if (contains(KEEPTTL)) {
byteParams.add(SafeEncoder.encode(KEEPTTL));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ public void setAndKeepttl() {
assertTrue(ttl < 0);
}

@Test
public void setAndPxat() {
String status = jedis.set(bfoo, binaryValue, setParams().nx().pxat(System.currentTimeMillis() + expireMillis));
assertTrue(Keyword.OK.name().equalsIgnoreCase(status));
long ttl = jedis.ttl(bfoo);
assertTrue(ttl > 0 && ttl <= expireSeconds);
}

@Test
public void setAndExat() {
String status = jedis.set(bfoo, binaryValue, setParams().nx().exat(System.currentTimeMillis() / 1000 + expireSeconds));
assertTrue(Keyword.OK.name().equalsIgnoreCase(status));
long ttl = jedis.ttl(bfoo);
assertTrue(ttl > 0 && ttl <= expireSeconds);
}

@Test
public void getSet() {
byte[] value = jedis.getSet(bfoo, binaryValue);
Expand Down Expand Up @@ -338,4 +354,4 @@ public void sendBlockingCommandTest() {

assertNull(jedis.sendBlockingCommand(BLPOP, bfoo, Protocol.toByteArray(1L)));
}
}
}