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
import net.halfbinary.prettyplayerapi.model.CacheType
import net.halfbinary.prettyplayerapi.service.AlbumCacheService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("cache")
class CacheController(private val albumCacheService: AlbumCacheService) {
@GetMapping
suspend fun refreshAlbumCache() {
albumCacheService.refreshAlbumCache()
suspend fun refreshAlbumCache(@RequestParam type: CacheType) {
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 net.halfbinary.prettyplayerapi.exception.EnvironmentVariableNotFoundException
import net.halfbinary.prettyplayerapi.model.AlbumMetadata
import net.halfbinary.prettyplayerapi.model.CacheType
import net.halfbinary.prettyplayerapi.repository.AlbumRepository
import org.springframework.stereotype.Service
import java.awt.Image
@ -21,14 +22,20 @@ class AlbumCacheService(private val albumRepository: AlbumRepository,
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO) {
suspend fun doAlbumCache() {
suspend fun doAlbumCacheOnlyNewArt() {
coroutineScope {
launch { cacheAlbums(false) }
launch { cacheAlbums(CacheType.ART) }
}
}
suspend fun refreshAlbumCache() {
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
}
suspend fun cacheAlbums(refresh: Boolean = false) {
logger.debug { "Starting the cache process with refresh set to $refresh" }
suspend fun cacheAlbums(type: CacheType = CacheType.ART) {
logger.debug { "Starting the cache process with type ${type.name}" }
createAlbumArtRootDir()
val list = getAlbumRootDir()
.walkTopDown()
@ -50,6 +57,7 @@ class AlbumCacheService(private val albumRepository: AlbumRepository,
.map {
val folder = it.first.parentFile.toPath()
val folderHash = folder.name.hashCode().toString()
val refresh = type == CacheType.ALL
coroutineScope {
launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)}
}

View File

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