From c84ee2c5eb85992fd1617f20ff5f2e7a75e234c6 Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Fri, 5 Apr 2024 03:05:26 +0000 Subject: [PATCH] implemented global caching for tmdb service requests (except search) --- .../src/app/core/services/tmdb.service.ts | 61 ++++++++++++++----- .../pages/popular/popular.component.ts | 50 +++++++++------ .../pages/trending/trending.component.ts | 28 ++------- 3 files changed, 82 insertions(+), 57 deletions(-) diff --git a/frontend/src/app/core/services/tmdb.service.ts b/frontend/src/app/core/services/tmdb.service.ts index 94b4c85..7aefdee 100644 --- a/frontend/src/app/core/services/tmdb.service.ts +++ b/frontend/src/app/core/services/tmdb.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; -import { Observable, map } from 'rxjs'; +import { Observable, map, of } from 'rxjs'; import { Entry } from '../../shared/models/entry.model'; import { EntryId } from '../../shared/models/entryId.model'; import { EntryRating } from '../../shared/models/entryRating.model'; @@ -9,21 +9,49 @@ import { EntryRating } from '../../shared/models/entryRating.model'; providedIn: 'root', }) export class TmdbService { + cach: Record = {}; + constructor(private http: HttpClient) {} // private apiUrl = environment.apiBaseUrl; - private tmdb_apiUrl = ' https://api.themoviedb.org/3'; + private tmdb_apiUrl = 'https://api.themoviedb.org/3'; private tmdb_apiKey = '5323ef3dc430df54dcb9e58c37fd22bb'; fetchdata(endPoint: string): Observable { + if (this.cach[endPoint]) { + return of({ + exist: true, + results: this.cach[endPoint], + }); + } else { + return this.http.get<{ results: any[] }>(endPoint); + } + } + + fetchdataNoCach(endPoint: string): Observable { return this.http.get<{ results: any[] }>(endPoint); } getEntriesUsingEndPoint(endPoint: string): Observable { return this.fetchdata(endPoint).pipe( - map((response) => - response.results.map((movie: any) => this.adaptMovieToEntry(movie)) - ) + // map((response) => + // response.results.map((movie: any) => this.adaptMovieToEntry(movie)) + // ) + map((response) => { + // Check if the data is from cache and has `exist` property + if (response.exist) { + // Return the cached data directly + return response.results; + } else { + // Data is not from cache, process and adapt it + const results = response.results ? response.results : response; + const fetchedData = results.map((data: any) => + this.adaptMovieToEntry(data) + ); + this.cach[endPoint] = fetchedData; + return fetchedData; + } + }) ); } @@ -31,11 +59,7 @@ export class TmdbService { var type = isMovie ? 'movie' : 'tv'; var endPoint = `${this.tmdb_apiUrl}/${type}/popular?page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`; - return this.fetchdata(endPoint).pipe( - map((response) => - response.results.map((movie: any) => this.adaptMovieToEntry(movie)) - ) - ); + return this.getEntriesUsingEndPoint(endPoint); } getTrending( @@ -46,10 +70,19 @@ export class TmdbService { var type = isMovie ? 'movie' : 'tv'; var endPoint = `${this.tmdb_apiUrl}/trending/${type}/${period}?page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`; - return this.fetchdata(endPoint).pipe( - map((response) => - response.results.map((movie: any) => this.adaptMovieToEntry(movie)) - ) + return this.getEntriesUsingEndPoint(endPoint); + } + + getByKeyword(keyword: string, type: string, page: number) { + var endPoint = `${this.tmdb_apiUrl}/search/${type}?query=${keyword}&page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`; + + // return this.getEntriesUsingEndPoint(endPoint); + return this.fetchdataNoCach(endPoint).pipe( + map((response) => { + return response.results.map((movie: any) => + this.adaptMovieToEntry(movie) + ); + }) ); } diff --git a/frontend/src/app/features/pages/popular/popular.component.ts b/frontend/src/app/features/pages/popular/popular.component.ts index a034379..328779e 100644 --- a/frontend/src/app/features/pages/popular/popular.component.ts +++ b/frontend/src/app/features/pages/popular/popular.component.ts @@ -11,10 +11,11 @@ export class PopularComponent { entryType: boolean = true; entries: Entry[] = []; - pages: Record> = { - true: {}, - false: {}, - }; + // * old local cach + // pages: Record> = { + // true: {}, + // false: {}, + // }; currentPage: number = 1; constructor(private tmdbService: TmdbService) {} @@ -28,23 +29,32 @@ export class PopularComponent { this.fetchEntries(); } - fetchEntries() { - if (this.pages[this.entryType.toString()][this.currentPage]) { - // ? not in cash > fetch entries from TMDB - this.entries = this.pages[this.entryType.toString()][this.currentPage]; - } else { - // ? in cash > use cash entries - this.tmdbService - .getPopular(this.currentPage, this.entryType) - .subscribe((myEntries) => { - // * add fetched entries to the cash - this.pages[this.entryType.toString()][this.currentPage] = myEntries; + // * old methode using local cach + // fetchEntries() { + // if (this.pages[this.entryType.toString()][this.currentPage]) { + // // ? not in cash > fetch entries from TMDB + // this.entries = this.pages[this.entryType.toString()][this.currentPage]; + // } else { + // // ? in cash > use cash entries + // this.tmdbService + // .getPopular(this.currentPage, this.entryType) + // .subscribe((myEntries) => { + // // * add fetched entries to the cash + // this.pages[this.entryType.toString()][this.currentPage] = myEntries; - // * add fetched entries to the display list - this.entries = - this.pages[this.entryType.toString()][this.currentPage]; - }); - } + // // * add fetched entries to the display list + // this.entries = + // this.pages[this.entryType.toString()][this.currentPage]; + // }); + // } + // } + + fetchEntries() { + this.tmdbService + .getPopular(this.currentPage, this.entryType) + .subscribe((myEntries) => { + this.entries = myEntries; + }); } goToPage(page: number) { diff --git a/frontend/src/app/features/pages/trending/trending.component.ts b/frontend/src/app/features/pages/trending/trending.component.ts index 6b5e77b..a2bbda9 100644 --- a/frontend/src/app/features/pages/trending/trending.component.ts +++ b/frontend/src/app/features/pages/trending/trending.component.ts @@ -11,12 +11,6 @@ export class TrendingComponent { entryType: boolean = true; entries: Entry[] = []; - pages: Record> = { - movieday: {}, - dayweek: {}, - tvday: {}, - tvweek: {}, - }; currentPage: number = 1; period: string = 'day'; @@ -38,23 +32,11 @@ export class TrendingComponent { } fetchEntries() { - const cachKey = (this.entryType ? 'movie' : 'tv') + this.period; // ex: movieday, movieweek, tvday, tvweek - - if (this.pages[cachKey][this.currentPage]) { - // ? not in cash > fetch entries from TMDB - this.entries = this.pages[cachKey][this.currentPage]; - } else { - // ? in cash > use cash entries - this.tmdbService - .getTrending(this.currentPage, this.entryType, this.period) - .subscribe((myEntries) => { - // * add fetched entries to the cash - this.pages[cachKey][this.currentPage] = myEntries; - - // * add fetched entries to the display list - this.entries = this.pages[cachKey][this.currentPage]; - }); - } + this.tmdbService + .getPopular(this.currentPage, this.entryType) + .subscribe((myEntries) => { + this.entries = myEntries; + }); } setRange(period: string) {