Skip to content

Commit

Permalink
chore: Potential load time improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
LichtHund committed Jun 26, 2024
1 parent 4cf325f commit a501dab
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,5 @@ tailwind/
backend/src/main/resources/static/css/tailwind.css

output/

projects.zip
4 changes: 3 additions & 1 deletion backend/src/main/kotlin/api/ProjectSetup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.triumphteam.backend.api
import dev.triumphteam.backend.DATA_FOLDER
import dev.triumphteam.backend.api.database.DocVersionEntity
import dev.triumphteam.backend.api.database.PageEntity
import dev.triumphteam.backend.api.database.Pages.subTitle
import dev.triumphteam.backend.api.database.ProjectEntity
import dev.triumphteam.backend.banner.BannerMaker
import dev.triumphteam.website.JsonSerializer
Expand Down Expand Up @@ -35,6 +34,9 @@ public fun setupRepository(projects: File) {
// Parse repos
val repo = JsonSerializer.from<Repository>(json)

// Delete downloaded files
projects.delete()

transaction {
repo.projects.forEach { project ->

Expand Down
33 changes: 30 additions & 3 deletions backend/src/main/kotlin/website/pages/docs/DocsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import dev.triumphteam.backend.website.pages.setupHead
import dev.triumphteam.website.project.Navigation
import dev.triumphteam.website.project.PageSummary
import dev.triumphteam.website.project.SummaryEntry
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.TextContent
import io.ktor.http.withCharset
import io.ktor.server.application.call
import io.ktor.server.html.respondHtml
import io.ktor.server.request.uri
import io.ktor.server.response.respond
import io.ktor.server.response.respondRedirect
Expand All @@ -38,11 +40,13 @@ import kotlinx.html.classes
import kotlinx.html.div
import kotlinx.html.footer
import kotlinx.html.h1
import kotlinx.html.html
import kotlinx.html.id
import kotlinx.html.img
import kotlinx.html.li
import kotlinx.html.meta
import kotlinx.html.script
import kotlinx.html.stream.appendHTML
import kotlinx.html.style
import kotlinx.html.styleLink
import kotlinx.html.title
Expand All @@ -54,6 +58,10 @@ import java.time.LocalDate
import kotlin.time.Duration.Companion.minutes
import kotlin.time.toJavaDuration

private val pageCache: Cache<String, TextContent> = Caffeine.newBuilder()
.expireAfterWrite(10.minutes.toJavaDuration())
.build()

private val projectCache: Cache<String, ProjectData> = Caffeine.newBuilder()
.expireAfterWrite(5.minutes.toJavaDuration())
.build()
Expand Down Expand Up @@ -84,9 +92,24 @@ public fun Routing.docsRoutes(developmentMode: Boolean) {
}
}

call.respondHtml {
renderFullPage(developmentMode, project, currentVersion, page)
val cacheId = cacheId(project, currentVersion, page)
val cached = pageCache.getIfPresent(cacheId)
if (cached != null) {
return@get call.respond(cached)
}

val text = buildString {
append("<!DOCTYPE html>\n")
appendHTML().html {
renderFullPage(developmentMode, project, currentVersion, page)
}
}

call.respond(
message = TextContent(text, ContentType.Text.Html.withCharset(Charsets.UTF_8), HttpStatusCode.OK).also {
pageCache.put(cacheId, it)
}
)
}
}

Expand Down Expand Up @@ -465,3 +488,7 @@ public data class Page(
public val title: String,
public val subTitle: String,
)

private fun cacheId(project: ProjectData, version: Version, page: Page): String {
return "${project.id}:${version.reference}:${page.id}"
}

0 comments on commit a501dab

Please sign in to comment.