@@ -236,26 +236,28 @@ public Object put(Object id, Object item, String keyspace) {
236236
237237 connection .hMSet (objectKey , rdo .getBucket ().rawMap ());
238238
239- if (rdo .getTimeToLive () != null && rdo .getTimeToLive () > 0 ) {
239+ if (isNew ) {
240+ connection .sAdd (toBytes (rdo .getKeyspace ()), key );
241+ }
240242
243+ if (expires (rdo )) {
241244 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 );
242250
243- if (keepShadowCopy ( )) { // add phantom key so values can be restored
251+ if (expires ( rdo )) {
244252
245- byte [] phantomKey = ByteUtils .concat (objectKey , BinaryKeyspaceIdentifier .PHANTOM_SUFFIX );
246253 connection .del (phantomKey );
247254 connection .hMSet (phantomKey , rdo .getBucket ().rawMap ());
248255 connection .expire (phantomKey , rdo .getTimeToLive () + PHANTOM_KEY_TTL );
256+ } else if (!isNew ) {
257+ connection .del (phantomKey );
249258 }
250259 }
251260
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-
259261 IndexWriter indexWriter = new IndexWriter (connection , converter );
260262 if (isNew ) {
261263 indexWriter .createIndexes (key , rdo .getIndexedData ());
@@ -349,11 +351,14 @@ public <T> T delete(Object id, String keyspace, Class<T> type) {
349351 connection .sRem (binKeyspace , binId );
350352 new IndexWriter (connection , converter ).removeKeyFromIndexes (asString (keyspace ), binId );
351353
352- RedisPersistentEntity <?> persistentEntity = converter .getMappingContext ().getPersistentEntity (type );
353- if (persistentEntity != null && persistentEntity .isExpiring ()) {
354+ if (RedisKeyValueAdapter .this .keepShadowCopy ()) {
354355
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+ }
357362 }
358363 return null ;
359364 });
@@ -484,7 +489,7 @@ public void update(PartialUpdate<?> update) {
484489
485490 if (update .isRefreshTtl ()) {
486491
487- if (rdo . getTimeToLive () != null && rdo . getTimeToLive () > 0 ) {
492+ if (expires ( rdo ) ) {
488493
489494 connection .expire (redisKey , rdo .getTimeToLive ());
490495
@@ -498,7 +503,10 @@ public void update(PartialUpdate<?> update) {
498503 } else {
499504
500505 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+ }
502510 }
503511 }
504512
@@ -655,6 +663,16 @@ private <T> T readBackTimeToLiveIfSet(@Nullable byte[] key, @Nullable T target)
655663 return target ;
656664 }
657665
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+
658676 /**
659677 * Configure usage of {@link KeyExpirationEventMessageListener}.
660678 *
0 commit comments