Sets up caching methods for eventual introduction of database-based caching

This commit is contained in:
aarbit 2024-06-25 16:11:07 -05:00
parent afd9f22861
commit db994d92cd
4 changed files with 30 additions and 8 deletions

View File

@ -1,15 +1,22 @@
package net.halfbinary.prettyplayerapi.controller package net.halfbinary.prettyplayerapi.controller
import net.halfbinary.prettyplayerapi.model.CacheType
import net.halfbinary.prettyplayerapi.service.AlbumCacheService import net.halfbinary.prettyplayerapi.service.AlbumCacheService
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
@RestController @RestController
@RequestMapping("cache") @RequestMapping("cache")
class CacheController(private val albumCacheService: AlbumCacheService) { class CacheController(private val albumCacheService: AlbumCacheService) {
@GetMapping @GetMapping
suspend fun refreshAlbumCache() { suspend fun refreshAlbumCache(@RequestParam type: CacheType) {
albumCacheService.refreshAlbumCache() when(type) {
CacheType.ART -> albumCacheService.doAlbumCacheOnlyNewArt()
CacheType.NEW -> albumCacheService.doAddNewAlbumCache() // TODO: This will make more sense with a DB backing
CacheType.ALL -> albumCacheService.refreshAlbumCache()
}
} }
} }

View File

@ -0,0 +1,7 @@
package net.halfbinary.prettyplayerapi.model
enum class CacheType {
ART,
NEW,
ALL
}

View File

@ -5,6 +5,7 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.* import kotlinx.coroutines.*
import net.halfbinary.prettyplayerapi.exception.EnvironmentVariableNotFoundException import net.halfbinary.prettyplayerapi.exception.EnvironmentVariableNotFoundException
import net.halfbinary.prettyplayerapi.model.AlbumMetadata import net.halfbinary.prettyplayerapi.model.AlbumMetadata
import net.halfbinary.prettyplayerapi.model.CacheType
import net.halfbinary.prettyplayerapi.repository.AlbumRepository import net.halfbinary.prettyplayerapi.repository.AlbumRepository
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.awt.Image import java.awt.Image
@ -21,14 +22,20 @@ class AlbumCacheService(private val albumRepository: AlbumRepository,
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO) { private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO) {
suspend fun doAlbumCache() { suspend fun doAlbumCacheOnlyNewArt() {
coroutineScope { coroutineScope {
launch { cacheAlbums(false) } launch { cacheAlbums(CacheType.ART) }
} }
} }
suspend fun refreshAlbumCache() { suspend fun refreshAlbumCache() {
coroutineScope{ coroutineScope{
launch { cacheAlbums(true)} launch { cacheAlbums(CacheType.ALL)}
}
}
suspend fun doAddNewAlbumCache() {
coroutineScope {
launch { cacheAlbums(CacheType.NEW) }
} }
} }
@ -36,8 +43,8 @@ class AlbumCacheService(private val albumRepository: AlbumRepository,
return albumRepository.albumCache return albumRepository.albumCache
} }
suspend fun cacheAlbums(refresh: Boolean = false) { suspend fun cacheAlbums(type: CacheType = CacheType.ART) {
logger.debug { "Starting the cache process with refresh set to $refresh" } logger.debug { "Starting the cache process with type ${type.name}" }
createAlbumArtRootDir() createAlbumArtRootDir()
val list = getAlbumRootDir() val list = getAlbumRootDir()
.walkTopDown() .walkTopDown()
@ -50,6 +57,7 @@ class AlbumCacheService(private val albumRepository: AlbumRepository,
.map { .map {
val folder = it.first.parentFile.toPath() val folder = it.first.parentFile.toPath()
val folderHash = folder.name.hashCode().toString() val folderHash = folder.name.hashCode().toString()
val refresh = type == CacheType.ALL
coroutineScope { coroutineScope {
launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)} launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)}
} }

View File

@ -17,7 +17,7 @@ class StartupService(private val albumCacheService: AlbumCacheService) {
suspend fun onStartup() { suspend fun onStartup() {
coroutineScope { coroutineScope {
launch { launch {
albumCacheService.doAlbumCache() albumCacheService.doAlbumCacheOnlyNewArt()
} }
} }
} }