@@ -236,26 +236,28 @@ public Object put(Object id, Object item, String keyspace) {
236
236
237
237
connection .hMSet (objectKey , rdo .getBucket ().rawMap ());
238
238
239
- if (rdo .getTimeToLive () != null && rdo .getTimeToLive () > 0 ) {
239
+ if (isNew ) {
240
+ connection .sAdd (toBytes (rdo .getKeyspace ()), key );
241
+ }
240
242
243
+ if (expires (rdo )) {
241
244
connection .expire (objectKey , rdo .getTimeToLive ());
245
+ }
246
+
247
+ if (keepShadowCopy ()) { // add phantom key so values can be restored
248
+
249
+ byte [] phantomKey = ByteUtils .concat (objectKey , BinaryKeyspaceIdentifier .PHANTOM_SUFFIX );
242
250
243
- if (keepShadowCopy ( )) { // add phantom key so values can be restored
251
+ if (expires ( rdo )) {
244
252
245
- byte [] phantomKey = ByteUtils .concat (objectKey , BinaryKeyspaceIdentifier .PHANTOM_SUFFIX );
246
253
connection .del (phantomKey );
247
254
connection .hMSet (phantomKey , rdo .getBucket ().rawMap ());
248
255
connection .expire (phantomKey , rdo .getTimeToLive () + PHANTOM_KEY_TTL );
256
+ } else if (!isNew ) {
257
+ connection .del (phantomKey );
249
258
}
250
259
}
251
260
252
- boolean isNoExpire = rdo .getTimeToLive () == null || rdo .getTimeToLive () != null && rdo .getTimeToLive () < 0 ;
253
- if (isNoExpire && !isNew && keepShadowCopy ()){
254
- connection .del (ByteUtils .concat (objectKey , BinaryKeyspaceIdentifier .PHANTOM_SUFFIX ));
255
- }
256
-
257
- connection .sAdd (toBytes (rdo .getKeyspace ()), key );
258
-
259
261
IndexWriter indexWriter = new IndexWriter (connection , converter );
260
262
if (isNew ) {
261
263
indexWriter .createIndexes (key , rdo .getIndexedData ());
@@ -349,11 +351,14 @@ public <T> T delete(Object id, String keyspace, Class<T> type) {
349
351
connection .sRem (binKeyspace , binId );
350
352
new IndexWriter (connection , converter ).removeKeyFromIndexes (asString (keyspace ), binId );
351
353
352
- RedisPersistentEntity <?> persistentEntity = converter .getMappingContext ().getPersistentEntity (type );
353
- if (persistentEntity != null && persistentEntity .isExpiring ()) {
354
+ if (RedisKeyValueAdapter .this .keepShadowCopy ()) {
354
355
355
- byte [] phantomKey = ByteUtils .concat (keyToDelete , BinaryKeyspaceIdentifier .PHANTOM_SUFFIX );
356
- connection .del (phantomKey );
356
+ RedisPersistentEntity <?> persistentEntity = converter .getMappingContext ().getPersistentEntity (type );
357
+ if (persistentEntity != null && persistentEntity .isExpiring ()) {
358
+
359
+ byte [] phantomKey = ByteUtils .concat (keyToDelete , BinaryKeyspaceIdentifier .PHANTOM_SUFFIX );
360
+ connection .del (phantomKey );
361
+ }
357
362
}
358
363
return null ;
359
364
});
@@ -484,7 +489,7 @@ public void update(PartialUpdate<?> update) {
484
489
485
490
if (update .isRefreshTtl ()) {
486
491
487
- if (rdo . getTimeToLive () != null && rdo . getTimeToLive () > 0 ) {
492
+ if (expires ( rdo ) ) {
488
493
489
494
connection .expire (redisKey , rdo .getTimeToLive ());
490
495
@@ -498,7 +503,10 @@ public void update(PartialUpdate<?> update) {
498
503
} else {
499
504
500
505
connection .persist (redisKey );
501
- connection .del (ByteUtils .concat (redisKey , BinaryKeyspaceIdentifier .PHANTOM_SUFFIX ));
506
+
507
+ if (keepShadowCopy ()) {
508
+ connection .del (ByteUtils .concat (redisKey , BinaryKeyspaceIdentifier .PHANTOM_SUFFIX ));
509
+ }
502
510
}
503
511
}
504
512
@@ -655,6 +663,16 @@ private <T> T readBackTimeToLiveIfSet(@Nullable byte[] key, @Nullable T target)
655
663
return target ;
656
664
}
657
665
666
+ /**
667
+ * @return {@literal true} if {@link RedisData#getTimeToLive()} has a positive value.
668
+ *
669
+ * @param data must not be {@literal null}.
670
+ * @since 2.3.7
671
+ */
672
+ private boolean expires (RedisData data ) {
673
+ return data .getTimeToLive () != null && data .getTimeToLive () > 0 ;
674
+ }
675
+
658
676
/**
659
677
* Configure usage of {@link KeyExpirationEventMessageListener}.
660
678
*
0 commit comments