|
22 | 22 | import java.util.List; |
23 | 23 | import java.util.Map; |
24 | 24 | import java.util.concurrent.TimeUnit; |
25 | | - |
26 | | -import org.springframework.dao.DataAccessException; |
27 | 25 | import org.springframework.data.redis.connection.BitFieldSubCommands; |
28 | 26 | import org.springframework.data.redis.connection.RedisConnection; |
29 | 27 | import org.springframework.data.redis.connection.RedisStringCommands.SetOption; |
|
37 | 35 | * @author Jennifer Hickey |
38 | 36 | * @author Christoph Strobl |
39 | 37 | * @author Jiahe Cai |
| 38 | + * @author Ehsan Alemzadeh |
40 | 39 | */ |
41 | 40 | class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements ValueOperations<K, V> { |
42 | 41 |
|
@@ -250,43 +249,15 @@ public void set(K key, V value, long timeout, TimeUnit unit) { |
250 | 249 | byte[] rawKey = rawKey(key); |
251 | 250 | byte[] rawValue = rawValue(value); |
252 | 251 |
|
253 | | - execute(new RedisCallback<Object>() { |
254 | | - |
255 | | - @Override |
256 | | - public Object doInRedis(RedisConnection connection) throws DataAccessException { |
257 | | - |
258 | | - potentiallyUsePsetEx(connection); |
259 | | - return null; |
260 | | - } |
261 | | - |
262 | | - public void potentiallyUsePsetEx(RedisConnection connection) { |
263 | | - |
264 | | - if (!TimeUnit.MILLISECONDS.equals(unit) || !failsafeInvokePsetEx(connection)) { |
265 | | - connection.setEx(rawKey, TimeoutUtils.toSeconds(timeout, unit), rawValue); |
266 | | - } |
267 | | - } |
268 | | - |
269 | | - private boolean failsafeInvokePsetEx(RedisConnection connection) { |
270 | | - |
271 | | - boolean failed = false; |
272 | | - try { |
273 | | - connection.pSetEx(rawKey, timeout, rawValue); |
274 | | - } catch (UnsupportedOperationException ignore) { |
275 | | - // in case the connection does not support pSetEx return false to allow fallback to other operation. |
276 | | - failed = true; |
277 | | - } |
278 | | - return !failed; |
279 | | - } |
280 | | - |
281 | | - }); |
| 252 | + execute(connection -> connection.set(rawKey, rawValue, Expiration.from(timeout, unit), SetOption.upsert())); |
282 | 253 | } |
283 | 254 |
|
284 | 255 | @Override |
285 | 256 | public Boolean setIfAbsent(K key, V value) { |
286 | 257 |
|
287 | 258 | byte[] rawKey = rawKey(key); |
288 | 259 | byte[] rawValue = rawValue(value); |
289 | | - return execute(connection -> connection.setNX(rawKey, rawValue)); |
| 260 | + return execute(connection -> connection.set(rawKey, rawValue, Expiration.persistent(), SetOption.ifAbsent())); |
290 | 261 | } |
291 | 262 |
|
292 | 263 | @Override |
|
0 commit comments