Skip to content

Commit 0f54fcf

Browse files
DATAREDIS-1103 - Polishing.
Move keepTTL to Expiration and remove superfluous methods from interfaces. Add tests for reactive variant and work around an open issue in Jedis to support KEEPTTL though the API does not offer that option directly. Original Pull Request: #562
1 parent 232c8a5 commit 0f54fcf

14 files changed

+117
-98
lines changed

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -993,15 +993,6 @@ public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption op
993993
return convertAndReturn(delegate.set(key, value, expiration, option), identityConverter);
994994
}
995995

996-
/*
997-
* (non-Javadoc)
998-
* @see org.springframework.data.redis.connection.RedisStringCommands#set(byte[], byte[], org.springframework.data.redis.core.types.Expiration, org.springframework.data.redis.connection.RedisStringCommands.SetOptions, boolean)
999-
*/
1000-
@Override
1001-
public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option, boolean keepTtl) {
1002-
return convertAndReturn(delegate.set(key, value, expiration, option, keepTtl), identityConverter);
1003-
}
1004-
1005996
/*
1006997
* (non-Javadoc)
1007998
* @see org.springframework.data.redis.connection.RedisStringCommands#setBit(byte[], long, boolean)

src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,6 @@ default Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption o
294294
return stringCommands().set(key, value, expiration, option);
295295
}
296296

297-
/** @deprecated in favor of {@link RedisConnection#stringCommands()}}. */
298-
@Override
299-
@Deprecated
300-
default Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option, boolean keepTtl) {
301-
return stringCommands().set(key, value, expiration, option, keepTtl);
302-
}
303-
304297
/** @deprecated in favor of {@link RedisConnection#stringCommands()}}. */
305298
@Override
306299
@Deprecated

src/main/java/org/springframework/data/redis/connection/ReactiveStringCommands.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ default Mono<Boolean> set(ByteBuffer key, ByteBuffer value) {
169169
*
170170
* @param key must not be {@literal null}.
171171
* @param value must not be {@literal null}.
172-
* @param expiration must not be {@literal null}.
172+
* @param expiration must not be {@literal null}. Use {@link Expiration#persistent()} for no expiration time or
173+
* {@link Expiration#keepTtl()} to keep the existing.
173174
* @param option must not be {@literal null}.
174175
* @return
175176
* @see <a href="https://redis.io/commands/set">Redis Documentation: SET</a>

src/main/java/org/springframework/data/redis/connection/RedisStringCommands.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ enum BitOperation {
8383
*
8484
* @param key must not be {@literal null}.
8585
* @param value must not be {@literal null}.
86-
* @param expiration must not be {@literal null}. Use {@link Expiration#persistent()} to not set any ttl.
86+
* @param expiration must not be {@literal null}. Use {@link Expiration#persistent()} to not set any ttl or
87+
* {@link Expiration#keepTtl()} to keep the existing expiration.
8788
* @param option must not be {@literal null}. Use {@link SetOption#upsert()} to add non existing.
8889
* @return {@literal null} when used in pipeline / transaction.
8990
* @since 1.7
@@ -92,22 +93,6 @@ enum BitOperation {
9293
@Nullable
9394
Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option);
9495

95-
/**
96-
* Set {@code value} for {@code key} applying timeouts from {@code expiration} if set and inserting/updating values
97-
* depending on {@code option}.
98-
*
99-
* @param key must not be {@literal null}.
100-
* @param value must not be {@literal null}.
101-
* @param expiration must not be {@literal null}. Use {@link Expiration#persistent()} to not set any ttl.
102-
* @param option must not be {@literal null}. Use {@link SetOption#upsert()} to add non existing.
103-
* @param keepTtl set the value and retain the existing TTL.
104-
* @return {@literal null} when used in pipeline / transaction.
105-
* @since 2.4
106-
* @see <a href="https://redis.io/commands/set">Redis Documentation: SET</a>
107-
*/
108-
@Nullable
109-
Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option, boolean keepTtl);
110-
11196
/**
11297
* Set {@code value} for {@code key}, only if {@code key} does not exist.
11398
*

src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ interface StringTuple extends Tuple {
420420
*
421421
* @param key must not be {@literal null}.
422422
* @param value must not be {@literal null}.
423-
* @param expiration can be {@literal null}. Defaulted to {@link Expiration#persistent()}.
423+
* @param expiration can be {@literal null}. Defaulted to {@link Expiration#persistent()}. Use
424+
* {@link Expiration#keepTtl()} to keep the existing expiration.
424425
* @param option can be {@literal null}. Defaulted to {@link SetOption#UPSERT}.
425426
* @since 1.7
426427
* @see <a href="https://redis.io/commands/set">Redis Documentation: SET</a>

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterStringCommands.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,11 @@ public Boolean set(byte[] key, byte[] value) {
127127
*/
128128
@Override
129129
public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option) {
130-
return set(key, value, expiration, option, false);
131-
}
132130

133-
/*
134-
* (non-Javadoc)
135-
* @see org.springframework.data.redis.connection.RedisStringCommands#set(byte[], byte[], org.springframework.data.redis.core.types.Expiration, org.springframework.data.redis.connection.RedisStringCommands.SetOptions, boolean)
136-
*/
137-
@Override
138-
public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option, boolean keepTtl) {
139131
Assert.notNull(key, "Key must not be null!");
140132
Assert.notNull(value, "Value must not be null!");
141133
Assert.notNull(expiration, "Expiration must not be null!");
142134
Assert.notNull(option, "Option must not be null!");
143-
Assert.isTrue(keepTtl, "KEEPTTL is currently not supported in jedis!");
144135

145136
SetParams setParams = JedisConverters.toSetCommandExPxArgument(expiration,
146137
JedisConverters.toSetCommandNxXxArgument(option));

src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,24 @@ public static SetParams toSetCommandExPxArgument(Expiration expiration, SetParam
533533

534534
SetParams paramsToUse = params == null ? SetParams.setParams() : params;
535535

536+
if (expiration.isKeepTtl()) {
537+
538+
// TODO: remove once jedis supports KEEPTTL (https://github.com/xetorthio/jedis/issues/2248)
539+
return new SetParams() {
540+
541+
@Override
542+
public byte[][] getByteParams(byte[]... args) {
543+
544+
ArrayList<byte[]> byteParams = new ArrayList<>();
545+
for (byte[] arg : paramsToUse.getByteParams(args)) {
546+
byteParams.add(arg);
547+
}
548+
byteParams.add(SafeEncoder.encode("keepttl"));
549+
return byteParams.toArray(new byte[byteParams.size()][]);
550+
}
551+
};
552+
}
553+
536554
if (!expiration.isPersistent()) {
537555
if (expiration.getTimeUnit() == TimeUnit.MILLISECONDS) {
538556
return paramsToUse.px(expiration.getExpirationTime());

src/main/java/org/springframework/data/redis/connection/jedis/JedisStringCommands.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,11 @@ public Boolean set(byte[] key, byte[] value) {
153153
*/
154154
@Override
155155
public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option) {
156-
return set(key, value, expiration, option, false);
157-
}
158156

159-
/*
160-
* (non-Javadoc)
161-
* @see org.springframework.data.redis.connection.RedisStringCommands#set(byte[], byte[], org.springframework.data.redis.core.types.Expiration, org.springframework.data.redis.connection.RedisStringCommands.SetOption, boolean)
162-
*/
163-
@Override
164-
public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option, boolean keepTtl) {
165157
Assert.notNull(key, "Key must not be null!");
166158
Assert.notNull(value, "Value must not be null!");
167159
Assert.notNull(expiration, "Expiration must not be null!");
168160
Assert.notNull(option, "Option must not be null!");
169-
Assert.isTrue(keepTtl, "KEEPTTL is currently not supported in jedis!");
170161

171162
SetParams params = JedisConverters.toSetCommandExPxArgument(expiration,
172163
JedisConverters.toSetCommandNxXxArgument(option));

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -768,34 +768,24 @@ public static RedisClusterNode toRedisClusterNode(io.lettuce.core.cluster.models
768768
* @since 1.7
769769
*/
770770
public static SetArgs toSetArgs(@Nullable Expiration expiration, @Nullable SetOption option) {
771-
return toSetArgs(expiration, option, false);
772-
}
773-
774-
/**
775-
* Converts a given {@link Expiration} and {@link SetOption} to the according {@link SetArgs}.<br />
776-
*
777-
* @param expiration can be {@literal null}.
778-
* @param option can be {@literal null}.
779-
* @param keepTtl set the value and retain the existing TTL.
780-
* @since 2.4
781-
*/
782-
public static SetArgs toSetArgs(@Nullable Expiration expiration, @Nullable SetOption option, boolean keepTtl) {
783771

784772
SetArgs args = new SetArgs();
785-
if (expiration != null && !expiration.isPersistent()) {
786773

787-
switch (expiration.getTimeUnit()) {
788-
case SECONDS:
789-
args.ex(expiration.getExpirationTime());
790-
break;
791-
default:
792-
args.px(expiration.getConverted(TimeUnit.MILLISECONDS));
793-
break;
794-
}
795-
}
774+
if (expiration != null) {
775+
776+
if (expiration.isKeepTtl()) {
777+
args.keepttl();
778+
} else if (!expiration.isPersistent()) {
796779

797-
if (keepTtl) {
798-
args.keepttl();
780+
switch (expiration.getTimeUnit()) {
781+
case SECONDS:
782+
args.ex(expiration.getExpirationTime());
783+
break;
784+
default:
785+
args.px(expiration.getConverted(TimeUnit.MILLISECONDS));
786+
break;
787+
}
788+
}
799789
}
800790

801791
if (option != null) {

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceStringCommands.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,7 @@ public Boolean set(byte[] key, byte[] value) {
157157
*/
158158
@Override
159159
public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option) {
160-
return set(key, value, expiration, option, false);
161-
}
162160

163-
/*
164-
* (non-Javadoc)
165-
* @see org.springframework.data.redis.connection.RedisStringCommands#set(byte[], byte[], org.springframework.data.redis.core.types.Expiration, org.springframework.data.redis.connection.RedisStringCommands.SetOption, boolean)
166-
*/
167-
@Override
168-
public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option, boolean keepTtl) {
169161
Assert.notNull(key, "Key must not be null!");
170162
Assert.notNull(value, "Value must not be null!");
171163
Assert.notNull(expiration, "Expiration must not be null!");
@@ -174,18 +166,18 @@ public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption op
174166
try {
175167
if (isPipelined()) {
176168
pipeline(connection.newLettuceResult(
177-
getAsyncConnection().set(key, value, LettuceConverters.toSetArgs(expiration, option, keepTtl)),
169+
getAsyncConnection().set(key, value, LettuceConverters.toSetArgs(expiration, option)),
178170
Converters.stringToBooleanConverter(), () -> false));
179171
return null;
180172
}
181173
if (isQueueing()) {
182174
transaction(connection.newLettuceResult(
183-
getAsyncConnection().set(key, value, LettuceConverters.toSetArgs(expiration, option, keepTtl)),
175+
getAsyncConnection().set(key, value, LettuceConverters.toSetArgs(expiration, option)),
184176
Converters.stringToBooleanConverter(), () -> false));
185177
return null;
186178
}
187179
return Converters
188-
.stringToBoolean(getConnection().set(key, value, LettuceConverters.toSetArgs(expiration, option, keepTtl)));
180+
.stringToBoolean(getConnection().set(key, value, LettuceConverters.toSetArgs(expiration, option)));
189181
} catch (Exception ex) {
190182
throw convertLettuceAccessException(ex);
191183
}

0 commit comments

Comments
 (0)