Skip to content

Commit c3c1e46

Browse files
committed
Polishing.
1 parent 7f39778 commit c3c1e46

File tree

12 files changed

+71
-52
lines changed

12 files changed

+71
-52
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
* @author Andrey Shlykov
7272
* @author ihaohong
7373
* @author Shyngys Sapraliyev
74-
*
7574
* @see RedisCallback
7675
* @see RedisSerializer
7776
* @see StringRedisTemplate
@@ -1661,7 +1660,6 @@ default Long lPos(String key, String element) {
16611660
*/
16621661
Long zRemRange(String key, long start, long end);
16631662

1664-
16651663
/**
16661664
* Remove all elements between the lexicographical {@link Range}.
16671665
*
@@ -1941,7 +1939,8 @@ default Set<StringTuple> zUnionWithScores(Aggregate aggregate, int[] weights, St
19411939
* @return
19421940
* @since 1.6
19431941
* @see <a href="https://redis.io/commands/zrangebylex">Redis Documentation: ZRANGEBYLEX</a>
1944-
* @see RedisZSetCommands#zRangeByLex(byte[], org.springframework.data.domain.Range, org.springframework.data.redis.connection.Limit)
1942+
* @see RedisZSetCommands#zRangeByLex(byte[], org.springframework.data.domain.Range,
1943+
* org.springframework.data.redis.connection.Limit)
19451944
*/
19461945
Set<String> zRangeByLex(String key, org.springframework.data.domain.Range<String> range,
19471946
org.springframework.data.redis.connection.Limit limit);
@@ -1983,7 +1982,8 @@ default Set<String> zRevRangeByLex(String key, org.springframework.data.domain.R
19831982
* @return
19841983
* @since 2.4
19851984
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
1986-
* @see RedisZSetCommands#zRevRangeByLex(byte[], org.springframework.data.domain.Range, org.springframework.data.redis.connection.Limit)
1985+
* @see RedisZSetCommands#zRevRangeByLex(byte[], org.springframework.data.domain.Range,
1986+
* org.springframework.data.redis.connection.Limit)
19871987
*/
19881988
Set<String> zRevRangeByLex(String key, org.springframework.data.domain.Range<String> range,
19891989
org.springframework.data.redis.connection.Limit limit);
@@ -2498,9 +2498,10 @@ List<Long> hpExpireAt(String key, long unixTimeInMillis, Hash.FieldExpirationOpt
24982498
*
24992499
* @param key must not be {@literal null}.
25002500
* @param fields must not be {@literal null}.
2501-
* @return a list of {@link Long} values for each of the fields provided: the time to live in milliseconds; or a negative value
2502-
* to signal an error. The command returns {@code -1} if the key exists but has no associated expiration time.
2503-
* The command returns {@code -2} if the key does not exist; {@literal null} when used in pipeline / transaction.
2501+
* @return a list of {@link Long} values for each of the fields provided: the time to live in milliseconds; or a
2502+
* negative value to signal an error. The command returns {@code -1} if the key exists but has no associated
2503+
* expiration time. The command returns {@code -2} if the key does not exist; {@literal null} when used in
2504+
* pipeline / transaction.
25042505
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HTTL</a>
25052506
* @since 3.5
25062507
*/
@@ -2513,9 +2514,10 @@ List<Long> hpExpireAt(String key, long unixTimeInMillis, Hash.FieldExpirationOpt
25132514
* @param key must not be {@literal null}.
25142515
* @param timeUnit must not be {@literal null}.
25152516
* @param fields must not be {@literal null}.
2516-
* @return a list of {@link Long} values for each of the fields provided: the time to live in the {@link TimeUnit} provided; or a negative value
2517-
* to signal an error. The command returns {@code -1} if the key exists but has no associated expiration time.
2518-
* The command returns {@code -2} if the key does not exist; {@literal null} when used in pipeline / transaction.
2517+
* @return a list of {@link Long} values for each of the fields provided: the time to live in the {@link TimeUnit}
2518+
* provided; or a negative value to signal an error. The command returns {@code -1} if the key exists but has
2519+
* no associated expiration time. The command returns {@code -2} if the key does not exist; {@literal null}
2520+
* when used in pipeline / transaction.
25192521
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HTTL</a>
25202522
* @since 3.5
25212523
*/
@@ -2527,9 +2529,10 @@ List<Long> hpExpireAt(String key, long unixTimeInMillis, Hash.FieldExpirationOpt
25272529
*
25282530
* @param key must not be {@literal null}.
25292531
* @param fields must not be {@literal null}.
2530-
* @return a list of {@link Long} values for each of the fields provided: the time to live in milliseconds; or a negative value
2531-
* to signal an error. The command returns {@code -1} if the key exists but has no associated expiration time.
2532-
* The command returns {@code -2} if the key does not exist; {@literal null} when used in pipeline / transaction.
2532+
* @return a list of {@link Long} values for each of the fields provided: the time to live in milliseconds; or a
2533+
* negative value to signal an error. The command returns {@code -1} if the key exists but has no associated
2534+
* expiration time. The command returns {@code -2} if the key does not exist; {@literal null} when used in
2535+
* pipeline / transaction.
25332536
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HTTL</a>
25342537
* @since 3.5
25352538
*/
@@ -2759,8 +2762,7 @@ GeoResults<GeoLocation<String>> geoRadiusByMember(String key, String member, Dis
27592762

27602763
/**
27612764
* Return the members of a geo set which are within the borders of the area specified by a given {@link GeoShape
2762-
* shape}. The query's center point is provided by
2763-
* {@link GeoReference}.
2765+
* shape}. The query's center point is provided by {@link GeoReference}.
27642766
*
27652767
* @param key must not be {@literal null}.
27662768
* @param reference must not be {@literal null}.
@@ -2776,8 +2778,7 @@ GeoResults<GeoLocation<String>> geoSearch(String key, GeoReference<String> refer
27762778

27772779
/**
27782780
* Query the members of a geo set which are within the borders of the area specified by a given {@link GeoShape shape}
2779-
* and store the result at {@code destKey}. The query's center point is provided by
2780-
* {@link GeoReference}.
2781+
* and store the result at {@code destKey}. The query's center point is provided by {@link GeoReference}.
27812782
*
27822783
* @param key must not be {@literal null}.
27832784
* @param reference must not be {@literal null}.

src/main/java/org/springframework/data/redis/core/BoundHashOperations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.springframework.data.redis.connection.Hash.FieldExpirationOptions;
2727
import org.springframework.data.redis.core.types.Expiration;
28+
import org.springframework.data.redis.core.types.Expirations;
2829
import org.springframework.lang.Nullable;
2930

3031
/**

src/main/java/org/springframework/data/redis/core/DefaultHashOperations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
import org.springframework.core.convert.converter.Converter;
3030
import org.springframework.data.redis.connection.Hash.FieldExpirationOptions;
3131
import org.springframework.data.redis.connection.convert.Converters;
32-
import org.springframework.data.redis.core.Expirations.Timeouts;
3332
import org.springframework.data.redis.core.types.Expiration;
33+
import org.springframework.data.redis.core.types.Expirations;
34+
import org.springframework.data.redis.core.types.Expirations.Timeouts;
3435
import org.springframework.lang.Nullable;
3536
import org.springframework.util.Assert;
3637

src/main/java/org/springframework/data/redis/core/DefaultReactiveHashOperations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
import org.springframework.data.redis.connection.ReactiveHashCommands.ExpireCommand;
3737
import org.springframework.data.redis.connection.ReactiveRedisConnection.NumericResponse;
3838
import org.springframework.data.redis.connection.convert.Converters;
39-
import org.springframework.data.redis.core.Expirations.Timeouts;
4039
import org.springframework.data.redis.core.types.Expiration;
40+
import org.springframework.data.redis.core.types.Expirations;
41+
import org.springframework.data.redis.core.types.Expirations.Timeouts;
4142
import org.springframework.data.redis.serializer.RedisSerializationContext;
4243
import org.springframework.lang.Nullable;
4344
import org.springframework.util.Assert;

src/main/java/org/springframework/data/redis/core/HashOperations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.springframework.data.redis.connection.Hash.FieldExpirationOptions;
2727
import org.springframework.data.redis.core.types.Expiration;
28+
import org.springframework.data.redis.core.types.Expirations;
2829
import org.springframework.lang.Nullable;
2930

3031
/**

src/main/java/org/springframework/data/redis/core/ReactiveHashOperations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.data.redis.connection.Hash.FieldExpirationOptions;
3030
import org.springframework.data.redis.core.types.Expiration;
31+
import org.springframework.data.redis.core.types.Expirations;
3132
import org.springframework.lang.Nullable;
3233

3334
/**

src/main/java/org/springframework/data/redis/core/Expirations.java renamed to src/main/java/org/springframework/data/redis/core/types/Expirations.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.redis.core;
16+
package org.springframework.data.redis.core.types;
1717

1818
import java.time.Duration;
19+
import java.util.Collection;
1920
import java.util.LinkedHashSet;
2021
import java.util.List;
2122
import java.util.Map;
23+
import java.util.NoSuchElementException;
2224
import java.util.Objects;
2325
import java.util.Set;
2426
import java.util.concurrent.TimeUnit;
@@ -39,9 +41,10 @@
3941
* </ol>
4042
*
4143
* @author Christoph Strobl
44+
* @author Mark Paluch
4245
* @since 3.5
4346
*/
44-
public class Expirations<K> { // TODO: should we move this to let's say Hash.class or another place
47+
public class Expirations<K> {
4548

4649
private final TimeUnit unit;
4750
private final Map<K, TimeToLive> expirations;
@@ -87,15 +90,15 @@ public Set<K> persistent() {
8790
}
8891

8992
/**
90-
* @return an ordered set of keys that do not exists and therefore do not have a time to live.
93+
* @return an ordered set of keys that do not exist and therefore do not have a time to live.
9194
*/
9295
public Set<K> missing() {
9396
return filterByState(TimeToLive.MISSING);
9497
}
9598

9699
/**
97100
* @return an ordered set of all {@link Expirations expirations} where the {@link TimeToLive#value()} is using the
98-
* {@link TimeUnit} defined in {@link #precision()}.
101+
* {@link TimeUnit} defined in {@link #timeUnit()}.
99102
*/
100103
public List<TimeToLive> ttl() {
101104
return expirations.values().stream().map(it -> it.convert(this.unit)).toList();
@@ -104,7 +107,7 @@ public List<TimeToLive> ttl() {
104107
/**
105108
* @return the {@link TimeUnit} for {@link TimeToLive expirations} held by this instance.
106109
*/
107-
public TimeUnit precision() {
110+
public TimeUnit timeUnit() {
108111
return unit;
109112
}
110113

@@ -114,21 +117,30 @@ public TimeUnit precision() {
114117
* skipped.
115118
*/
116119
public List<Map.Entry<K, Duration>> expiring() {
120+
117121
return expirations.entrySet().stream().filter(it -> !it.getValue().isMissing() && !it.getValue().isPersistent())
118122
.map(it -> Map.entry(it.getKey(), toDuration(it.getValue()))).toList();
119123
}
120124

125+
/**
126+
* @return the ordered collection of keys that are associated with an expiration.
127+
*/
128+
public Collection<K> keys() {
129+
return expirations.keySet();
130+
}
131+
121132
/**
122133
* @param key
123134
* @return the {@link Expirations expirations} where the {@link TimeToLive#value()} is using the {@link TimeUnit}
124-
* defined in {@link #precision()} or {@literal null} if no entry could be found.
135+
* defined in {@link #timeUnit()} or {@literal null} if no entry could be found.
136+
* @throws NoSuchElementException if no expiration found for the given key.
125137
*/
126-
@Nullable
127138
public TimeToLive expirationOf(K key) {
128139

129140
TimeToLive timeToLive = expirations.get(key);
141+
130142
if (timeToLive == null) {
131-
return null;
143+
throw new NoSuchElementException("No expiration found for key '%s'".formatted(key));
132144
}
133145

134146
return timeToLive.convert(this.unit);
@@ -142,12 +154,7 @@ public TimeToLive expirationOf(K key) {
142154
*/
143155
@Nullable
144156
public Duration ttlOf(K key) {
145-
146-
TimeToLive timeToLive = expirationOf(key);
147-
if (timeToLive == null) {
148-
return null;
149-
}
150-
return toDuration(timeToLive);
157+
return toDuration(expirationOf(key));
151158
}
152159

153160
private Set<K> filterByState(TimeToLive filter) {
@@ -161,6 +168,7 @@ static Duration toDuration(TimeToLive timeToLive) {
161168
if (timeToLive.sourceUnit == null) {
162169
return null;
163170
}
171+
164172
return Duration.of(timeToLive.raw(), timeToLive.sourceUnit.toChronoUnit());
165173
}
166174

@@ -173,6 +181,7 @@ Long get(int index) {
173181
public int size() {
174182
return raw.size();
175183
}
184+
176185
}
177186

178187
/**
@@ -182,10 +191,10 @@ public int size() {
182191
* {@link #PERSISTENT} mark predefined states returned by Redis indicating a time to live value could not be retrieved
183192
* due to various reasons.
184193
*/
185-
public static class TimeToLive { // TODO: is Expiry a better name for this type?
194+
public static class TimeToLive {
186195

187196
/**
188-
* Predefined {@link TimeToLive} for a key that does not exists and therefore does not have a time to live.
197+
* Predefined {@link TimeToLive} for a key that does not exist and therefore does not have a time to live.
189198
*/
190199
public static TimeToLive MISSING = new TimeToLive(-2L);
191200

@@ -194,9 +203,10 @@ public static class TimeToLive { // TODO: is Expiry a better name for this type?
194203
*/
195204
public static TimeToLive PERSISTENT = new TimeToLive(-1L);
196205

206+
final @Nullable TimeUnit sourceUnit;
207+
final @Nullable TimeUnit targetUnit;
208+
197209
private final long raw;
198-
@Nullable TimeUnit sourceUnit;
199-
@Nullable TimeUnit targetUnit;
200210

201211
public TimeToLive(long value) {
202212
this(value, null);
@@ -301,5 +311,7 @@ public boolean equals(Object o) {
301311
public int hashCode() {
302312
return Objects.hash(raw);
303313
}
314+
304315
}
316+
305317
}

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import org.springframework.data.redis.connection.DataType;
2828
import org.springframework.data.redis.core.BoundHashOperations;
2929
import org.springframework.data.redis.core.Cursor;
30-
import org.springframework.data.redis.core.Expirations;
3130
import org.springframework.data.redis.core.ExpireChanges;
3231
import org.springframework.data.redis.core.RedisOperations;
3332
import org.springframework.data.redis.core.ScanOptions;
3433
import org.springframework.data.redis.core.SessionCallback;
34+
import org.springframework.data.redis.core.types.Expirations;
3535
import org.springframework.lang.Nullable;
3636

3737
/**

src/main/java/org/springframework/data/redis/support/collections/RedisMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import java.util.concurrent.ConcurrentMap;
2424
import java.util.concurrent.TimeUnit;
2525

26-
import org.springframework.data.redis.core.Expirations;
2726
import org.springframework.data.redis.core.ExpireChanges;
27+
import org.springframework.data.redis.core.types.Expirations;
2828
import org.springframework.lang.Nullable;
2929

3030
/**

src/main/java/org/springframework/data/redis/support/collections/RedisProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333

3434
import org.springframework.data.redis.connection.DataType;
3535
import org.springframework.data.redis.core.BoundHashOperations;
36-
import org.springframework.data.redis.core.Expirations;
3736
import org.springframework.data.redis.core.ExpireChanges;
3837
import org.springframework.data.redis.core.RedisOperations;
38+
import org.springframework.data.redis.core.types.Expirations;
3939
import org.springframework.lang.Nullable;
4040

4141
/**

src/test/java/org/springframework/data/redis/core/DefaultHashOperationsIntegrationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import org.springframework.data.redis.connection.Hash.FieldExpirationOptions;
3737
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
3838
import org.springframework.data.redis.connection.jedis.extension.JedisConnectionFactoryExtension;
39-
import org.springframework.data.redis.core.Expirations.TimeToLive;
4039
import org.springframework.data.redis.core.ExpireChanges.ExpiryChangeState;
40+
import org.springframework.data.redis.core.types.Expirations.TimeToLive;
4141
import org.springframework.data.redis.test.condition.EnabledOnCommand;
4242
import org.springframework.data.redis.test.extension.RedisStanalone;
4343
import org.springframework.data.redis.test.extension.parametrized.MethodSource;
@@ -230,7 +230,7 @@ void testExpireAndGetExpireMillis() {
230230
assertThat(redisTemplate.opsForHash().getTimeToLive(key, List.of(key1))).satisfies(expirations -> {
231231

232232
assertThat(expirations.missing()).isEmpty();
233-
assertThat(expirations.precision()).isEqualTo(TimeUnit.SECONDS);
233+
assertThat(expirations.timeUnit()).isEqualTo(TimeUnit.SECONDS);
234234
assertThat(expirations.expirationOf(key1)).extracting(TimeToLive::raw, InstanceOfAssertFactories.LONG)
235235
.isBetween(0L, 1L);
236236
assertThat(expirations.ttlOf(key1)).isBetween(Duration.ZERO, Duration.ofSeconds(1));
@@ -261,7 +261,7 @@ void testExpireAndGetExpireSeconds() {
261261
assertThat(redisTemplate.opsForHash().getTimeToLive(key, TimeUnit.SECONDS, List.of(key1, key2)))
262262
.satisfies(expirations -> {
263263
assertThat(expirations.missing()).isEmpty();
264-
assertThat(expirations.precision()).isEqualTo(TimeUnit.SECONDS);
264+
assertThat(expirations.timeUnit()).isEqualTo(TimeUnit.SECONDS);
265265
assertThat(expirations.expirationOf(key1)).extracting(TimeToLive::raw, InstanceOfAssertFactories.LONG)
266266
.isBetween(0L, 5L);
267267
assertThat(expirations.ttlOf(key1)).isBetween(Duration.ofSeconds(1), Duration.ofSeconds(5));
@@ -286,7 +286,7 @@ void testExpireAtAndGetExpireMillis() {
286286
assertThat(redisTemplate.opsForHash().getTimeToLive(key, TimeUnit.MILLISECONDS, List.of(key1, key2)))
287287
.satisfies(expirations -> {
288288
assertThat(expirations.missing()).isEmpty();
289-
assertThat(expirations.precision()).isEqualTo(TimeUnit.MILLISECONDS);
289+
assertThat(expirations.timeUnit()).isEqualTo(TimeUnit.MILLISECONDS);
290290
assertThat(expirations.expirationOf(key1)).extracting(TimeToLive::raw, InstanceOfAssertFactories.LONG)
291291
.isBetween(0L, 500L);
292292
assertThat(expirations.ttlOf(key1)).isBetween(Duration.ZERO, Duration.ofMillis(500));

0 commit comments

Comments
 (0)