Skip to content

Commit 7f39778

Browse files
committed
Polishing.
Introduce command variants with Condition to avoid duplicate expireHashField(…) implementations. Move expireHashField to default method and rename it to applyExpiration(…). Rename Expiration to TimeToLive and methods to getTimeToLive(…). Fix since tags.
1 parent 8c5e3a2 commit 7f39778

25 files changed

+797
-521
lines changed

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

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
26+
2627
import org.springframework.core.convert.converter.Converter;
2728
import org.springframework.data.geo.Circle;
2829
import org.springframework.data.geo.Distance;
@@ -35,10 +36,19 @@
3536
import org.springframework.data.redis.connection.convert.ListConverter;
3637
import org.springframework.data.redis.connection.convert.MapConverter;
3738
import org.springframework.data.redis.connection.convert.SetConverter;
38-
import org.springframework.data.redis.connection.stream.*;
39+
import org.springframework.data.redis.connection.stream.ByteRecord;
40+
import org.springframework.data.redis.connection.stream.Consumer;
41+
import org.springframework.data.redis.connection.stream.MapRecord;
42+
import org.springframework.data.redis.connection.stream.PendingMessages;
43+
import org.springframework.data.redis.connection.stream.PendingMessagesSummary;
44+
import org.springframework.data.redis.connection.stream.ReadOffset;
45+
import org.springframework.data.redis.connection.stream.RecordId;
3946
import org.springframework.data.redis.connection.stream.StreamInfo.XInfoConsumers;
4047
import org.springframework.data.redis.connection.stream.StreamInfo.XInfoGroups;
4148
import org.springframework.data.redis.connection.stream.StreamInfo.XInfoStream;
49+
import org.springframework.data.redis.connection.stream.StreamOffset;
50+
import org.springframework.data.redis.connection.stream.StreamReadOptions;
51+
import org.springframework.data.redis.connection.stream.StringRecord;
4252
import org.springframework.data.redis.connection.zset.Aggregate;
4353
import org.springframework.data.redis.connection.zset.DefaultTuple;
4454
import org.springframework.data.redis.connection.zset.Tuple;
@@ -2561,35 +2571,36 @@ public Cursor<Entry<byte[], byte[]>> hScan(byte[] key, ScanOptions options) {
25612571
return this.delegate.hScan(key, options);
25622572
}
25632573

2564-
@Nullable
25652574
@Override
25662575
public Long hStrLen(byte[] key, byte[] field) {
25672576
return convertAndReturn(delegate.hStrLen(key, field), Converters.identityConverter());
25682577
}
25692578

2570-
public @Nullable List<Long> expireHashField(byte[] key, org.springframework.data.redis.core.types.Expiration expiration,
2579+
public @Nullable List<Long> applyExpiration(byte[] key,
2580+
org.springframework.data.redis.core.types.Expiration expiration,
25712581
FieldExpirationOptions options, byte[]... fields) {
2572-
return this.delegate.expireHashField(key, expiration, options, fields);
2582+
return this.delegate.applyExpiration(key, expiration, options, fields);
25732583
}
25742584

25752585
@Override
2576-
public List<Long> hExpire(byte[] key, long seconds, byte[]... fields) {
2577-
return this.delegate.hExpire(key, seconds, fields);
2586+
public List<Long> hExpire(byte[] key, long seconds, FieldExpirationOptions.Condition condition, byte[]... fields) {
2587+
return this.delegate.hExpire(key, seconds, condition, fields);
25782588
}
25792589

25802590
@Override
2581-
public List<Long> hpExpire(byte[] key, long millis, byte[]... fields) {
2582-
return this.delegate.hpExpire(key, millis, fields);
2591+
public List<Long> hpExpire(byte[] key, long millis, FieldExpirationOptions.Condition condition, byte[]... fields) {
2592+
return this.delegate.hpExpire(key, millis, condition, fields);
25832593
}
25842594

25852595
@Override
2586-
public List<Long> hExpireAt(byte[] key, long unixTime, byte[]... fields) {
2587-
return this.delegate.hExpireAt(key, unixTime, fields);
2596+
public List<Long> hExpireAt(byte[] key, long unixTime, FieldExpirationOptions.Condition condition, byte[]... fields) {
2597+
return this.delegate.hExpireAt(key, unixTime, condition, fields);
25882598
}
25892599

25902600
@Override
2591-
public List<Long> hpExpireAt(byte[] key, long unixTimeInMillis, byte[]... fields) {
2592-
return this.delegate.hpExpireAt(key, unixTimeInMillis, fields);
2601+
public List<Long> hpExpireAt(byte[] key, long unixTimeInMillis, FieldExpirationOptions.Condition condition,
2602+
byte[]... fields) {
2603+
return this.delegate.hpExpireAt(key, unixTimeInMillis, condition, fields);
25932604
}
25942605

25952606
@Override
@@ -2612,29 +2623,31 @@ public List<Long> hTtl(byte[] key, TimeUnit timeUnit, byte[]... fields) {
26122623
return this.delegate.hTtl(key, timeUnit, fields);
26132624
}
26142625

2615-
public @Nullable List<Long> expireHashField(String key, org.springframework.data.redis.core.types.Expiration expiration,
2626+
public @Nullable List<Long> applyExpiration(String key,
2627+
org.springframework.data.redis.core.types.Expiration expiration,
26162628
FieldExpirationOptions options, String... fields) {
2617-
return expireHashField(serialize(key), expiration, options, serializeMulti(fields));
2629+
return applyExpiration(serialize(key), expiration, options, serializeMulti(fields));
26182630
}
26192631

26202632
@Override
2621-
public List<Long> hExpire(String key, long seconds, String... fields) {
2622-
return hExpire(serialize(key), seconds, serializeMulti(fields));
2633+
public List<Long> hExpire(String key, long seconds, FieldExpirationOptions.Condition condition, String... fields) {
2634+
return hExpire(serialize(key), seconds, condition, serializeMulti(fields));
26232635
}
26242636

26252637
@Override
2626-
public List<Long> hpExpire(String key, long millis, String... fields) {
2627-
return hpExpire(serialize(key), millis, serializeMulti(fields));
2638+
public List<Long> hpExpire(String key, long millis, FieldExpirationOptions.Condition condition, String... fields) {
2639+
return hpExpire(serialize(key), millis, condition, serializeMulti(fields));
26282640
}
26292641

26302642
@Override
2631-
public List<Long> hExpireAt(String key, long unixTime, String... fields) {
2632-
return hExpireAt(serialize(key), unixTime, serializeMulti(fields));
2643+
public List<Long> hExpireAt(String key, long unixTime, FieldExpirationOptions.Condition condition, String... fields) {
2644+
return hExpireAt(serialize(key), unixTime, condition, serializeMulti(fields));
26332645
}
26342646

26352647
@Override
2636-
public List<Long> hpExpireAt(String key, long unixTimeInMillis, String... fields) {
2637-
return hpExpireAt(serialize(key), unixTimeInMillis, serializeMulti(fields));
2648+
public List<Long> hpExpireAt(String key, long unixTimeInMillis, FieldExpirationOptions.Condition condition,
2649+
String... fields) {
2650+
return hpExpireAt(serialize(key), unixTimeInMillis, condition, serializeMulti(fields));
26382651
}
26392652

26402653
@Override

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,28 +1476,58 @@ default Long hStrLen(byte[] key, byte[] field) {
14761476
@Override
14771477
@Deprecated
14781478
default List<Long> hExpire(byte[] key, long seconds, byte[]... fields) {
1479-
return hashCommands().hExpire(key, seconds, fields);
1479+
return hashCommands().hExpire(key, seconds, FieldExpirationOptions.Condition.ALWAYS, fields);
1480+
}
1481+
1482+
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
1483+
@Override
1484+
@Deprecated
1485+
default List<Long> hExpire(byte[] key, long seconds, FieldExpirationOptions.Condition condition, byte[]... fields) {
1486+
return hashCommands().hExpire(key, seconds, condition, fields);
14801487
}
14811488

14821489
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
14831490
@Override
14841491
@Deprecated
14851492
default List<Long> hpExpire(byte[] key, long millis, byte[]... fields) {
1486-
return hashCommands().hpExpire(key, millis, fields);
1493+
return hashCommands().hpExpire(key, millis, FieldExpirationOptions.Condition.ALWAYS, fields);
1494+
}
1495+
1496+
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
1497+
@Override
1498+
@Deprecated
1499+
default List<Long> hpExpire(byte[] key, long millis, FieldExpirationOptions.Condition condition, byte[]... fields) {
1500+
return hashCommands().hpExpire(key, millis, condition, fields);
14871501
}
14881502

14891503
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
14901504
@Override
14911505
@Deprecated
14921506
default List<Long> hExpireAt(byte[] key, long unixTime, byte[]... fields) {
1493-
return hashCommands().hExpireAt(key, unixTime, fields);
1507+
return hashCommands().hExpireAt(key, unixTime, FieldExpirationOptions.Condition.ALWAYS, fields);
1508+
}
1509+
1510+
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
1511+
@Override
1512+
@Deprecated
1513+
default List<Long> hExpireAt(byte[] key, long unixTime, FieldExpirationOptions.Condition condition,
1514+
byte[]... fields) {
1515+
return hashCommands().hExpireAt(key, unixTime, condition, fields);
14941516
}
14951517

14961518
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
14971519
@Override
14981520
@Deprecated
14991521
default List<Long> hpExpireAt(byte[] key, long unixTimeInMillis, byte[]... fields) {
1500-
return hashCommands().hpExpireAt(key, unixTimeInMillis, fields);
1522+
return hashCommands().hpExpireAt(key, unixTimeInMillis, FieldExpirationOptions.Condition.ALWAYS, fields);
1523+
}
1524+
1525+
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
1526+
@Override
1527+
@Deprecated
1528+
default List<Long> hpExpireAt(byte[] key, long unixTimeInMillis, FieldExpirationOptions.Condition condition,
1529+
byte[]... fields) {
1530+
return hashCommands().hpExpireAt(key, unixTimeInMillis, condition, fields);
15011531
}
15021532

15031533
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
@@ -1531,9 +1561,10 @@ default List<Long> hpTtl(byte[] key, byte[]... fields) {
15311561
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
15321562
@Override
15331563
@Deprecated
1534-
default @Nullable List<Long> expireHashField(byte[] key, org.springframework.data.redis.core.types.Expiration expiration,
1535-
FieldExpirationOptions options, byte[]... fields) {
1536-
return hashCommands().expireHashField(key, expiration, options, fields);
1564+
default @Nullable List<Long> applyExpiration(byte[] key,
1565+
org.springframework.data.redis.core.types.Expiration expiration, FieldExpirationOptions options,
1566+
byte[]... fields) {
1567+
return hashCommands().applyExpiration(key, expiration, options, fields);
15371568
}
15381569

15391570
// GEO COMMANDS
@@ -1907,9 +1938,8 @@ default <T> T evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte
19071938
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
19081939
@Override
19091940
@Deprecated
1910-
default Long zRangeStoreByLex(byte[] dstKey, byte[] srcKey,
1911-
org.springframework.data.domain.Range<byte[]> range,
1912-
org.springframework.data.redis.connection.Limit limit) {
1941+
default Long zRangeStoreByLex(byte[] dstKey, byte[] srcKey, org.springframework.data.domain.Range<byte[]> range,
1942+
org.springframework.data.redis.connection.Limit limit) {
19131943
return zSetCommands().zRangeStoreByLex(dstKey, srcKey, range, limit);
19141944
}
19151945

@@ -1926,7 +1956,7 @@ default Long zRangeStoreRevByLex(byte[] dstKey, byte[] srcKey, org.springframewo
19261956
@Deprecated
19271957
default Long zRangeStoreByScore(byte[] dstKey, byte[] srcKey,
19281958
org.springframework.data.domain.Range<? extends Number> range,
1929-
org.springframework.data.redis.connection.Limit limit) {
1959+
org.springframework.data.redis.connection.Limit limit) {
19301960
return zSetCommands().zRangeStoreByScore(dstKey, srcKey, range, limit);
19311961
}
19321962

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Objects;
1919

2020
import org.springframework.lang.Contract;
21-
import org.springframework.lang.Nullable;
2221
import org.springframework.util.ObjectUtils;
2322

2423
/**
@@ -29,23 +28,22 @@ public interface Hash {
2928

3029
class FieldExpirationOptions {
3130

32-
private static final FieldExpirationOptions NONE = new FieldExpirationOptions(null);
33-
private @Nullable Condition condition;
31+
private static final FieldExpirationOptions NONE = new FieldExpirationOptions(Condition.ALWAYS);
32+
private final Condition condition;
3433

35-
FieldExpirationOptions(@Nullable Condition condition) {
34+
FieldExpirationOptions(Condition condition) {
3635
this.condition = condition;
3736
}
3837

3938
public static FieldExpirationOptions none() {
4039
return NONE;
4140
}
4241

43-
@Contract("_ -> new")
4442
public static FieldExpireOptionsBuilder builder() {
4543
return new FieldExpireOptionsBuilder();
4644
}
4745

48-
public @Nullable Condition getCondition() {
46+
public Condition getCondition() {
4947
return condition;
5048
}
5149

@@ -68,47 +66,63 @@ public int hashCode() {
6866

6967
public static class FieldExpireOptionsBuilder {
7068

71-
@Nullable Condition condition;
69+
private Condition condition = Condition.ALWAYS;
7270

73-
@Contract("_ -> this")
71+
@Contract("-> this")
7472
public FieldExpireOptionsBuilder nx() {
7573
this.condition = Condition.NX;
7674
return this;
7775
}
7876

79-
@Contract("_ -> this")
77+
@Contract("-> this")
8078
public FieldExpireOptionsBuilder xx() {
8179
this.condition = Condition.XX;
8280
return this;
8381
}
8482

85-
@Contract("_ -> this")
83+
@Contract("-> this")
8684
public FieldExpireOptionsBuilder gt() {
8785
this.condition = Condition.GT;
8886
return this;
8987
}
9088

91-
@Contract("_ -> this")
89+
@Contract("-> this")
9290
public FieldExpireOptionsBuilder lt() {
9391
this.condition = Condition.LT;
9492
return this;
9593
}
9694

97-
@Contract("_ -> !null")
9895
public FieldExpirationOptions build() {
99-
return condition == null ? NONE : new FieldExpirationOptions(condition);
96+
return condition == Condition.ALWAYS ? NONE : new FieldExpirationOptions(condition);
10097
}
98+
10199
}
102100

103101
public enum Condition {
104102

105-
/** Set expiration only when the field has no expiration. */
103+
/**
104+
* Always apply expiration.
105+
*/
106+
ALWAYS,
107+
108+
/**
109+
* Set expiration only when the field has no expiration.
110+
*/
106111
NX,
107-
/** Set expiration only when the field has an existing expiration. */
112+
113+
/**
114+
* Set expiration only when the field has an existing expiration.
115+
*/
108116
XX,
109-
/** Set expiration only when the new expiration is greater than current one. */
117+
118+
/**
119+
* Set expiration only when the new expiration is greater than current one.
120+
*/
110121
GT,
111-
/** Set expiration only when the new expiration is greater than current one. */
122+
123+
/**
124+
* Set expiration only when the new expiration is greater than current one.
125+
*/
112126
LT
113127
}
114128
}

0 commit comments

Comments
 (0)