diff --git a/src/main/kotlin/fr/shikkanime/controllers/api/AnimeController.kt b/src/main/kotlin/fr/shikkanime/controllers/api/AnimeController.kt index e33eeba2..eb55b21a 100644 --- a/src/main/kotlin/fr/shikkanime/controllers/api/AnimeController.kt +++ b/src/main/kotlin/fr/shikkanime/controllers/api/AnimeController.kt @@ -6,11 +6,16 @@ import fr.shikkanime.dtos.* import fr.shikkanime.dtos.animes.AnimeDto import fr.shikkanime.dtos.enums.Status import fr.shikkanime.dtos.weekly.v1.WeeklyAnimesDto +import fr.shikkanime.entities.Anime +import fr.shikkanime.entities.EpisodeMapping +import fr.shikkanime.entities.EpisodeVariant +import fr.shikkanime.entities.Simulcast import fr.shikkanime.entities.enums.CountryCode import fr.shikkanime.entities.enums.LangType import fr.shikkanime.services.AnimeService import fr.shikkanime.services.caches.AnimeCacheService import fr.shikkanime.services.caches.MemberFollowAnimeCacheService +import fr.shikkanime.utils.MapCache import fr.shikkanime.utils.atStartOfWeek import fr.shikkanime.utils.routes.* import fr.shikkanime.utils.routes.method.Delete @@ -153,6 +158,7 @@ class AnimeController : HasPageableRoute() { @OpenAPI(hidden = true) private fun updateAnime(@PathParam("uuid") uuid: UUID, @BodyParam animeDto: AnimeDto): Response { val updated = animeService.update(uuid, animeDto) + MapCache.invalidate(Anime::class.java) return Response.ok(AbstractConverter.convert(updated, AnimeDto::class.java)) } @@ -162,6 +168,7 @@ class AnimeController : HasPageableRoute() { @OpenAPI(hidden = true) private fun deleteAnime(@PathParam("uuid") uuid: UUID): Response { animeService.delete(animeService.find(uuid) ?: return Response.notFound()) + MapCache.invalidate(Anime::class.java, EpisodeMapping::class.java, EpisodeVariant::class.java, Simulcast::class.java) return Response.noContent() } diff --git a/src/main/kotlin/fr/shikkanime/controllers/api/ConfigController.kt b/src/main/kotlin/fr/shikkanime/controllers/api/ConfigController.kt index ff0b689f..bb1d113e 100644 --- a/src/main/kotlin/fr/shikkanime/controllers/api/ConfigController.kt +++ b/src/main/kotlin/fr/shikkanime/controllers/api/ConfigController.kt @@ -3,8 +3,10 @@ package fr.shikkanime.controllers.api import com.google.inject.Inject import fr.shikkanime.converters.AbstractConverter import fr.shikkanime.dtos.ConfigDto +import fr.shikkanime.entities.Config import fr.shikkanime.services.ConfigService import fr.shikkanime.utils.Constant +import fr.shikkanime.utils.MapCache import fr.shikkanime.utils.routes.AdminSessionAuthenticated import fr.shikkanime.utils.routes.Controller import fr.shikkanime.utils.routes.Path @@ -44,6 +46,7 @@ class ConfigController { @OpenAPI(hidden = true) private fun updateConfig(@PathParam("uuid") uuid: UUID, @BodyParam configDto: ConfigDto): Response { configService.update(uuid, configDto) + MapCache.invalidate(Config::class.java) Constant.abstractSocialNetworks.forEach { it.logout() } return Response.ok(AbstractConverter.convert(configService.find(uuid), ConfigDto::class.java)) } diff --git a/src/main/kotlin/fr/shikkanime/controllers/api/EpisodeMappingController.kt b/src/main/kotlin/fr/shikkanime/controllers/api/EpisodeMappingController.kt index 326fda56..6a8854dd 100644 --- a/src/main/kotlin/fr/shikkanime/controllers/api/EpisodeMappingController.kt +++ b/src/main/kotlin/fr/shikkanime/controllers/api/EpisodeMappingController.kt @@ -7,8 +7,10 @@ import fr.shikkanime.dtos.mappings.UpdateAllEpisodeMappingDto import fr.shikkanime.dtos.enums.Status import fr.shikkanime.dtos.mappings.EpisodeMappingDto import fr.shikkanime.dtos.variants.EpisodeVariantDto +import fr.shikkanime.entities.Anime import fr.shikkanime.entities.EpisodeMapping import fr.shikkanime.entities.EpisodeVariant +import fr.shikkanime.entities.Simulcast import fr.shikkanime.entities.enums.CountryCode import fr.shikkanime.services.EpisodeMappingService import fr.shikkanime.services.EpisodeVariantService @@ -149,6 +151,7 @@ class EpisodeMappingController : HasPageableRoute() { } episodeMappingService.updateAll(updateAllEpisodeMappingDto) + MapCache.invalidate(Anime::class.java, Simulcast::class.java, EpisodeMapping::class.java, EpisodeVariant::class.java) return Response.ok() } @@ -161,6 +164,7 @@ class EpisodeMappingController : HasPageableRoute() { @BodyParam episodeMappingDto: EpisodeMappingDto ): Response { val updated = episodeMappingService.update(uuid, episodeMappingDto) + MapCache.invalidate(Anime::class.java, EpisodeMapping::class.java, EpisodeVariant::class.java, Simulcast::class.java) return Response.ok(AbstractConverter.convert(updated, EpisodeMappingDto::class.java)) } diff --git a/src/main/kotlin/fr/shikkanime/controllers/api/MemberActionController.kt b/src/main/kotlin/fr/shikkanime/controllers/api/MemberActionController.kt index d40d0169..de3262e3 100644 --- a/src/main/kotlin/fr/shikkanime/controllers/api/MemberActionController.kt +++ b/src/main/kotlin/fr/shikkanime/controllers/api/MemberActionController.kt @@ -2,7 +2,9 @@ package fr.shikkanime.controllers.api import com.google.inject.Inject import fr.shikkanime.dtos.MessageDto +import fr.shikkanime.entities.Member import fr.shikkanime.services.MemberActionService +import fr.shikkanime.utils.MapCache import fr.shikkanime.utils.routes.Controller import fr.shikkanime.utils.routes.JWTAuthenticated import fr.shikkanime.utils.routes.Path @@ -62,6 +64,7 @@ class MemberActionController { try { memberActionService.validateAction(uuid, action) + MapCache.invalidate(Member::class.java) return Response.ok() } catch (e: Exception) { return Response.badRequest( diff --git a/src/main/kotlin/fr/shikkanime/controllers/site/ShortLinkController.kt b/src/main/kotlin/fr/shikkanime/controllers/site/ShortLinkController.kt index c8b35009..d862a34a 100644 --- a/src/main/kotlin/fr/shikkanime/controllers/site/ShortLinkController.kt +++ b/src/main/kotlin/fr/shikkanime/controllers/site/ShortLinkController.kt @@ -2,9 +2,11 @@ package fr.shikkanime.controllers.site import com.google.inject.Inject import fr.shikkanime.dtos.variants.EpisodeVariantDto +import fr.shikkanime.entities.Member import fr.shikkanime.services.MemberActionService import fr.shikkanime.services.caches.EpisodeVariantCacheService import fr.shikkanime.utils.Constant +import fr.shikkanime.utils.MapCache import fr.shikkanime.utils.routes.Controller import fr.shikkanime.utils.routes.Path import fr.shikkanime.utils.routes.Response @@ -34,6 +36,7 @@ class ShortLinkController { private fun validateWebToken(@PathParam("webTokenAction") webToken: String): Response { try { memberActionService.validateWebAction(webToken) + MapCache.invalidate(Member::class.java) return Response.template("/site/validateAction.ftl") } catch (_: Exception) { return Response.redirect("/") diff --git a/src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt b/src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt index 3842488c..f8d1b101 100644 --- a/src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt +++ b/src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt @@ -147,6 +147,7 @@ class FetchOldEpisodesJob : AbstractJob { config.propertyValue = from.toString() configService.update(config) + MapCache.invalidate(Config::class.java) traceActionService.createTraceAction(config, TraceAction.Action.UPDATE) log(emailLogs, Level.INFO, "Take ${(System.currentTimeMillis() - start) / 1000}s to check ${dates.size} dates") @@ -200,12 +201,10 @@ class FetchOldEpisodesJob : AbstractJob { countryCode: CountryCode, dates: Set ): List { - return runBlocking { - CrunchyrollCachedWrapper.getSimulcastCalendarWithDates( - countryCode, - dates - ) - }.mapNotNull { browseObject -> + return CrunchyrollCachedWrapper.getSimulcastCalendarWithDates( + countryCode, + dates + ).mapNotNull { browseObject -> try { crunchyrollPlatform.convertEpisode( countryCode, diff --git a/src/main/kotlin/fr/shikkanime/services/AnimeService.kt b/src/main/kotlin/fr/shikkanime/services/AnimeService.kt index e75074c3..9ea51e6e 100644 --- a/src/main/kotlin/fr/shikkanime/services/AnimeService.kt +++ b/src/main/kotlin/fr/shikkanime/services/AnimeService.kt @@ -422,7 +422,6 @@ class AnimeService : AbstractService() { anime.status = StringUtils.getStatus(anime) val update = super.update(anime) - MapCache.invalidate(Anime::class.java) traceActionService.createTraceAction(anime, TraceAction.Action.UPDATE) return update } @@ -444,7 +443,6 @@ class AnimeService : AbstractService() { memberFollowAnimeService.findAllByAnime(entity).forEach { memberFollowAnimeService.delete(it) } animePlatformService.findAllByAnime(entity).forEach { animePlatformService.delete(it) } super.delete(entity) - MapCache.invalidate(Anime::class.java, EpisodeMapping::class.java, EpisodeVariant::class.java, Simulcast::class.java) traceActionService.createTraceAction(entity, TraceAction.Action.DELETE) } diff --git a/src/main/kotlin/fr/shikkanime/services/ConfigService.kt b/src/main/kotlin/fr/shikkanime/services/ConfigService.kt index e507813c..c00ce405 100644 --- a/src/main/kotlin/fr/shikkanime/services/ConfigService.kt +++ b/src/main/kotlin/fr/shikkanime/services/ConfigService.kt @@ -5,7 +5,6 @@ import fr.shikkanime.dtos.ConfigDto import fr.shikkanime.entities.Config import fr.shikkanime.entities.TraceAction import fr.shikkanime.repositories.ConfigRepository -import fr.shikkanime.utils.MapCache import java.util.* class ConfigService : AbstractService() { @@ -28,7 +27,6 @@ class ConfigService : AbstractService() { config.propertyValue = configDto.propertyValue val updated = super.update(config) - MapCache.invalidate(Config::class.java) traceActionService.createTraceAction(config, TraceAction.Action.UPDATE) return updated } diff --git a/src/main/kotlin/fr/shikkanime/services/EpisodeMappingService.kt b/src/main/kotlin/fr/shikkanime/services/EpisodeMappingService.kt index 92d1542d..e74f93b6 100644 --- a/src/main/kotlin/fr/shikkanime/services/EpisodeMappingService.kt +++ b/src/main/kotlin/fr/shikkanime/services/EpisodeMappingService.kt @@ -11,7 +11,6 @@ import fr.shikkanime.entities.enums.LangType import fr.shikkanime.entities.enums.Platform import fr.shikkanime.repositories.EpisodeMappingRepository import fr.shikkanime.utils.Constant -import fr.shikkanime.utils.MapCache import fr.shikkanime.utils.StringUtils import java.time.LocalDate import java.time.ZonedDateTime @@ -82,7 +81,7 @@ class EpisodeMappingService : AbstractService - var forcedUpdate = updateAllEpisodeMappingDto.forceUpdate == true + val forcedUpdate = updateAllEpisodeMappingDto.forceUpdate == true updateAllEpisodeMappingDto.episodeType?.let { episode.episodeType = it } updateAllEpisodeMappingDto.season?.let { episode.season = it } @@ -101,7 +100,7 @@ class EpisodeMappingService : AbstractService - mergeEpisodeMapping(episode, existing, false)?.apply { + mergeEpisodeMapping(episode, existing)?.apply { if (forcedUpdate) lastUpdateDateTime = ZonedDateTime.parse("2000-01-01T00:00:00Z") else ZonedDateTime.now() super.update(this) } @@ -115,11 +114,8 @@ class EpisodeMappingService : AbstractService @@ -194,9 +188,6 @@ class EpisodeMappingService : AbstractService() { @Inject @@ -29,7 +28,6 @@ class SimulcastService : AbstractService() { override fun delete(entity: Simulcast) { super.delete(entity) - MapCache.invalidate(Simulcast::class.java) traceActionService.createTraceAction(entity, TraceAction.Action.DELETE) } diff --git a/src/main/kotlin/fr/shikkanime/utils/MapCache.kt b/src/main/kotlin/fr/shikkanime/utils/MapCache.kt index 5ec32805..2197ef1e 100644 --- a/src/main/kotlin/fr/shikkanime/utils/MapCache.kt +++ b/src/main/kotlin/fr/shikkanime/utils/MapCache.kt @@ -60,7 +60,7 @@ class MapCache( } companion object { - private val logger = LoggerFactory.getLogger(javaClass) + private val logger = LoggerFactory.getLogger(this::class.java) private val globalCaches: MutableList> = mutableListOf() fun loadAll() { diff --git a/src/main/kotlin/fr/shikkanime/wrappers/impl/caches/CrunchyrollCachedWrapper.kt b/src/main/kotlin/fr/shikkanime/wrappers/impl/caches/CrunchyrollCachedWrapper.kt index de72774c..69942a72 100644 --- a/src/main/kotlin/fr/shikkanime/wrappers/impl/caches/CrunchyrollCachedWrapper.kt +++ b/src/main/kotlin/fr/shikkanime/wrappers/impl/caches/CrunchyrollCachedWrapper.kt @@ -167,20 +167,20 @@ object CrunchyrollCachedWrapper : AbstractCrunchyrollWrapper() { private val seriesRegex = "/series/([A-Z0-9]{9})/".toRegex() private val episodeRegex = "/watch/([A-Z0-9]{9})".toRegex() - suspend fun getSimulcastCalendarWithDates(countryCode: CountryCode, dates: Set): List { + fun getSimulcastCalendarWithDates(countryCode: CountryCode, dates: Set): List { val startOfWeekDates = dates.map { it.atStartOfWeek() }.distinct() val releaseDateTimes = mutableSetOf() val seriesIds = mutableSetOf() val episodeIds = mutableSetOf() - startOfWeekDates.forEach { date -> - val response = HttpRequest.retry(3) { - httpRequest.get("$baseUrl${countryCode.name.lowercase()}/simulcastcalendar?filter=premium&date=$date").apply { - require(status == HttpStatusCode.OK) - } + startOfWeekDates.parallelStream().forEach { date -> + val document = HttpRequest.retry(3) { + Jsoup.parse( + httpRequest.get("$baseUrl${countryCode.name.lowercase()}/simulcastcalendar?filter=premium&date=$date").apply { + require(status == HttpStatusCode.OK) + }.bodyAsText()) } - val document = Jsoup.parse(response.bodyAsText()) document.select("article.release").forEach { element -> val releaseDateTime = ZonedDateTime.parse(element.select("time").attr("datetime")).withUTC() releaseDateTimes.add(releaseDateTime) diff --git a/src/test/kotlin/fr/shikkanime/wrappers/CrunchyrollWrapperTest.kt b/src/test/kotlin/fr/shikkanime/wrappers/CrunchyrollWrapperTest.kt index b59a73f0..b5ea322f 100644 --- a/src/test/kotlin/fr/shikkanime/wrappers/CrunchyrollWrapperTest.kt +++ b/src/test/kotlin/fr/shikkanime/wrappers/CrunchyrollWrapperTest.kt @@ -53,12 +53,11 @@ class CrunchyrollWrapperTest { @Test fun getSimulcastCalendarWithDates() { - val episodes = runBlocking { - CrunchyrollCachedWrapper.getSimulcastCalendarWithDates( - CountryCode.FR, - setOf(LocalDate.parse("2024-01-01")) - ) - } + val episodes = CrunchyrollCachedWrapper.getSimulcastCalendarWithDates( + CountryCode.FR, + setOf(LocalDate.parse("2024-01-01")) + ) + assertEquals(true, episodes.isNotEmpty()) }