Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,21 @@ public RedisFuture<Double> incrbyfloat(K key, double amount) {
return dispatch(commandBuilder.incrbyfloat(key, amount));
}

@Override
public RedisFuture<IncrexValue<Long>> increx(K key) {
return dispatch(commandBuilder.increx(key));
}

@Override
public RedisFuture<IncrexValue<Long>> increx(K key, long amount, IncrexArgs increxArgs) {
return dispatch(commandBuilder.increx(key, amount, increxArgs));
}

@Override
public RedisFuture<IncrexValue<Double>> increx(K key, double amount, IncrexFloatArgs increxArgs) {
return dispatch(commandBuilder.increx(key, amount, increxArgs));
}

@Override
public RedisFuture<String> info() {
return dispatch(commandBuilder.info());
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,21 @@ public Mono<Double> incrbyfloat(K key, double amount) {
return createMono(() -> commandBuilder.incrbyfloat(key, amount));
}

@Override
public Mono<IncrexValue<Long>> increx(K key) {
return createMono(() -> commandBuilder.increx(key));
}

@Override
public Mono<IncrexValue<Long>> increx(K key, long amount, IncrexArgs increxArgs) {
return createMono(() -> commandBuilder.increx(key, amount, increxArgs));
}

@Override
public Mono<IncrexValue<Double>> increx(K key, double amount, IncrexFloatArgs increxArgs) {
return createMono(() -> commandBuilder.increx(key, amount, increxArgs));
}

@Override
public Mono<String> info() {
return createMono(commandBuilder::info);
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/io/lettuce/core/BaseIncrexArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.lettuce.core;

import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandKeyword;
import io.lettuce.core.protocol.CommandType;

/**
* Abstract base for {@code INCREX} arguments. Owns options common to both the integer-bounded variant ({@link IncrexArgs}) and
* the float-bounded variant ({@link IncrexFloatArgs}): {@code SATURATE}, the
* {@code EX}/{@code PX}/{@code EXAT}/{@code PXAT}/{@code PERSIST} expiration block, and {@code ENX}.
* <p>
* Bounds ({@code LBOUND}/{@code UBOUND}) live on the concrete subclasses so the bound type matches the increment type at
* compile time &mdash; integer-mode {@code increx} only accepts {@link IncrexArgs}, float-mode {@code increx} only accepts
* {@link IncrexFloatArgs}.
*
* @param <T> the concrete subtype, for fluent method chaining
* @since 7.6
*/
abstract class BaseIncrexArgs<T extends BaseIncrexArgs<T>> implements CompositeArgument {
Comment thread
a-TODO-rov marked this conversation as resolved.
Resolved

private boolean saturate = false;

private Long ex;

private Long px;

private Long exAt;

private Long pxAt;

private boolean persist = false;

private boolean enx = false;

@SuppressWarnings("unchecked")
private T self() {
return (T) this;
}

/**
* Saturate the result on out-of-bounds: the value is clamped to the violated bound (or the implicit numeric-type limit when
* no explicit bound is set), and the second element of the reply reflects the actual applied increment. Without
* {@code SATURATE}, an out-of-bounds operation is silently rejected: the key value and TTL are unchanged and the reply is
* {@code [current_value, 0]}.
*/
public T saturate() {
this.saturate = true;
return self();
}

public T ex(long seconds) {
this.ex = seconds;
return self();
}

public T px(long milliseconds) {
this.px = milliseconds;
return self();
}

public T exAt(long timestampSeconds) {
this.exAt = timestampSeconds;
return self();
}

public T pxAt(long timestampMilliseconds) {
this.pxAt = timestampMilliseconds;
return self();
}

public T persist() {
this.persist = true;
return self();
}

/**
* Apply the expiry only if the key does not already have one ({@code Expiry only if Not eXists}). If the key already has a
* TTL, the existing TTL is preserved.
*/
public T enx() {
this.enx = true;
return self();
}

@Override
public <K, V> void build(CommandArgs<K, V> args) {

buildBounds(args);

if (saturate) {
args.add(CommandKeyword.SATURATE);
}

if (ex != null) {
args.add(CommandKeyword.EX).add(ex);
}
if (px != null) {
args.add(CommandKeyword.PX).add(px);
}
if (exAt != null) {
args.add(CommandKeyword.EXAT).add(exAt);
}
if (pxAt != null) {
args.add(CommandKeyword.PXAT).add(pxAt);
}
if (persist) {
args.add(CommandType.PERSIST);
}

if (enx) {
args.add(CommandKeyword.ENX);
}
}

/**
* Subclasses emit their typed {@code LBOUND}/{@code UBOUND} here.
*/
protected abstract <K, V> void buildBounds(CommandArgs<K, V> args);

}
100 changes: 100 additions & 0 deletions src/main/java/io/lettuce/core/IncrexArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.lettuce.core;

import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandKeyword;

/**
* Argument list builder for the integer-bounded form of the Redis {@code INCREX} command. Accepts {@code long} lower/upper
* bounds; paired with the {@code increx(K, long, IncrexArgs)} overload on the commands interface (sends {@code BYINT} on the
* wire).
* <p>
* For float-bounded INCREX use {@link IncrexFloatArgs}.
* <p>
* Usage:
*
* <pre>
* IncrexArgs.Builder.ubound(100).saturate().ex(60)
* IncrexArgs.Builder.lbound(0).ubound(100).saturate()
* new IncrexArgs().ex(30).enx()
* </pre>
*
* @since 7.6
*/
public class IncrexArgs extends BaseIncrexArgs<IncrexArgs> {

private Long lbound;

private Long ubound;

// ── Static Builder ─────────────────────────────

public static class Builder {

private Builder() {
}

public static IncrexArgs lbound(long lbound) {
return new IncrexArgs().lbound(lbound);
}

public static IncrexArgs ubound(long ubound) {
return new IncrexArgs().ubound(ubound);
}

public static IncrexArgs saturate() {
return new IncrexArgs().saturate();
}

public static IncrexArgs ex(long seconds) {
return new IncrexArgs().ex(seconds);
}

public static IncrexArgs px(long milliseconds) {
return new IncrexArgs().px(milliseconds);
}

public static IncrexArgs exAt(long timestampSeconds) {
return new IncrexArgs().exAt(timestampSeconds);
}

public static IncrexArgs pxAt(long timestampMilliseconds) {
return new IncrexArgs().pxAt(timestampMilliseconds);
}

public static IncrexArgs persist() {
return new IncrexArgs().persist();
}

public static IncrexArgs enx() {
return new IncrexArgs().enx();
}

}

// ── Fluent Setters ─────────────────────────────

public IncrexArgs lbound(long lbound) {
this.lbound = lbound;
return this;
}

public IncrexArgs ubound(long ubound) {
this.ubound = ubound;
return this;
}

@Override
protected <K, V> void buildBounds(CommandArgs<K, V> args) {

if (lbound != null) {
args.add(CommandKeyword.LBOUND);
args.add(lbound);
}

if (ubound != null) {
args.add(CommandKeyword.UBOUND);
args.add(ubound);
}
}

}
100 changes: 100 additions & 0 deletions src/main/java/io/lettuce/core/IncrexFloatArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.lettuce.core;

import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandKeyword;

/**
* Argument list builder for the floating-point-bounded form of the Redis {@code INCREX} command. Accepts {@code double}
* lower/upper bounds; paired with the {@code increx(K, double, IncrexFloatArgs)} overload on the commands interface (sends
* {@code BYFLOAT} on the wire).
* <p>
* For integer-bounded INCREX use {@link IncrexArgs}.
* <p>
* Usage:
*
* <pre>
* IncrexFloatArgs.Builder.ubound(100.0).saturate().ex(60)
* IncrexFloatArgs.Builder.lbound(0.0).ubound(100.0).saturate()
* new IncrexFloatArgs().ex(30).enx()
* </pre>
*
* @since 7.6
*/
public class IncrexFloatArgs extends BaseIncrexArgs<IncrexFloatArgs> {

private Double lbound;
Comment thread
a-TODO-rov marked this conversation as resolved.
Resolved

private Double ubound;

// ── Static Builder ─────────────────────────────

public static class Builder {

private Builder() {
}

public static IncrexFloatArgs lbound(double lbound) {
return new IncrexFloatArgs().lbound(lbound);
}

public static IncrexFloatArgs ubound(double ubound) {
return new IncrexFloatArgs().ubound(ubound);
}

public static IncrexFloatArgs saturate() {
return new IncrexFloatArgs().saturate();
}

public static IncrexFloatArgs ex(long seconds) {
return new IncrexFloatArgs().ex(seconds);
}

public static IncrexFloatArgs px(long milliseconds) {
return new IncrexFloatArgs().px(milliseconds);
}

public static IncrexFloatArgs exAt(long timestampSeconds) {
return new IncrexFloatArgs().exAt(timestampSeconds);
}

public static IncrexFloatArgs pxAt(long timestampMilliseconds) {
return new IncrexFloatArgs().pxAt(timestampMilliseconds);
}

public static IncrexFloatArgs persist() {
return new IncrexFloatArgs().persist();
}

public static IncrexFloatArgs enx() {
return new IncrexFloatArgs().enx();
}

}

// ── Fluent Setters ─────────────────────────────

public IncrexFloatArgs lbound(double lbound) {
this.lbound = lbound;
return this;
}

public IncrexFloatArgs ubound(double ubound) {
this.ubound = ubound;
return this;
}

@Override
protected <K, V> void buildBounds(CommandArgs<K, V> args) {

if (lbound != null) {
args.add(CommandKeyword.LBOUND);
args.add(lbound);
}

if (ubound != null) {
args.add(CommandKeyword.UBOUND);
args.add(ubound);
}
}

}
Loading
Loading
⌂ 换网址