Makes caching async

This commit is contained in:
aarbit 2024-06-06 00:32:09 -05:00
parent bb0cac8553
commit 9edcb5566b
5 changed files with 39 additions and 11 deletions

View File

@ -8,7 +8,7 @@ plugins {
}
group = "net.halfbinary"
version = "0.0.8-SNAPSHOT"
version = "0.0.11-SNAPSHOT"
java {
sourceCompatibility = JavaVersion.VERSION_21
@ -32,6 +32,11 @@ val coroutinesVersion = "1.8.1"
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("com.mpatric:mp3agic:$mp3agicVersion")
@ -43,6 +48,9 @@ dependencies {
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("ch.qos.logback:logback-classic:$logbackVersion")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

View File

@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RestController
@CrossOrigin
class CacheController(private val albumCacheService: AlbumCacheService) {
@GetMapping("cache")
fun refreshAlbumCache() {
suspend fun refreshAlbumCache() {
albumCacheService.refreshAlbumCache()
}
}

View File

@ -2,8 +2,8 @@ package net.halfbinary.prettyplayerapi.service
import com.mpatric.mp3agic.Mp3File
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.halfbinary.prettyplayerapi.exception.EnvironmentVariableNotFoundException
import net.halfbinary.prettyplayerapi.model.AlbumInfo
import net.halfbinary.prettyplayerapi.repository.AlbumRepository
@ -20,8 +20,14 @@ private val logger = KotlinLogging.logger {}
@Service
class AlbumCacheService(private val albumRepository: AlbumRepository) {
fun refreshAlbumCache() {
runBlocking{
suspend fun doAlbumCache() {
coroutineScope {
launch { cacheAlbums(false) }
}
}
suspend fun refreshAlbumCache() {
coroutineScope{
launch { cacheAlbums(true)}
}
}
@ -43,7 +49,9 @@ class AlbumCacheService(private val albumRepository: AlbumRepository) {
.map {
val folder = it.first.parentFile.toPath()
val folderHash = folder.name.hashCode().toString()
runBlocking { launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)} }
coroutineScope {
launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)}
}
Pair(folderHash, AlbumInfo(it.second.id3v2Tag.album, it.first.parent, folderHash))
}

View File

@ -10,6 +10,7 @@ class MusicService(private val albumCacheService: AlbumCacheService) {
val albumInfo = albumCacheService.getAlbumCache()[albumHash]
return if(albumInfo != null) {
File(albumInfo.albumFolder).listFiles()[trackNumber-1]
File(albumInfo.albumFolder).listFiles().sortedBy { it.name }[trackNumber-1]
} else {
throw AlbumHashNotFoundException("Album hash $albumHash not found")
}

View File

@ -1,15 +1,26 @@
package net.halfbinary.prettyplayerapi.service
import jakarta.annotation.PostConstruct
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import org.springframework.boot.context.event.ApplicationStartedEvent
import org.springframework.context.event.EventListener
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Component
private val logger = KotlinLogging.logger {}
@Component
class StartupService(private val albumCacheService: AlbumCacheService) {
@PostConstruct
fun onStartup() {
albumCacheService.refreshAlbumCache()
@Async
@EventListener(ApplicationStartedEvent::class)
suspend fun onStartup() {
coroutineScope {
launch {
logger.info { "Starting up..." }
albumCacheService.doAlbumCache()
}
}
}
}