Skip to content

Commit

Permalink
fix: prevent negative TTLs from insta-killing @document annotated ent…
Browse files Browse the repository at this point in the history
…ities with save/saveAll (resolves gh-429)
  • Loading branch information
bsbodden committed May 14, 2024
1 parent 49d999e commit 221ffdf
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ public Object put(Object id, Object item, String keyspace) {
processReferences(key, item);

redisOperations.execute((RedisCallback<Object>) connection -> {

maybeTtl.ifPresent(aLong -> connection.keyCommands().expire(toBytes(key), aLong));

maybeTtl.ifPresent(ttl -> { if (ttl > 0) connection.keyCommands().expire(toBytes(key), ttl); });
return null;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {

processReferenceAnnotations(objectKey, entity, pipeline);

maybeTtl.ifPresent(aLong -> pipeline.expire(objectKey, aLong));
maybeTtl.ifPresent(ttl -> { if (ttl > 0) pipeline.expire(objectKey, ttl); });

saved.add(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -119,4 +120,28 @@ void testExpiredEntitiesAreNotFound() throws InterruptedException {
assertThat(withTTLAnnotationRepository.findOneByName("Konrad Zuse")).isNotPresent();
assertThat(withTTLAnnotationRepository.findOneByName("Steve Wozniak")).isNotPresent();
}

@Test
void testNegativeTimeToLiveAnnotationWithSave() {
ExpiringPerson mWoodger = ExpiringPerson.of("Mike Woodger", -1L);
withTTLAnnotationRepository.save(mWoodger);

Long expire = withTTLAnnotationRepository.getExpiration(mWoodger.getId());
boolean exists = withTTLAnnotationRepository.existsById(mWoodger.getId());

assertThat(expire).isEqualTo(-1L);
assertThat(exists).isTrue();
}

@Test
void testNegativeTimeToLiveAnnotationWithSaveAll() {
ExpiringPerson mWoodger = ExpiringPerson.of("Mike Woodger", -1L);
withTTLAnnotationRepository.saveAll(Set.of(mWoodger));

Long expire = withTTLAnnotationRepository.getExpiration(mWoodger.getId());
boolean exists = withTTLAnnotationRepository.existsById(mWoodger.getId());

assertThat(expire).isEqualTo(-1L);
assertThat(exists).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -103,4 +104,28 @@ void testTimeToLiveAnnotationWithDifferentTimeUnitOnSaveAll() {
assertThat(jWilkinsonExpiration).isEqualTo(7L*24*60*60);
assertThat(sDummontExpiration).isEqualTo(7L*24*60*60);
}

@Test
void testNegativeTimeToLiveAnnotationUsingSave() {
ExpiringPerson mWoodger = ExpiringPerson.of("Mike Woodger", -1L);
withTTLAnnotationRepository.save(mWoodger);

Long expire = withTTLAnnotationRepository.getExpiration(mWoodger.getId());
boolean exists = withTTLAnnotationRepository.existsById(mWoodger.getId());

assertThat(expire).isEqualTo(-1L);
assertThat(exists).isTrue();
}

@Test
void testNegativeTimeToLiveAnnotationUsingSaveAll() {
ExpiringPerson mWoodger = ExpiringPerson.of("Mike Woodger", -1L);
withTTLAnnotationRepository.saveAll(Set.of(mWoodger));

Long expire = withTTLAnnotationRepository.getExpiration(mWoodger.getId());
boolean exists = withTTLAnnotationRepository.existsById(mWoodger.getId());

assertThat(expire).isEqualTo(-1L);
assertThat(exists).isTrue();
}
}

0 comments on commit 221ffdf

Please sign in to comment.