Makes caching async
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user