Skip to content

Commit eac79e8

Browse files
author
codeba
committed
feat: Add setObjectEx, setObjectEXAsync methods
1 parent ef8346f commit eac79e8

File tree

5 files changed

+249
-2
lines changed

5 files changed

+249
-2
lines changed

redis-keeper-support/src/main/java/org/codeba/redis/keeper/support/DefaultRedissonTemplate.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,12 +2047,24 @@ public void setObject(String key, Object value) {
20472047
kString.setObject(key, value);
20482048
}
20492049

2050+
@Override
2051+
public void setObjectEx(String key, Object value, Duration duration) {
2052+
log("setObjectEx", key, value);
2053+
kString.setObjectEx(key, value, duration);
2054+
}
2055+
20502056
@Override
20512057
public CompletableFuture<Void> setObjectAsync(String key, Object value) {
20522058
log("setObjectAsync", key, value);
20532059
return kString.setObjectAsync(key, value);
20542060
}
20552061

2062+
@Override
2063+
public CompletableFuture<Void> setObjectEXAsync(String key, Object value, Duration duration) {
2064+
log("setObjectAsync", key, value);
2065+
return kString.setObjectEXAsync(key, value, duration);
2066+
}
2067+
20562068
@Override
20572069
public Map<String, Object> mGet(String... keys) {
20582070
log("mGet", keys);

redis-keeper-support/src/main/java/org/codeba/redis/keeper/support/KRedissonBatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**
3737
* The type K redisson batch.
3838
*/
39-
class KRedissonBatch implements KBatch {
39+
public class KRedissonBatch implements KBatch {
4040
/**
4141
* The R batch.
4242
*/

redis-keeper-support/src/main/java/org/codeba/redis/keeper/support/KRedissonString.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ public void setObject(String key, Object value) {
137137
}
138138
}
139139

140+
@Override
141+
public void setObjectEx(String key, Object value, Duration duration) {
142+
if (value instanceof String) {
143+
setEX(key, value.toString(), duration);
144+
} else if (value instanceof Integer || value instanceof Long) {
145+
final RAtomicLong rAtomicLong = getRAtomicLong(key);
146+
rAtomicLong.set(Long.parseLong(value.toString()));
147+
rAtomicLong.expire(duration.toMillis(), TimeUnit.MILLISECONDS);
148+
} else if (value instanceof Float || value instanceof Double) {
149+
final RAtomicDouble rAtomicDouble = getRAtomicDouble(key);
150+
rAtomicDouble.set(Double.parseDouble(value.toString()));
151+
rAtomicDouble.expire(duration.toMillis(), TimeUnit.MILLISECONDS);
152+
} else {
153+
getRBucket(key).set(value, duration.toMillis(), TimeUnit.MILLISECONDS);
154+
}
155+
}
156+
140157
@Override
141158
public CompletableFuture<Map<String, Object>> mGetAsync(String... keys) {
142159
return getRBuckets(getCodec()).getAsync(keys).toCompletableFuture();

redis-keeper-support/src/main/java/org/codeba/redis/keeper/support/KRedissonStringAsync.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ public CompletableFuture<Void> setObjectAsync(String key, Object value) {
137137
}
138138
}
139139

140+
@Override
141+
public CompletableFuture<Void> setObjectEXAsync(String key, Object value, Duration duration) {
142+
if (value instanceof String) {
143+
return setEXAsync(key, value.toString(), duration);
144+
} else if (value instanceof Integer || value instanceof Long) {
145+
final RAtomicLongAsync rAtomicLong = getRAtomicLong(key);
146+
return rAtomicLong.setAsync(Long.parseLong(value.toString()))
147+
.thenRunAsync(() -> rAtomicLong.expireAsync(duration.toMillis(), TimeUnit.MILLISECONDS))
148+
.toCompletableFuture();
149+
} else if (value instanceof Float || value instanceof Double) {
150+
final RAtomicDoubleAsync rAtomicDouble = getRAtomicDouble(key);
151+
return rAtomicDouble.setAsync(Double.parseDouble(value.toString()))
152+
.thenRunAsync(() -> rAtomicDouble.expireAsync(duration.toMillis(), TimeUnit.MILLISECONDS))
153+
.toCompletableFuture();
154+
} else {
155+
return getRBucket(key).setAsync(value, duration.toMillis(), TimeUnit.MILLISECONDS).toCompletableFuture();
156+
}
157+
}
158+
140159
@Override
141160
public CompletableFuture<Void> setAsync(String key, String value) {
142161
return getRBucket(key, getCodec()).setAsync(value).toCompletableFuture();

0 commit comments

Comments
 (0)