From 61197504c3f159aa28fc007cc45e8aaee13b5e13 Mon Sep 17 00:00:00 2001 From: Andrew Glaze Date: Sun, 1 Dec 2024 22:53:42 -0500 Subject: [PATCH] Implement caching --- .gitignore | 1 + app/src/test/kotlin/util/InputDownloader.kt | 34 +++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/.gitignore b/.gitignore index cc064d4..d28bee1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ build .env +.aoc_cache diff --git a/app/src/test/kotlin/util/InputDownloader.kt b/app/src/test/kotlin/util/InputDownloader.kt index 09fd72d..49984fd 100644 --- a/app/src/test/kotlin/util/InputDownloader.kt +++ b/app/src/test/kotlin/util/InputDownloader.kt @@ -9,10 +9,17 @@ import java.net.CookieHandler import java.net.CookieManager import java.net.ConnectException import java.time.Duration +import java.io.File import io.github.cdimascio.dotenv.dotenv +import kotlin.text.trim class InputDownloader { fun getInput(day: Int): String { + val cache = readCache(day, false) + if (cache != null) { + return cache.trim() + } + val dotenv = dotenv() val sessionCookie = HttpCookie("session", dotenv["AOC_TOKEN"]); sessionCookie.path = "/" @@ -39,10 +46,16 @@ class InputDownloader { throw ConnectException("Failed to download input for day $day. Is the day open yet?") } + writeCache(day, false, res.body()) return res.body().trim() } fun getExample(day: Int): String { + val cache = readCache(day, true) + if (cache != null) { + return cache.trim() + } + val client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build() val req = HttpRequest.newBuilder() @@ -59,6 +72,27 @@ class InputDownloader { val start = bod.indexOf("
")
         val stop = bod.indexOf("
") val sliced = bod.substring(start + 11..