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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jacocoTestCoverageVerification {
rule {
limit {
counter = "LINE"
minimum = 0.8
minimum = 0.7
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.birdia.cacher.file.bucket;

import org.springframework.stereotype.Component;
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;

@Component
public class ExtendedBucketComponent extends BucketComponent {
private final BucketConf bucketConf;

public ExtendedBucketComponent(BucketConf bucketConf) {
super(bucketConf);
this.bucketConf = bucketConf;
}

public boolean exists(String objectKey) {
var s3client = bucketConf.getS3Client();
try {
s3client.headObject(
HeadObjectRequest.builder().bucket(bucketConf.getBucketName()).key(objectKey).build());
return true;
} catch (NoSuchKeyException e) {
return false;
}
}
}
10 changes: 4 additions & 6 deletions src/main/java/fr/birdia/cacher/service/CacheService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.net.http.HttpClient.newHttpClient;

import fr.birdia.cacher.file.bucket.BucketComponent;
import fr.birdia.cacher.file.bucket.ExtendedBucketComponent;
import fr.birdia.cacher.hash.SHA256;
import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -20,18 +20,16 @@
@AllArgsConstructor
@Service
public class CacheService {
private final BucketComponent bucketComponent;
private final ExtendedBucketComponent bucketComponent;

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

private final HttpClient httpClient = newHttpClient();

public URL getWithCache(URL url) {
var bucketKey = SHA256.apply(url.toString());
try {
var cachedFile = bucketComponent.download(bucketKey);
log.info("isCachedFile null: " + (cachedFile == null)); // TODO: clean
} catch (Exception e) {
if (!bucketComponent.exists(bucketKey)) {
var downloadedFromSource = downloadWithGet(url);
bucketComponent.upload(downloadedFromSource, bucketKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static org.mockito.Mockito.when;

import fr.birdia.cacher.conf.FacadeIT;
import fr.birdia.cacher.file.bucket.BucketComponent;
import fr.birdia.cacher.file.bucket.ExtendedBucketComponent;
import fr.birdia.cacher.hash.SHA256;
import java.io.File;
import java.net.MalformedURLException;
Expand All @@ -26,7 +26,7 @@ class CacherControllerIT extends FacadeIT {

@Autowired SHA256 SHA256;

@MockBean BucketComponent bucketComponent;
@MockBean ExtendedBucketComponent bucketComponent;

@Test
void unauthorized() {
Expand All @@ -43,7 +43,7 @@ void miss() throws MalformedURLException {
var bucketKey = SHA256.apply(decodedUrl);
assertEquals("8547b3662d07fb7867a11c2c89672a664fd6e4ffb370dee9f60e0e65eeebde09", bucketKey);

when(bucketComponent.download(bucketKey)).thenThrow(new RuntimeException("cache miss"));
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());
Expand Down
Loading