Skip to content

Commit

Permalink
Introduce overloads for expire[at] and pexpire[at] accepting Instant …
Browse files Browse the repository at this point in the history
…and Duration #1607

We now ship an improved integration with JSR-310 types.
  • Loading branch information
mp911de committed Feb 3, 2021
1 parent 8a31910 commit 073a799
Show file tree
Hide file tree
Showing 13 changed files with 497 additions and 98 deletions.
31 changes: 29 additions & 2 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static io.lettuce.core.protocol.CommandType.*;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -592,15 +593,28 @@ public RedisFuture<Boolean> expire(K key, long seconds) {
}

@Override
public RedisFuture<Boolean> expireat(K key, Date timestamp) {
return expireat(key, timestamp.getTime() / 1000);
public RedisFuture<Boolean> expire(K key, Duration seconds) {
LettuceAssert.notNull(seconds, "Timeout must not be null");
return expire(key, seconds.toMillis() / 1000);
}

@Override
public RedisFuture<Boolean> expireat(K key, long timestamp) {
return dispatch(commandBuilder.expireat(key, timestamp));
}

@Override
public RedisFuture<Boolean> expireat(K key, Date timestamp) {
LettuceAssert.notNull(timestamp, "Timestamp must not be null");
return expireat(key, timestamp.getTime() / 1000);
}

@Override
public RedisFuture<Boolean> expireat(K key, Instant timestamp) {
LettuceAssert.notNull(timestamp, "Timestamp must not be null");
return expireat(key, timestamp.toEpochMilli() / 1000);
}

@Override
public void flushCommands() {
connection.flushCommands();
Expand Down Expand Up @@ -1088,11 +1102,24 @@ public RedisFuture<Boolean> pexpire(K key, long milliseconds) {
return dispatch(commandBuilder.pexpire(key, milliseconds));
}

@Override
public RedisFuture<Boolean> pexpire(K key, Duration milliseconds) {
LettuceAssert.notNull(milliseconds, "Timeout must not be null");
return pexpire(key, milliseconds.toMillis());
}

@Override
public RedisFuture<Boolean> pexpireat(K key, Date timestamp) {
LettuceAssert.notNull(timestamp, "Timestamp must not be null");
return pexpireat(key, timestamp.getTime());
}

@Override
public RedisFuture<Boolean> pexpireat(K key, Instant timestamp) {
LettuceAssert.notNull(timestamp, "Timestamp must not be null");
return pexpireat(key, timestamp.toEpochMilli());
}

@Override
public RedisFuture<Boolean> pexpireat(K key, long timestamp) {
return dispatch(commandBuilder.pexpireat(key, timestamp));
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static io.lettuce.core.protocol.CommandType.*;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -649,16 +650,29 @@ public Mono<Boolean> expire(K key, long seconds) {
return createMono(() -> commandBuilder.expire(key, seconds));
}

@Override
public Mono<Boolean> expire(K key, Duration seconds) {
LettuceAssert.notNull(seconds, "Timeout must not be null");
return expire(key, seconds.toMillis() / 1000);
}

@Override
public Mono<Boolean> expireat(K key, long timestamp) {
return createMono(() -> commandBuilder.expireat(key, timestamp));
}

@Override
public Mono<Boolean> expireat(K key, Date timestamp) {
LettuceAssert.notNull(timestamp, "Timestamp must not be null");
return expireat(key, timestamp.getTime() / 1000);
}

@Override
public Mono<Boolean> expireat(K key, Instant timestamp) {
LettuceAssert.notNull(timestamp, "Timestamp must not be null");
return expireat(key, timestamp.toEpochMilli() / 1000);
}

@Override
public void flushCommands() {
connection.flushCommands();
Expand Down Expand Up @@ -1154,15 +1168,26 @@ public Mono<Boolean> pexpire(K key, long milliseconds) {
}

@Override
public Mono<Boolean> pexpireat(K key, Date timestamp) {
return pexpireat(key, timestamp.getTime());
public Mono<Boolean> pexpire(K key, Duration milliseconds) {
LettuceAssert.notNull(milliseconds, "Timeout must not be null");
return pexpire(key, milliseconds.toMillis());
}

@Override
public Mono<Boolean> pexpireat(K key, long timestamp) {
return createMono(() -> commandBuilder.pexpireat(key, timestamp));
}

@Override
public Mono<Boolean> pexpireat(K key, Date timestamp) {
return pexpireat(key, timestamp.getTime());
}

@Override
public Mono<Boolean> pexpireat(K key, Instant timestamp) {
return pexpireat(key, timestamp.toEpochMilli());
}

@Override
public Mono<Long> pfadd(K key, V... values) {
return createMono(() -> commandBuilder.pfadd(key, values));
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/io/lettuce/core/SetArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.lettuce.core;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;

Expand Down Expand Up @@ -69,6 +70,18 @@ public static SetArgs ex(long timeout) {
return new SetArgs().ex(timeout);
}

/**
* Creates new {@link SetArgs} and enable {@literal EX}.
*
* @param timeout expire time in seconds.
* @return new {@link SetArgs} with {@literal EX} enabled.
* @see SetArgs#ex(long)
* @since 6.1
*/
public static SetArgs ex(Duration timeout) {
return new SetArgs().ex(timeout);
}

/**
* Creates new {@link SetArgs} and enable {@literal EXAT}.
*
Expand Down Expand Up @@ -115,6 +128,18 @@ public static SetArgs px(long timeout) {
return new SetArgs().px(timeout);
}

/**
* Creates new {@link SetArgs} and enable {@literal PX}.
*
* @param timeout expire time in milliseconds.
* @return new {@link SetArgs} with {@literal PX} enabled.
* @see SetArgs#px(long)
* @since 6.1
*/
public static SetArgs px(Duration timeout) {
return new SetArgs().px(timeout);
}

/**
* Creates new {@link SetArgs} and enable {@literal PXAT}.
*
Expand Down Expand Up @@ -195,6 +220,21 @@ public SetArgs ex(long timeout) {
return this;
}

/**
* Set the specified expire time, in seconds.
*
* @param timeout expire time in seconds.
* @return {@code this} {@link SetArgs}.
* @since 6.1
*/
public SetArgs ex(Duration timeout) {

LettuceAssert.notNull(timeout, "Timeout must not be null");

this.ex = timeout.toMillis() / 1000;
return this;
}

/**
* Set the specified expire at time using a posix {@code timestamp}.
*
Expand Down Expand Up @@ -248,6 +288,20 @@ public SetArgs px(long timeout) {
return this;
}

/**
* Set the specified expire time, in milliseconds.
*
* @param timeout expire time in milliseconds.
* @return {@code this} {@link SetArgs}.
*/
public SetArgs px(Duration timeout) {

LettuceAssert.notNull(timeout, "Timeout must not be null");

this.px = timeout.toMillis();
return this;
}

/**
* Set the specified expire at time using a posix {@code timestamp}.
*
Expand Down
66 changes: 51 additions & 15 deletions src/main/java/io/lettuce/core/api/async/RedisKeyAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.lettuce.core.api.async;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -101,20 +103,38 @@ public interface RedisKeyAsyncCommands<K, V> {
* @param key the key.
* @param seconds the seconds type: long.
* @return Boolean integer-reply specifically:
*
* {@code true} if the timeout was set. {@code false} if {@code key} does not exist or the timeout could not be set.
*/
RedisFuture<Boolean> expire(K key, long seconds);

/**
* Set a key's time to live in seconds.
*
* @param key the key.
* @param seconds the seconds.
* @return Boolean integer-reply specifically: {@code true} if the timeout was set. {@code false} if {@code key} does not
* exist or the timeout could not be set.
* @since 6.1
*/
RedisFuture<Boolean> expire(K key, Duration seconds);

/**
* Set the expiration for a key as a UNIX timestamp.
*
* @param key the key.
* @param timestamp the timestamp type: posix time.
* @return Boolean integer-reply specifically:
* @return Boolean integer-reply specifically: {@code true} if the timeout was set. {@code false} if {@code key} does not
* exist or the timeout could not be set (see: {@code EXPIRE}).
*/
RedisFuture<Boolean> expireat(K key, long timestamp);

/**
* Set the expiration for a key as a UNIX timestamp.
*
* {@code true} if the timeout was set. {@code false} if {@code key} does not exist or the timeout could not be set
* (see: {@code EXPIRE}).
* @param key the key.
* @param timestamp the timestamp type: posix time.
* @return Boolean integer-reply specifically: {@code true} if the timeout was set. {@code false} if {@code key} does not
* exist or the timeout could not be set (see: {@code EXPIRE}).
*/
RedisFuture<Boolean> expireat(K key, Date timestamp);

Expand All @@ -123,12 +143,11 @@ public interface RedisKeyAsyncCommands<K, V> {
*
* @param key the key.
* @param timestamp the timestamp type: posix time.
* @return Boolean integer-reply specifically:
*
* {@code true} if the timeout was set. {@code false} if {@code key} does not exist or the timeout could not be set
* (see: {@code EXPIRE}).
* @return Boolean integer-reply specifically: {@code true} if the timeout was set. {@code false} if {@code key} does not
* exist or the timeout could not be set (see: {@code EXPIRE}).
* @since 6.1
*/
RedisFuture<Boolean> expireat(K key, long timestamp);
RedisFuture<Boolean> expireat(K key, Instant timestamp);

/**
* Find all keys matching the given pattern.
Expand Down Expand Up @@ -222,20 +241,38 @@ public interface RedisKeyAsyncCommands<K, V> {
* @param key the key.
* @param milliseconds the milliseconds type: long.
* @return integer-reply, specifically:
*
* {@code true} if the timeout was set. {@code false} if {@code key} does not exist or the timeout could not be set.
*/
RedisFuture<Boolean> pexpire(K key, long milliseconds);

/**
* Set a key's time to live in milliseconds.
*
* @param key the key.
* @param milliseconds the milliseconds.
* @return integer-reply, specifically: {@code true} if the timeout was set. {@code false} if {@code key} does not exist or
* the timeout could not be set.
* @since 6.1
*/
RedisFuture<Boolean> pexpire(K key, Duration milliseconds);

/**
* Set the expiration for a key as a UNIX timestamp specified in milliseconds.
*
* @param key the key.
* @param timestamp the milliseconds-timestamp type: posix time.
* @return Boolean integer-reply specifically:
* @return Boolean integer-reply specifically: {@code true} if the timeout was set. {@code false} if {@code key} does not
* exist or the timeout could not be set (see: {@code EXPIRE}).
*/
RedisFuture<Boolean> pexpireat(K key, long timestamp);

/**
* Set the expiration for a key as a UNIX timestamp specified in milliseconds.
*
* {@code true} if the timeout was set. {@code false} if {@code key} does not exist or the timeout could not be set
* (see: {@code EXPIRE}).
* @param key the key.
* @param timestamp the milliseconds-timestamp type: posix time.
* @return Boolean integer-reply specifically: {@code true} if the timeout was set. {@code false} if {@code key} does not
* exist or the timeout could not be set (see: {@code EXPIRE}).
*/
RedisFuture<Boolean> pexpireat(K key, Date timestamp);

Expand All @@ -245,11 +282,10 @@ public interface RedisKeyAsyncCommands<K, V> {
* @param key the key.
* @param timestamp the milliseconds-timestamp type: posix time.
* @return Boolean integer-reply specifically:
*
* {@code true} if the timeout was set. {@code false} if {@code key} does not exist or the timeout could not be set
* (see: {@code EXPIRE}).
*/
RedisFuture<Boolean> pexpireat(K key, long timestamp);
RedisFuture<Boolean> pexpireat(K key, Instant timestamp);

/**
* Get the time to live for a key in milliseconds.
Expand Down
Loading

0 comments on commit 073a799

Please sign in to comment.