Skip to content

Commit

Permalink
change cache invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Dec 20, 2024
1 parent bc90643 commit 2ac77f3
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand All @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand All @@ -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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("/")
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -200,12 +201,10 @@ class FetchOldEpisodesJob : AbstractJob {
countryCode: CountryCode,
dates: Set<LocalDate>
): List<Episode> {
return runBlocking {
CrunchyrollCachedWrapper.getSimulcastCalendarWithDates(
countryCode,
dates
)
}.mapNotNull { browseObject ->
return CrunchyrollCachedWrapper.getSimulcastCalendarWithDates(
countryCode,
dates
).mapNotNull { browseObject ->
try {
crunchyrollPlatform.convertEpisode(
countryCode,
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/fr/shikkanime/services/AnimeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ class AnimeService : AbstractService<Anime, AnimeRepository>() {

anime.status = StringUtils.getStatus(anime)
val update = super.update(anime)
MapCache.invalidate(Anime::class.java)
traceActionService.createTraceAction(anime, TraceAction.Action.UPDATE)
return update
}
Expand All @@ -444,7 +443,6 @@ class AnimeService : AbstractService<Anime, AnimeRepository>() {
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)
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/fr/shikkanime/services/ConfigService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config, ConfigRepository>() {
Expand All @@ -28,7 +27,6 @@ class ConfigService : AbstractService<Config, ConfigRepository>() {

config.propertyValue = configDto.propertyValue
val updated = super.update(config)
MapCache.invalidate(Config::class.java)
traceActionService.createTraceAction(config, TraceAction.Action.UPDATE)
return updated
}
Expand Down
19 changes: 4 additions & 15 deletions src/main/kotlin/fr/shikkanime/services/EpisodeMappingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,7 +81,7 @@ class EpisodeMappingService : AbstractService<EpisodeMapping, EpisodeMappingRepo
var startDate = updateAllEpisodeMappingDto.startDate?.let { LocalDate.parse(it) }

episodes.forEach { episode ->
var forcedUpdate = updateAllEpisodeMappingDto.forceUpdate == true
val forcedUpdate = updateAllEpisodeMappingDto.forceUpdate == true

updateAllEpisodeMappingDto.episodeType?.let { episode.episodeType = it }
updateAllEpisodeMappingDto.season?.let { episode.season = it }
Expand All @@ -101,7 +100,7 @@ class EpisodeMappingService : AbstractService<EpisodeMapping, EpisodeMappingRepo
findByAnimeSeasonEpisodeTypeNumber(episode.anime!!.uuid!!, episode.season!!, episode.episodeType!!, episode.number!!)
?.takeIf { it.uuid != episode.uuid }
?.let { existing ->
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)
}
Expand All @@ -115,11 +114,8 @@ class EpisodeMappingService : AbstractService<EpisodeMapping, EpisodeMappingRepo
traceActionService.createTraceAction(episode, TraceAction.Action.UPDATE)
}

if (startDate == null) {
MapCache.invalidate(EpisodeMapping::class.java, EpisodeVariant::class.java)
} else {
if (startDate != null) {
animeService.recalculateSimulcasts()
MapCache.invalidate(Anime::class.java, Simulcast::class.java, EpisodeMapping::class.java, EpisodeVariant::class.java)
}
}

Expand Down Expand Up @@ -165,15 +161,13 @@ class EpisodeMappingService : AbstractService<EpisodeMapping, EpisodeMappingRepo
episode.lastUpdateDateTime = ZonedDateTime.now()
val update = super.update(episode)
updateEpisodeMappingVariants(entity, episode, update)
MapCache.invalidate(EpisodeMapping::class.java)
traceActionService.createTraceAction(episode, TraceAction.Action.UPDATE)
return update
}

private fun mergeEpisodeMapping(
episode: EpisodeMapping,
existing: EpisodeMapping,
updateCache: Boolean = true
existing: EpisodeMapping
): EpisodeMapping? {
// Set the variants of the current episode to the existing episode
episodeVariantService.findAllByMapping(episode).forEach { variant ->
Expand All @@ -194,9 +188,6 @@ class EpisodeMappingService : AbstractService<EpisodeMapping, EpisodeMappingRepo
update(existing)
traceActionService.createTraceAction(existing, TraceAction.Action.UPDATE)

if (updateCache)
MapCache.invalidate(EpisodeMapping::class.java, EpisodeVariant::class.java)

return find(existing.uuid!!)
}

Expand All @@ -220,7 +211,6 @@ class EpisodeMappingService : AbstractService<EpisodeMapping, EpisodeMappingRepo

if (findAllByAnime(oldAnime).isEmpty()) {
animeService.delete(oldAnime)
MapCache.invalidate(Anime::class.java)
}
}
}
Expand Down Expand Up @@ -271,7 +261,6 @@ class EpisodeMappingService : AbstractService<EpisodeMapping, EpisodeMappingRepo
}

oldList.forEach { episodeVariantService.delete(it) }
MapCache.invalidate(EpisodeVariant::class.java)
}

override fun delete(entity: EpisodeMapping) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/fr/shikkanime/services/MemberActionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class MemberActionService : AbstractService<MemberAction, MemberActionRepository
memberAction.member!!.email = memberAction.email
memberService.update(memberAction.member!!)
traceActionService.createTraceAction(memberAction.member!!, TraceAction.Action.UPDATE)
MapCache.invalidate(Member::class.java)

try {
emailService.sendEmail(
Expand All @@ -139,7 +138,6 @@ class MemberActionService : AbstractService<MemberAction, MemberActionRepository
memberAction.member!!.username = EncryptionManager.toSHA512(identifier)
memberService.update(memberAction.member!!)
traceActionService.createTraceAction(memberAction.member!!, TraceAction.Action.UPDATE)
MapCache.invalidate(Member::class.java)

try {
emailService.sendEmail(
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/fr/shikkanime/services/SimulcastService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import fr.shikkanime.entities.Simulcast
import fr.shikkanime.entities.TraceAction
import fr.shikkanime.repositories.SimulcastRepository
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.MapCache

class SimulcastService : AbstractService<Simulcast, SimulcastRepository>() {
@Inject
Expand All @@ -29,7 +28,6 @@ class SimulcastService : AbstractService<Simulcast, SimulcastRepository>() {

override fun delete(entity: Simulcast) {
super.delete(entity)
MapCache.invalidate(Simulcast::class.java)
traceActionService.createTraceAction(entity, TraceAction.Action.DELETE)
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/shikkanime/utils/MapCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MapCache<K : Any, V>(
}

companion object {
private val logger = LoggerFactory.getLogger(javaClass)
private val logger = LoggerFactory.getLogger(this::class.java)
private val globalCaches: MutableList<MapCache<*, *>> = mutableListOf()

fun loadAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalDate>): List<BrowseObject> {
fun getSimulcastCalendarWithDates(countryCode: CountryCode, dates: Set<LocalDate>): List<BrowseObject> {
val startOfWeekDates = dates.map { it.atStartOfWeek() }.distinct()
val releaseDateTimes = mutableSetOf<ZonedDateTime>()
val seriesIds = mutableSetOf<String>()
val episodeIds = mutableSetOf<String>()

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)
Expand Down
11 changes: 5 additions & 6 deletions src/test/kotlin/fr/shikkanime/wrappers/CrunchyrollWrapperTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down

0 comments on commit 2ac77f3

Please sign in to comment.