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