This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ProfileService and caching for improved performance
- Loading branch information
Showing
7 changed files
with
129 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/fr/ziedelth/caches/ProfileEpisodeTypesLangTypesPaginationCacheKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package fr.ziedelth.caches | ||
|
||
import java.util.* | ||
|
||
data class ProfileEpisodeTypesLangTypesPaginationCacheKey( | ||
override val profile: UUID, | ||
val episodeTypes: List<UUID>, | ||
val langTypes: List<UUID>, | ||
override val page: Int, | ||
override val limit: Int, | ||
) : ProfilePaginationCacheKey(profile, page, limit) |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/fr/ziedelth/caches/ProfilePaginationCacheKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package fr.ziedelth.caches | ||
|
||
import java.util.* | ||
|
||
open class ProfilePaginationCacheKey( | ||
open val profile: UUID, | ||
open val page: Int, | ||
open val limit: Int, | ||
) { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is ProfilePaginationCacheKey) return false | ||
|
||
if (profile != other.profile) return false | ||
if (page != other.page) return false | ||
if (limit != other.limit) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = profile.hashCode() | ||
result = 31 * result + page | ||
result = 31 * result + limit | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package fr.ziedelth.services | ||
|
||
import com.google.common.cache.CacheBuilder | ||
import com.google.common.cache.CacheLoader | ||
import com.google.inject.Inject | ||
import fr.ziedelth.caches.ProfileEpisodeTypesLangTypesPaginationCacheKey | ||
import fr.ziedelth.caches.ProfilePaginationCacheKey | ||
import fr.ziedelth.dtos.MissingAnimeDto | ||
import fr.ziedelth.entities.Anime | ||
import fr.ziedelth.entities.Episode | ||
import fr.ziedelth.repositories.ProfileRepository | ||
import fr.ziedelth.utils.Logger | ||
import java.util.* | ||
|
||
class ProfileService : AbstractService() { | ||
@Inject | ||
private lateinit var repository: ProfileRepository | ||
|
||
private val missingAnimesLoadingCache = CacheBuilder.newBuilder() | ||
.build(object : CacheLoader<ProfileEpisodeTypesLangTypesPaginationCacheKey, List<MissingAnimeDto>>() { | ||
override fun load(key: ProfileEpisodeTypesLangTypesPaginationCacheKey): List<MissingAnimeDto> { | ||
Logger.config("Updating profile missing animes cache") | ||
return repository.getMissingAnimes(key.profile, key.episodeTypes, key.langTypes, key.page, key.limit) | ||
} | ||
}) | ||
|
||
private val watchlistAnimesLoadingCache = CacheBuilder.newBuilder() | ||
.build(object : CacheLoader<ProfilePaginationCacheKey, List<Anime>>() { | ||
override fun load(key: ProfilePaginationCacheKey): List<Anime> { | ||
Logger.config("Updating profile animes cache") | ||
return repository.getWatchlistAnimes(key.profile, key.page, key.limit) | ||
} | ||
}) | ||
|
||
private val watchlistEpisodesLoadingCache = CacheBuilder.newBuilder() | ||
.build(object : CacheLoader<ProfilePaginationCacheKey, List<Episode>>() { | ||
override fun load(key: ProfilePaginationCacheKey): List<Episode> { | ||
Logger.config("Updating profile episodes cache") | ||
return repository.getWatchlistEpisodes(key.profile, key.page, key.limit) | ||
} | ||
}) | ||
|
||
fun invalidateProfile(profile: UUID) { | ||
Logger.warning("Invalidate all profile cache for profile $profile") | ||
|
||
val missingAnimesKeys = missingAnimesLoadingCache.asMap().keys.filter { it.profile == profile } | ||
missingAnimesLoadingCache.invalidateAll(missingAnimesKeys) | ||
|
||
val watchlistAnimesKeys = watchlistAnimesLoadingCache.asMap().keys.filter { it.profile == profile } | ||
watchlistAnimesLoadingCache.invalidateAll(watchlistAnimesKeys) | ||
|
||
val watchlistEpisodesKeys = watchlistEpisodesLoadingCache.asMap().keys.filter { it.profile == profile } | ||
watchlistEpisodesLoadingCache.invalidateAll(watchlistEpisodesKeys) | ||
} | ||
|
||
fun getMissingAnimes(profile: UUID, episodeTypes: List<UUID>, langTypes: List<UUID>, page: Int, limit: Int): List<MissingAnimeDto> = | ||
missingAnimesLoadingCache.getUnchecked(ProfileEpisodeTypesLangTypesPaginationCacheKey(profile, episodeTypes, langTypes, page, limit)) | ||
|
||
fun getWatchlistAnimes(profile: UUID, page: Int, limit: Int): List<Anime> = | ||
watchlistAnimesLoadingCache.getUnchecked(ProfilePaginationCacheKey(profile, page, limit)) | ||
|
||
fun getWatchlistEpisodes(profile: UUID, page: Int, limit: Int): List<Episode> = | ||
watchlistEpisodesLoadingCache.getUnchecked(ProfilePaginationCacheKey(profile, page, limit)) | ||
} |