implemented global caching for tmdb service requests (except search)

This commit is contained in:
X4Lo committed 2024-04-05 03:05:26 +00:00
1 parent 00cbf216c4
commit c84ee2c5eb
3 files changed
+75 -50

No files matched your search

+47 -14
View File
@@ -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<string, Entry[]> = {};
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<any> {
if (this.cach[endPoint]) {
return of({
exist: true,
results: this.cach[endPoint],
});
} else {
return this.http.get<{ results: any[] }>(endPoint);
}
}
fetchdataNoCach(endPoint: string): Observable<any> {
return this.http.get<{ results: any[] }>(endPoint);
}
getEntriesUsingEndPoint(endPoint: string): Observable<Entry[]> {
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)
);
})
);
}
@@ -11,10 +11,11 @@ export class PopularComponent {
entryType: boolean = true;
entries: Entry[] = [];
pages: Record<string, Record<number, Entry[]>> = {
true: {},
false: {},
};
// * old local cach
// pages: Record<string, Record<number, Entry[]>> = {
// true: {},
// false: {},
// };
currentPage: number = 1;
constructor(private tmdbService: TmdbService) {}
@@ -28,24 +29,33 @@ export class PopularComponent {
this.fetchEntries();
}
// * 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];
// });
// }
// }
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];
this.entries = myEntries;
});
}
}
goToPage(page: number) {
this.currentPage = page;
@@ -11,12 +11,6 @@ export class TrendingComponent {
entryType: boolean = true;
entries: Entry[] = [];
pages: Record<string, Record<number, Entry[]>> = {
movieday: {},
dayweek: {},
tvday: {},
tvweek: {},
};
currentPage: number = 1;
period: string = 'day';
@@ -38,24 +32,12 @@ 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)
.getPopular(this.currentPage, this.entryType)
.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.entries = myEntries;
});
}
}
setRange(period: string) {
this.period = period;