Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/main/java/fr/birdia/cacher/service/CacheService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
@AllArgsConstructor
@Service
public class CacheService {
private static final long MIN_CACHEABLE_SIZE_BYTES = 1_000_000L;
private final ExtendedBucketComponent bucketComponent;

private final SHA256 SHA256;
private final Duration DOWNLOAD_DURATION = Duration.ofMinutes(5);

Expand All @@ -30,8 +30,25 @@ public class CacheService {
public URL getWithCache(URL url) {
var bucketKey = SHA256.apply(url.toString());
if (!bucketComponent.exists(bucketKey)) {
var downloadedFromSource = downloadWithGet(url);
bucketComponent.upload(downloadedFromSource, bucketKey);
File downloadedFromSource = null;
try {
downloadedFromSource = downloadWithGet(url);
if (downloadedFromSource.length() < MIN_CACHEABLE_SIZE_BYTES) {
log.info(
"File downloaded from {} too small ({} bytes < {}), skipping cache",
url,
downloadedFromSource.length(),
MIN_CACHEABLE_SIZE_BYTES);
return url;
}
bucketComponent.upload(downloadedFromSource, bucketKey);
} finally {
if (downloadedFromSource != null && downloadedFromSource.exists()) {
if (!downloadedFromSource.delete()) {
log.info("Failed to delete temporary file {}", downloadedFromSource);
}
}
}
}
return bucketComponent.presign(bucketKey, DOWNLOAD_DURATION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -15,11 +16,13 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.time.Duration;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.web.server.ResponseStatusException;

@Slf4j
class CacherControllerIT extends FacadeIT {

@Autowired CacherController subject;
Expand All @@ -35,7 +38,7 @@ void unauthorized() {
}

@Test
void miss() throws MalformedURLException {
void miss_case_one() throws MalformedURLException {
var encodedUrl =
"https%3A%2F%2Fimages.unsplash.com%2Fphoto-1779896412092-4cb69cf4ccad%3Fq%3D80%26w%3D3000%26auto%3Dformat%26fit%3Dcrop%26ixlib%3Drb-4.1.0%26ixid%3DM3wxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%253D%253D";
var decodedUrl =
Expand All @@ -50,6 +53,28 @@ void miss() throws MalformedURLException {

var response = subject.getWithCache(encodedUrl, "test-api-key");

verify(bucketComponent, never()).upload(any(), any());
assertEquals(
"https://images.unsplash.com/photo-1779896412092-4cb69cf4ccad?q=80&w=3000&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
response);
}

@Test
void miss_case_two() throws MalformedURLException {
var encodedUrl =
"https%3A%2F%2Fdata.geopf.fr%2Ftelechargement%2Fdownload%2FLiDARHD-NUALID%2FNUALHD_1-0__LAZ_LAMB93_EM_2025-07-01%2FLHD_FXX_0404_6445_PTS_LAMB93_IGN69.copc.laz";
var decodedUrl =
"https://data.geopf.fr/telechargement/download/LiDARHD-NUALID/NUALHD_1-0__LAZ_LAMB93_EM_2025-07-01/LHD_FXX_0404_6445_PTS_LAMB93_IGN69.copc.laz";
var bucketKey = SHA256.apply(decodedUrl);
assertEquals("07826fff9818eb56aad68a40bcbd0348ecd211c386549ae2b5bdb5b7d7cb7db1", bucketKey);

when(bucketComponent.exists(bucketKey)).thenReturn(false);
when(bucketComponent.presign(eq(bucketKey), any(Duration.class)))
.thenReturn(
URI.create("https://example.com/presigned?param1=3&param2=4%20param3=5").toURL());

var response = subject.getWithCache(encodedUrl, "test-api-key");

verify(bucketComponent, times(1)).upload(any(File.class), eq(bucketKey));
assertEquals("https://example.com/presigned?param1=3&param2=4%20param3=5", response);
}
Expand Down
Loading