Remove phantom copy of expiring data when source gets persisted.

This commit makes sure to clean up resources when a previously expiring entity is persisted by setting the time to live to zero or negative.
In case a phantom copy exists for the changed entity, it is removed to free space on the server and prevent expiration events from being sent.

Closes #1955
Original Pull Request: #1961
This commit is contained in:
Muchnik Andrey
2021-02-08 21:18:54 +03:00
committed by Christoph Strobl
parent febdb82e58
commit e46246721f
2 changed files with 43 additions and 1 deletions

View File

@@ -54,6 +54,7 @@ import org.springframework.data.redis.test.condition.EnabledIfLongRunningTest;
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Andrey Muchnik
*/
@ExtendWith(LettuceConnectionFactoryExtension.class)
public class RedisKeyValueAdapterTests {
@@ -705,6 +706,41 @@ public class RedisKeyValueAdapterTests {
assertThat(template.hasKey("persons:1:phantom")).isTrue();
}
@Test // DATAREDIS-1955
void phantomKeyIsDeletedWhenPutWithNegativeTimeToLiveAndOldEntryTimeToLiveWasPositiveAndWhenShadowCopyIsTurnedOn() {
ExpiringPerson rand = new ExpiringPerson();
rand.id = "1";
rand.ttl = 3000L;
adapter.put("1", rand, "persons");
assertThat(template.getExpire("persons:1:phantom")).isPositive();
rand.ttl = -1L;
adapter.put("1", rand, "persons");
assertThat(template.hasKey("persons:1:phantom")).isFalse();
}
@Test // DATAREDIS-1955
void updateWithRefreshTtlAndWithoutPositiveTtlShouldDeletePhantomKey() {
ExpiringPerson person = new ExpiringPerson();
person.id = "1";
person.ttl = 100L;
adapter.put("1", person, "persons");
assertThat(template.getExpire("persons:1:phantom")).isPositive();
PartialUpdate<ExpiringPerson> update = new PartialUpdate<>("1", ExpiringPerson.class) //
.refreshTtl(true);
adapter.update(update);
assertThat(template.hasKey("persons:1:phantom")).isFalse();
}
/**
* Wait up to 5 seconds until {@code key} is no longer available in Redis.
*