Skip to content

Commit ef9a10c

Browse files
committed
Polishing.
Use SetCondition instead of SetOption, remove implementations using SetOption and tests. Reduce tests to avoid cartesian products caused by matrix testing on all sorts of levels, instead, test conversions to driver-specific argument objects and ensure that argument objects are applied. Revert deprecations of setIf… methods accepting Duration/Expiration. See spring-projects#3324
1 parent 279d0c7 commit ef9a10c

29 files changed

+633
-2774
lines changed

src/main/java/org/springframework/data/redis/cache/DefaultRedisCacheWriter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.redis.cache;
1717

18-
import org.springframework.data.redis.connection.SetCondition;
1918
import reactor.core.publisher.Flux;
2019
import reactor.core.publisher.Mono;
2120

@@ -40,6 +39,7 @@
4039
import org.springframework.data.redis.connection.RedisConnection;
4140
import org.springframework.data.redis.connection.RedisConnectionFactory;
4241
import org.springframework.data.redis.connection.RedisStringCommands;
42+
import org.springframework.data.redis.connection.SetCondition;
4343
import org.springframework.data.redis.core.ScanOptions;
4444
import org.springframework.data.redis.core.types.Expiration;
4545
import org.springframework.data.redis.util.ByteUtils;
@@ -213,8 +213,7 @@ public void enable(Consumer<CacheLockingConfiguration> configurationConsumer) {
213213
public CacheLockingConfiguration sleepTime(Duration sleepTime) {
214214

215215
Assert.notNull(sleepTime, "Lock sleep time must not be null");
216-
Assert.isTrue(isPositiveDuration(sleepTime),
217-
"Lock sleep time must not be null zero or negative");
216+
Assert.isTrue(isPositiveDuration(sleepTime), "Lock sleep time must not be null zero or negative");
218217

219218
this.lockSleepTime = sleepTime;
220219

@@ -356,7 +355,8 @@ public void put(String name, byte[] key, byte[] value, @Nullable Duration ttl) {
356355
private void doPut(RedisConnection connection, String name, byte[] key, byte[] value, @Nullable Duration ttl) {
357356

358357
if (isPositiveDuration(ttl)) {
359-
connection.stringCommands().set(key, value, SetCondition.upsert(), Expiration.from(ttl.toMillis(), TimeUnit.MILLISECONDS));
358+
connection.stringCommands().set(key, value, SetCondition.upsert(),
359+
Expiration.from(ttl.toMillis(), TimeUnit.MILLISECONDS));
360360
} else {
361361
connection.stringCommands().set(key, value);
362362
}
@@ -750,7 +750,8 @@ private Mono<Boolean> doStore(byte[] cacheKey, byte[] value, @Nullable Duration
750750
ByteBuffer wrappedValue = ByteBuffer.wrap(value);
751751

752752
if (isPositiveDuration(ttl)) {
753-
return connection.stringCommands().set(wrappedKey, wrappedValue, SetCondition.upsert(), Expiration.from(ttl.toMillis(), TimeUnit.MILLISECONDS));
753+
return connection.stringCommands().set(wrappedKey, wrappedValue, SetCondition.upsert(),
754+
Expiration.from(ttl.toMillis(), TimeUnit.MILLISECONDS));
754755
} else {
755756
return connection.stringCommands().set(wrappedKey, wrappedValue);
756757
}
@@ -784,8 +785,7 @@ private Mono<Long> doClear(byte[] pattern, ReactiveRedisConnection connection) {
784785
keys = commands.scan(ScanOptions.scanOptions().count(clearBatchSize).match(pattern).build());
785786
}
786787

787-
return keys
788-
.buffer(clearBatchSize) //
788+
return keys.buffer(clearBatchSize) //
789789
.flatMap(commands::mUnlink) //
790790
.collect(Collectors.summingLong(Long::longValue));
791791
}
@@ -840,8 +840,7 @@ private Mono<Void> waitForLock(ReactiveRedisConnection connection, String cacheN
840840
.then();
841841
}
842842

843-
private <T> CompletableFuture<T> doWithConnection(
844-
Function<ReactiveRedisConnection, Mono<T>> callback) {
843+
private <T> CompletableFuture<T> doWithConnection(Function<ReactiveRedisConnection, Mono<T>> callback) {
845844

846845
ReactiveRedisConnectionFactory cf = (ReactiveRedisConnectionFactory) connectionFactory;
847846

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -778,11 +778,6 @@ public Boolean set(byte[] key, byte[] value) {
778778
return convertAndReturn(delegate.set(key, value), Converters.identityConverter());
779779
}
780780

781-
@Override
782-
public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option) {
783-
return convertAndReturn(delegate.set(key, value, expiration, option), Converters.identityConverter());
784-
}
785-
786781
@Override
787782
public byte[] setGet(byte[] key, byte[] value, Expiration expiration, SetOption option) {
788783
return convertAndReturn(delegate.setGet(key, value, expiration, option), Converters.identityConverter());

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -373,20 +373,6 @@ default byte[] setGet(byte[] key, byte[] value, SetCondition condition, Expirati
373373
return stringCommands().setGet(key, value, condition, expiration);
374374
}
375375

376-
/** @deprecated in favor of {@link RedisConnection#stringCommands()}}. */
377-
@Override
378-
@Deprecated
379-
default Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option) {
380-
return stringCommands().set(key, value, expiration, option);
381-
}
382-
383-
/** @deprecated in favor of {@link RedisConnection#stringCommands()}}. */
384-
@Override
385-
@Deprecated
386-
default byte[] setGet(byte[] key, byte[] value, Expiration expiration, SetOption option) {
387-
return stringCommands().setGet(key, value, expiration, option);
388-
}
389-
390376
/** @deprecated in favor of {@link RedisConnection#stringCommands()}}. */
391377
@Override
392378
@Deprecated

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ public interface ReactiveStringCommands {
6363
class SetCommand extends KeyCommand {
6464

6565
private final @Nullable ByteBuffer value;
66-
private final @Nullable Expiration expiration;
67-
@Deprecated(since = "4.1")
66+
private final Expiration expiration;
6867
private final @Nullable SetOption option;
69-
private final @Nullable SetCondition condition;
68+
private final SetCondition condition;
7069

71-
private SetCommand(@Nullable ByteBuffer key, @Nullable ByteBuffer value,
72-
@Nullable Expiration expiration, @Nullable SetOption option,
73-
@Nullable SetCondition condition) {
70+
private SetCommand(@Nullable ByteBuffer key, @Nullable ByteBuffer value, Expiration expiration,
71+
@Nullable SetOption option, SetCondition condition) {
7472

7573
super(key);
7674

@@ -90,7 +88,7 @@ public static SetCommand set(ByteBuffer key) {
9088

9189
Assert.notNull(key, "Key must not be null");
9290

93-
return new SetCommand(key, null, null, null, null);
91+
return new SetCommand(key, null, Expiration.persistent(), null, SetCondition.upsert());
9492
}
9593

9694
/**
@@ -120,7 +118,8 @@ public SetCommand expiring(Expiration expiration) {
120118
}
121119

122120
/**
123-
* Applies {@link SetOption}. Constructs a new command instance with all previously configured properties.
121+
* Applies {@link SetOption} and {@link SetCondition}. Constructs a new command instance with all previously
122+
* configured properties.
124123
*
125124
* @param option must not be {@literal null}.
126125
* @return a new {@link SetCommand} with {@link SetOption} applied.
@@ -131,14 +130,15 @@ public SetCommand withSetOption(SetOption option) {
131130

132131
Assert.notNull(option, "SetOption must not be null");
133132

134-
return new SetCommand(getKey(), value, expiration, option, condition);
133+
return new SetCommand(getKey(), value, expiration, option, option.toSetCondition());
135134
}
136135

137136
/**
138137
* Applies {@link SetCondition}. Constructs a new command instance with all previously configured properties.
139138
*
140139
* @param condition must not be {@literal null}.
141140
* @return a new {@link SetCommand} with {@link SetCondition} applied.
141+
* @since 4.1
142142
*/
143143
public SetCommand withCondition(SetCondition condition) {
144144

@@ -155,14 +155,13 @@ public SetCommand withCondition(SetCondition condition) {
155155
}
156156

157157
/**
158-
* @return optional of expiration.
158+
* @return optional expiration.
159159
*/
160160
public Optional<Expiration> getExpiration() {
161161
return Optional.ofNullable(expiration);
162162
}
163163

164164
/**
165-
* @return
166165
* @deprecated since 4.1 in favor of {@link #getCondition()}.
167166
*/
168167
@Deprecated(since = "4.1")
@@ -171,7 +170,7 @@ public Optional<SetOption> getOption() {
171170
}
172171

173172
/**
174-
* @return optional of command condition.
173+
* @return optional command condition.
175174
*/
176175
public Optional<SetCondition> getCondition() {
177176
return Optional.ofNullable(condition);
@@ -192,8 +191,7 @@ default Mono<Boolean> set(ByteBuffer key, ByteBuffer value) {
192191
Assert.notNull(key, "Key must not be null");
193192
Assert.notNull(value, "Value must not be null");
194193

195-
return set(Mono.just(SetCommand.set(key).value(value).withCondition(SetCondition.upsert())))
196-
.next()
194+
return set(Mono.just(SetCommand.set(key).value(value).withCondition(SetCondition.upsert()))).next()
197195
.map(BooleanResponse::getOutput);
198196
}
199197

@@ -231,14 +229,13 @@ default Mono<Boolean> set(ByteBuffer key, ByteBuffer value, Expiration expiratio
231229
* @see <a href="https://redis.io/commands/set">Redis Documentation: SET</a>
232230
* @since 4.1
233231
*/
234-
default Mono<Boolean> set(ByteBuffer key, ByteBuffer value, SetCondition condition, Expiration expiration) {
232+
default Mono<Boolean> set(ByteBuffer key, ByteBuffer value, SetCondition condition, Expiration expiration) {
235233

236234
Assert.notNull(key, "Key must not be null");
237235
Assert.notNull(condition, "SetCondition must not be null");
238236
Assert.notNull(expiration, "Expiration must not be null");
239237

240-
return set(Mono.just(SetCommand.set(key).value(value).withCondition(condition).expiring(expiration)))
241-
.next()
238+
return set(Mono.just(SetCommand.set(key).value(value).withCondition(condition).expiring(expiration))).next()
242239
.map(BooleanResponse::getOutput);
243240
}
244241

@@ -277,15 +274,14 @@ default Mono<ByteBuffer> setGet(ByteBuffer key, ByteBuffer value, Expiration exp
277274
}
278275

279276
/**
280-
* Set value with {@link SetCondition} and {@link Expiration} for {@code key}. Return the old string
281-
* stored at key, or empty if key did not exist. An error is returned and SET aborted if the value stored at key is
282-
* not a string.
277+
* Set value with {@link SetCondition} and {@link Expiration} for {@code key}. Return the old string stored at key, or
278+
* empty if key did not exist. An error is returned and SET aborted if the value stored at key is not a string.
283279
*
284280
* @param key must not be {@literal null}.
285281
* @param value must not be {@literal null}.
286282
* @param condition must not be {@literal null}.
287-
* @param expiration must not be {@literal null}. Use {@link Expiration#persistent()} for no expiration time
288-
* or {@link Expiration#keepTtl()} to keep the existing.
283+
* @param expiration must not be {@literal null}. Use {@link Expiration#persistent()} for no expiration time or
284+
* {@link Expiration#keepTtl()} to keep the existing.
289285
* @return the old value stored at key, or empty if key did not exist.
290286
* @see <a href="https://redis.io/commands/set">Redis Documentation: SET</a>
291287
* @since 4.1
@@ -297,8 +293,7 @@ default Mono<ByteBuffer> setGet(ByteBuffer key, ByteBuffer value, SetCondition c
297293
Assert.notNull(condition, "SetCondition must not be null");
298294
Assert.notNull(expiration, "Expiration must not be null");
299295

300-
return setGet(Mono.just(SetCommand.set(key).value(value).withCondition(condition).expiring(expiration)))
301-
.next()
296+
return setGet(Mono.just(SetCommand.set(key).value(value).withCondition(condition).expiring(expiration))).next()
302297
.map(CommandResponse::getOutput);
303298
}
304299

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,40 +142,46 @@ enum BitOperation {
142142
* @deprecated since 4.1 in favor of {@link #set(byte[], byte[], SetCondition, Expiration)}.
143143
*/
144144
@Deprecated(since = "4.1")
145-
Boolean set(byte @NonNull [] key, byte @NonNull [] value, @NonNull Expiration expiration, @NonNull SetOption option);
145+
default Boolean set(byte @NonNull [] key, byte @NonNull [] value, @NonNull Expiration expiration,
146+
@NonNull SetOption option) {
147+
return set(key, value, option != null ? option.toSetCondition() : SetCondition.upsert(), expiration);
148+
}
146149

147150
/**
148-
* Set {@code value} for {@code key}. Return the old string stored at key, or {@literal null} if key did not exist. An
149-
* error is returned and SET aborted if the value stored at key is not a string.
151+
* Set {@code value} for {@code key} applying timeouts from {@code expiration} if set and inserting/updating values
152+
* depending on {@code option}.
150153
*
151154
* @param key must not be {@literal null}.
152155
* @param value must not be {@literal null}.
156+
* @param condition must not be {@literal null}.
153157
* @param expiration must not be {@literal null}. Use {@link Expiration#persistent()} to not set any ttl or
154158
* {@link Expiration#keepTtl()} to keep the existing expiration.
155-
* @param option must not be {@literal null}. Use {@link SetOption#upsert()} to add non-existing.
156159
* @return {@literal null} when used in pipeline / transaction.
157-
* @since 3.5
158160
* @see <a href="https://redis.io/commands/set">Redis Documentation: SET</a>
159-
* @deprecated since 4.1 in favor of {@link #set(byte[], byte[], SetCondition, Expiration)}.
161+
* @since 4.1
160162
*/
161-
@Deprecated(since = "4.1")
162-
byte[] setGet(byte @NonNull [] key, byte @NonNull [] value, @NonNull Expiration expiration,
163-
@NonNull SetOption option);
163+
Boolean set(byte @NonNull [] key, byte @NonNull [] value, @NonNull SetCondition condition,
164+
@NonNull Expiration expiration);
164165

165166
/**
166-
* Set {@code value} for {@code key} applying timeouts from {@code expiration} if set and inserting/updating values
167-
* depending on {@code option}.
167+
* Set {@code value} for {@code key}. Return the old string stored at key, or {@literal null} if key did not exist. An
168+
* error is returned and SET aborted if the value stored at key is not a string.
168169
*
169170
* @param key must not be {@literal null}.
170171
* @param value must not be {@literal null}.
171-
* @param condition must not be {@literal null}.
172172
* @param expiration must not be {@literal null}. Use {@link Expiration#persistent()} to not set any ttl or
173173
* {@link Expiration#keepTtl()} to keep the existing expiration.
174+
* @param option must not be {@literal null}. Use {@link SetOption#upsert()} to add non-existing.
174175
* @return {@literal null} when used in pipeline / transaction.
176+
* @since 3.5
175177
* @see <a href="https://redis.io/commands/set">Redis Documentation: SET</a>
176-
* @since 4.1
178+
* @deprecated since 4.1 in favor of {@link #set(byte[], byte[], SetCondition, Expiration)}.
177179
*/
178-
Boolean set(byte @NonNull [] key, byte @NonNull [] value, @NonNull SetCondition condition, @NonNull Expiration expiration);
180+
@Deprecated(since = "4.1")
181+
default byte[] setGet(byte @NonNull [] key, byte @NonNull [] value, @NonNull Expiration expiration,
182+
@NonNull SetOption option) {
183+
return setGet(key, value, option != null ? option.toSetCondition() : SetCondition.upsert(), expiration);
184+
}
179185

180186
/**
181187
* Set {@code value} for {@code key}. Return the old string stored at key, or {@literal null} if key did not exist. An
@@ -470,6 +476,18 @@ public static SetOption ifPresent() {
470476
public static SetOption ifAbsent() {
471477
return SET_IF_ABSENT;
472478
}
479+
480+
/**
481+
* Create {@link SetCondition} from this {@link SetOption}.
482+
*/
483+
public SetCondition toSetCondition() {
484+
return switch (this) {
485+
case UPSERT -> SetCondition.upsert();
486+
case SET_IF_ABSENT -> SetCondition.ifAbsent();
487+
case SET_IF_PRESENT -> SetCondition.ifPresent();
488+
};
489+
}
490+
473491
}
474492

475493
}

0 commit comments

Comments
 (0)