Skip to content

Commit 06c533b

Browse files
committed
improve caching
1 parent fb38762 commit 06c533b

1 file changed

Lines changed: 133 additions & 71 deletions

File tree

app/src/main/java/zechs/zplex/data/repository/TmdbRepository.kt

Lines changed: 133 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -74,127 +74,117 @@ class TmdbRepository @Inject constructor(
7474
) = showDao.deleteShowById(tmdbId)
7575

7676
fun getSavedShows() = showDao.getAllShows()
77-
7877
suspend fun getShow(
7978
tvId: Int,
8079
appendToQuery: String? = "credits,recommendations,videos,external_ids"
8180
): Response<TvResponse> {
8281
val cacheKey = "show_${tvId}${if (appendToQuery != null) "_${appendToQuery}" else ""}"
8382
val existingCache = apiCacheDao.getCacheById(cacheKey)
84-
if (existingCache != null) {
85-
if (existingCache.expiration < System.currentTimeMillis()) {
86-
apiCacheDao.deleteCacheById(cacheKey)
87-
Log.d(TAG, "Deleting cache with key: $cacheKey")
88-
} else {
89-
try {
90-
val parsed = parseCache<TvResponse>(existingCache.classType, existingCache.body)
91-
Log.d(TAG, "Retrieved cache successfully with key: $cacheKey")
92-
return Response.success(parsed)
93-
} catch (e: Exception) {
94-
Log.d(TAG, "Cache deserialization failed: " + e.message.toString())
95-
}
83+
if (existingCache != null && existingCache.expiration >= System.currentTimeMillis()) {
84+
try {
85+
val parsed = parseCache<TvResponse>(existingCache.classType, existingCache.body)
86+
Log.d(TAG, "Retrieved cache successfully with key: $cacheKey")
87+
return Response.success(parsed)
88+
} catch (e: Exception) {
89+
Log.d(TAG, "Cache deserialization failed: ${e.message}")
9690
}
9791
}
98-
return try {
92+
try {
9993
val tmdbResponse = tmdbApi.getShow(tvId, append_to_response = appendToQuery)
100-
val show = tmdbResponse.body()
94+
var show = tmdbResponse.body()
10195

10296
if (tmdbResponse.isSuccessful && show != null) {
97+
val expiration = System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000L
10398
val imdbId = show.external_ids?.get("imdb_id")
10499
if (!imdbId.isNullOrBlank()) {
105100
val omdbResponse = omdbApi.fetchTvById(imdbId)
106-
107101
if (omdbResponse.isSuccessful) {
108102
val imdbRating = omdbResponse.body()
109103
?.imdbRating
110104
?.nullIfNAOrElse { it.toDoubleOrNull() }
111105

112106
if (imdbRating != null) {
113-
val updatedMovie = show.copy(
107+
show = show.copy(
114108
vote_average = imdbRating,
115109
isImdbRating = true
116110
)
117-
val expiration = System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000L
118-
Log.d(TAG, "Creating cache with key: $cacheKey")
119-
apiCacheDao.addCache(
120-
ApiCache(
121-
id = cacheKey,
122-
body = gson.toJson(updatedMovie),
123-
classType = TvResponse::class.java.name,
124-
expiration = expiration
125-
)
126-
)
127-
return Response.success(updatedMovie)
128111
}
129112
}
130113
}
114+
115+
apiCacheDao.addCache(
116+
ApiCache(
117+
id = cacheKey,
118+
body = gson.toJson(show),
119+
classType = TvResponse::class.java.name,
120+
expiration = expiration
121+
)
122+
)
123+
Log.d(TAG, "Saved cache with key: $cacheKey")
124+
125+
return Response.success(show)
131126
}
127+
return tmdbResponse
132128

133-
tmdbResponse
134129
} catch (e: Exception) {
135-
Response.error(500, "Exception: ${e.message}".toResponseBody(null))
130+
return Response.error(500, "Exception: ${e.message}".toResponseBody(null))
136131
}
137132
}
138133

134+
139135
suspend fun getMovie(
140136
movieId: Int,
141137
appendToQuery: String? = "credits,recommendations,videos"
142138
): Response<MovieResponse> {
143139
val cacheKey = "movie_${movieId}${if (appendToQuery != null) "_${appendToQuery}" else ""}"
144140
val existingCache = apiCacheDao.getCacheById(cacheKey)
145-
if (existingCache != null) {
146-
if (existingCache.expiration < System.currentTimeMillis()) {
147-
apiCacheDao.deleteCacheById(cacheKey)
148-
Log.d(TAG, "Deleting cache with key: $cacheKey")
149-
} else {
150-
try {
151-
val parsed =
152-
parseCache<MovieResponse>(existingCache.classType, existingCache.body)
153-
Log.d(TAG, "Retrieved cache successfully with key: $cacheKey")
154-
return Response.success(parsed)
155-
} catch (e: Exception) {
156-
Log.d(TAG, "Cache deserialization failed: " + e.message.toString())
157-
}
141+
if (existingCache != null && existingCache.expiration >= System.currentTimeMillis()) {
142+
try {
143+
val parsed = parseCache<MovieResponse>(existingCache.classType, existingCache.body)
144+
Log.d(TAG, "Retrieved cache successfully with key: $cacheKey")
145+
return Response.success(parsed)
146+
} catch (e: Exception) {
147+
Log.d(TAG, "Cache deserialization failed: ${e.message}")
158148
}
159149
}
160-
return try {
150+
try {
161151
val tmdbResponse = tmdbApi.getMovie(movieId, append_to_response = appendToQuery)
162-
val movie = tmdbResponse.body()
152+
var movie = tmdbResponse.body()
163153

164154
if (tmdbResponse.isSuccessful && movie != null) {
155+
val expiration = System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000L
156+
165157
val imdbId = movie.imdb_id
166158
if (!imdbId.isNullOrBlank()) {
167159
val omdbResponse = omdbApi.fetchMovieById(imdbId)
168-
169160
if (omdbResponse.isSuccessful) {
170161
val imdbRating = omdbResponse.body()
171162
?.imdbRating
172163
?.nullIfNAOrElse { it.toDoubleOrNull() }
173164

174165
if (imdbRating != null) {
175-
val updatedMovie = movie.copy(
166+
movie = movie.copy(
176167
vote_average = imdbRating,
177168
isImdbRating = true
178169
)
179-
val expiration = System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000L
180-
Log.d(TAG, "Creating cache with key: $cacheKey")
181-
apiCacheDao.addCache(
182-
ApiCache(
183-
id = cacheKey,
184-
body = gson.toJson(updatedMovie),
185-
classType = MovieResponse::class.java.name,
186-
expiration = expiration
187-
)
188-
)
189-
return Response.success(updatedMovie)
190170
}
191171
}
192172
}
193-
}
194173

195-
tmdbResponse
174+
apiCacheDao.addCache(
175+
ApiCache(
176+
id = cacheKey,
177+
body = gson.toJson(movie),
178+
classType = MovieResponse::class.java.name,
179+
expiration = expiration
180+
)
181+
)
182+
Log.d(TAG, "Saved cache with key: $cacheKey")
183+
return Response.success(movie)
184+
}
185+
return tmdbResponse
196186
} catch (e: Exception) {
197-
Response.error(500, "Exception: ${e.message}".toResponseBody(null))
187+
return Response.error(500, "Exception: ${e.message}".toResponseBody(null))
198188
}
199189
}
200190

@@ -298,20 +288,92 @@ class TmdbRepository @Inject constructor(
298288
suspend fun getShowsFromCompany(
299289
companyId: Int,
300290
page: Int
301-
) = tmdbApi.getFromCompany(
302-
media_type = MediaType.tv,
303-
with_companies = companyId,
304-
page = page
305-
)
291+
): Response<SearchResponse> {
292+
val cacheKey = "show_company_${companyId}_${page}"
293+
val existingCache = apiCacheDao.getCacheById(cacheKey)
294+
if (existingCache != null) {
295+
if (existingCache.expiration < System.currentTimeMillis()) {
296+
apiCacheDao.deleteCacheById(cacheKey)
297+
Log.d(TAG, "Deleting cache with key: $cacheKey")
298+
} else {
299+
try {
300+
val parsed =
301+
parseCache<SearchResponse>(existingCache.classType, existingCache.body)
302+
Log.d(TAG, "Retrieved cache successfully with key: $cacheKey")
303+
return Response.success(parsed)
304+
} catch (e: Exception) {
305+
Log.d(
306+
TAG,
307+
"Cache deserialization failed (cache will be deleted): " + e.message.toString()
308+
)
309+
apiCacheDao.deleteCacheById(cacheKey)
310+
}
311+
}
312+
}
313+
val company = tmdbApi.getFromCompany(
314+
media_type = MediaType.tv,
315+
with_companies = companyId,
316+
page = page
317+
)
318+
if (company.isSuccessful && company.body() != null) {
319+
val expiration = System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000L
320+
Log.d(TAG, "Creating cache with key: $cacheKey")
321+
apiCacheDao.addCache(
322+
ApiCache(
323+
id = cacheKey,
324+
body = gson.toJson(company.body()),
325+
classType = SearchResponse::class.java.name,
326+
expiration = expiration
327+
)
328+
)
329+
}
330+
return company
331+
}
306332

307333
suspend fun getMoviesFromCompany(
308334
companyId: Int,
309335
page: Int
310-
) = tmdbApi.getFromCompany(
311-
media_type = MediaType.movie,
312-
with_companies = companyId,
313-
page = page
314-
)
336+
): Response<SearchResponse> {
337+
val cacheKey = "movie_company_${companyId}_${page}"
338+
val existingCache = apiCacheDao.getCacheById(cacheKey)
339+
if (existingCache != null) {
340+
if (existingCache.expiration < System.currentTimeMillis()) {
341+
apiCacheDao.deleteCacheById(cacheKey)
342+
Log.d(TAG, "Deleting cache with key: $cacheKey")
343+
} else {
344+
try {
345+
val parsed =
346+
parseCache<SearchResponse>(existingCache.classType, existingCache.body)
347+
Log.d(TAG, "Retrieved cache successfully with key: $cacheKey")
348+
return Response.success(parsed)
349+
} catch (e: Exception) {
350+
Log.d(
351+
TAG,
352+
"Cache deserialization failed (cache will be deleted): " + e.message.toString()
353+
)
354+
apiCacheDao.deleteCacheById(cacheKey)
355+
}
356+
}
357+
}
358+
val company = tmdbApi.getFromCompany(
359+
media_type = MediaType.movie,
360+
with_companies = companyId,
361+
page = page
362+
)
363+
if (company.isSuccessful && company.body() != null) {
364+
val expiration = System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000L
365+
Log.d(TAG, "Creating cache with key: $cacheKey")
366+
apiCacheDao.addCache(
367+
ApiCache(
368+
id = cacheKey,
369+
body = gson.toJson(company.body()),
370+
classType = SearchResponse::class.java.name,
371+
expiration = expiration
372+
)
373+
)
374+
}
375+
return company
376+
}
315377

316378
suspend fun getPerson(
317379
personId: Int

0 commit comments

Comments
 (0)