diff --git a/src/main/java/io/lettuce/core/XAddArgs.java b/src/main/java/io/lettuce/core/XAddArgs.java index ccd24e43fb..8fdc78acaa 100644 --- a/src/main/java/io/lettuce/core/XAddArgs.java +++ b/src/main/java/io/lettuce/core/XAddArgs.java @@ -26,6 +26,7 @@ * {@link XAddArgs} is a mutable object and instances should be used only once to avoid shared mutable state. * * @author Mark Paluch + * @author dengliming * @since 5.1 */ public class XAddArgs { @@ -36,6 +37,8 @@ public class XAddArgs { private boolean approximateTrimming; + private boolean noMkStream; + /** * Builder entry points for {@link XAddArgs}. */ @@ -108,6 +111,29 @@ public XAddArgs approximateTrimming(boolean approximateTrimming) { return this; } + /** + * Not to create new stream by default. + * + * @return {@code this} + * @since 6.1 + */ + public XAddArgs noMkStream() { + return noMkStream(true); + } + + /** + * Not to create new stream by default. + * + * @param noMkStream {@code true} to apply not to create new stream by default. + * @return {@code this} + * @since 6.1 + */ + public XAddArgs noMkStream(boolean noMkStream) { + + this.noMkStream = noMkStream; + return this; + } + public void build(CommandArgs args) { if (maxlen != null) { @@ -121,6 +147,10 @@ public void build(CommandArgs args) { args.add(maxlen); } + if (noMkStream) { + args.add(CommandKeyword.NOMKSTREAM); + } + if (id != null) { args.add(id); } else { diff --git a/src/main/java/io/lettuce/core/protocol/CommandKeyword.java b/src/main/java/io/lettuce/core/protocol/CommandKeyword.java index 0947919cd5..d4bba8c727 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandKeyword.java +++ b/src/main/java/io/lettuce/core/protocol/CommandKeyword.java @@ -23,6 +23,7 @@ * @author Will Glozer * @author Mark Paluch * @author Zhang Jessey + * @author dengliming */ public enum CommandKeyword implements ProtocolKeyword { @@ -34,7 +35,7 @@ public enum CommandKeyword implements ProtocolKeyword { IDLETIME, JUSTID, KILL, KEYSLOT, LEFT, LEN, LIMIT, LIST, LOAD, MATCH, - MAX, MAXLEN, MEET, MIN, MOVED, NO, NOACK, NODE, NODES, NOSAVE, NOT, NUMSUB, NUMPAT, OFF, ON, ONE, OR, PAUSE, + MAX, MAXLEN, MEET, MIN, MOVED, NO, NOACK, NODE, NODES, NOMKSTREAM, NOSAVE, NOT, NUMSUB, NUMPAT, OFF, ON, ONE, OR, PAUSE, REFCOUNT, REMOVE, RELOAD, REPLACE, REPLICATE, RESET, diff --git a/src/test/java/io/lettuce/core/commands/StreamCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/StreamCommandIntegrationTests.java index 2860b73a86..1f6e48b6a5 100644 --- a/src/test/java/io/lettuce/core/commands/StreamCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/StreamCommandIntegrationTests.java @@ -43,6 +43,7 @@ * Integration tests for {@link io.lettuce.core.api.sync.RedisStreamCommands}. * * @author Mark Paluch + * @author dengliming */ @ExtendWith(LettuceExtension.class) @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -91,6 +92,23 @@ void xaddMaxLenEfficientTrimming() { assertThat(id).isNotNull(); } + @Test + void xaddMaxLenEfficientTrimmingNoMkStream() { + + Map body = Collections.singletonMap("foo", "bar"); + String id = redis.xadd(key, XAddArgs.Builder.maxlen(5).approximateTrimming().noMkStream(), body); + assertThat(id).isNull(); + assertThat(redis.exists(key)).isEqualTo(0L); + + id = redis.xadd(key, XAddArgs.Builder.maxlen(5).approximateTrimming(), body); + assertThat(id).isNotNull(); + assertThat(redis.exists(key)).isEqualTo(1L); + + List> messages = redis.xrange(key, Range.unbounded()); + assertThat(messages.size()).isEqualTo(1); + assertThat(messages.get(0).getBody()).isEqualTo(body); + } + @Test void xdel() { @@ -523,4 +541,5 @@ void xgroupSetid() { assertThat(redis.xgroupSetid(StreamOffset.latest(key), "group")).isEqualTo("OK"); } + }