implemented global caching for tmdb service requests (except search)
This commit is contained in:
1 parent
00cbf216c4
commit
c84ee2c5eb
3 files changed
+82
-57
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
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 { Entry } from '../../shared/models/entry.model';
|
||||||
import { EntryId } from '../../shared/models/entryId.model';
|
import { EntryId } from '../../shared/models/entryId.model';
|
||||||
import { EntryRating } from '../../shared/models/entryRating.model';
|
import { EntryRating } from '../../shared/models/entryRating.model';
|
||||||
@@ -9,21 +9,49 @@ import { EntryRating } from '../../shared/models/entryRating.model';
|
|||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class TmdbService {
|
export class TmdbService {
|
||||||
|
cach: Record<string, Entry[]> = {};
|
||||||
|
|
||||||
constructor(private http: HttpClient) {}
|
constructor(private http: HttpClient) {}
|
||||||
|
|
||||||
// private apiUrl = environment.apiBaseUrl;
|
// private apiUrl = environment.apiBaseUrl;
|
||||||
private tmdb_apiUrl = ' https://api.themoviedb.org/3';
|
private tmdb_apiUrl = 'https://api.themoviedb.org/3';
|
||||||
private tmdb_apiKey = '5323ef3dc430df54dcb9e58c37fd22bb';
|
private tmdb_apiKey = '5323ef3dc430df54dcb9e58c37fd22bb';
|
||||||
|
|
||||||
fetchdata(endPoint: string): Observable<any> {
|
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);
|
return this.http.get<{ results: any[] }>(endPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
getEntriesUsingEndPoint(endPoint: string): Observable<Entry[]> {
|
getEntriesUsingEndPoint(endPoint: string): Observable<Entry[]> {
|
||||||
return this.fetchdata(endPoint).pipe(
|
return this.fetchdata(endPoint).pipe(
|
||||||
map((response) =>
|
// map((response) =>
|
||||||
response.results.map((movie: any) => this.adaptMovieToEntry(movie))
|
// 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 type = isMovie ? 'movie' : 'tv';
|
||||||
var endPoint = `${this.tmdb_apiUrl}/${type}/popular?page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`;
|
var endPoint = `${this.tmdb_apiUrl}/${type}/popular?page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`;
|
||||||
|
|
||||||
return this.fetchdata(endPoint).pipe(
|
return this.getEntriesUsingEndPoint(endPoint);
|
||||||
map((response) =>
|
|
||||||
response.results.map((movie: any) => this.adaptMovieToEntry(movie))
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getTrending(
|
getTrending(
|
||||||
@@ -46,10 +70,19 @@ export class TmdbService {
|
|||||||
var type = isMovie ? 'movie' : 'tv';
|
var type = isMovie ? 'movie' : 'tv';
|
||||||
var endPoint = `${this.tmdb_apiUrl}/trending/${type}/${period}?page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`;
|
var endPoint = `${this.tmdb_apiUrl}/trending/${type}/${period}?page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`;
|
||||||
|
|
||||||
return this.fetchdata(endPoint).pipe(
|
return this.getEntriesUsingEndPoint(endPoint);
|
||||||
map((response) =>
|
}
|
||||||
response.results.map((movie: any) => this.adaptMovieToEntry(movie))
|
|
||||||
)
|
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;
|
entryType: boolean = true;
|
||||||
entries: Entry[] = [];
|
entries: Entry[] = [];
|
||||||
|
|
||||||
pages: Record<string, Record<number, Entry[]>> = {
|
// * old local cach
|
||||||
true: {},
|
// pages: Record<string, Record<number, Entry[]>> = {
|
||||||
false: {},
|
// true: {},
|
||||||
};
|
// false: {},
|
||||||
|
// };
|
||||||
currentPage: number = 1;
|
currentPage: number = 1;
|
||||||
|
|
||||||
constructor(private tmdbService: TmdbService) {}
|
constructor(private tmdbService: TmdbService) {}
|
||||||
@@ -28,23 +29,32 @@ export class PopularComponent {
|
|||||||
this.fetchEntries();
|
this.fetchEntries();
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchEntries() {
|
// * old methode using local cach
|
||||||
if (this.pages[this.entryType.toString()][this.currentPage]) {
|
// fetchEntries() {
|
||||||
// ? not in cash > fetch entries from TMDB
|
// if (this.pages[this.entryType.toString()][this.currentPage]) {
|
||||||
this.entries = this.pages[this.entryType.toString()][this.currentPage];
|
// // ? not in cash > fetch entries from TMDB
|
||||||
} else {
|
// this.entries = this.pages[this.entryType.toString()][this.currentPage];
|
||||||
// ? in cash > use cash entries
|
// } else {
|
||||||
this.tmdbService
|
// // ? in cash > use cash entries
|
||||||
.getPopular(this.currentPage, this.entryType)
|
// this.tmdbService
|
||||||
.subscribe((myEntries) => {
|
// .getPopular(this.currentPage, this.entryType)
|
||||||
// * add fetched entries to the cash
|
// .subscribe((myEntries) => {
|
||||||
this.pages[this.entryType.toString()][this.currentPage] = myEntries;
|
// // * add fetched entries to the cash
|
||||||
|
// this.pages[this.entryType.toString()][this.currentPage] = myEntries;
|
||||||
|
|
||||||
// * add fetched entries to the display list
|
// // * add fetched entries to the display list
|
||||||
this.entries =
|
// this.entries =
|
||||||
this.pages[this.entryType.toString()][this.currentPage];
|
// this.pages[this.entryType.toString()][this.currentPage];
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
fetchEntries() {
|
||||||
|
this.tmdbService
|
||||||
|
.getPopular(this.currentPage, this.entryType)
|
||||||
|
.subscribe((myEntries) => {
|
||||||
|
this.entries = myEntries;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
goToPage(page: number) {
|
goToPage(page: number) {
|
||||||
|
|||||||
@@ -11,12 +11,6 @@ export class TrendingComponent {
|
|||||||
entryType: boolean = true;
|
entryType: boolean = true;
|
||||||
entries: Entry[] = [];
|
entries: Entry[] = [];
|
||||||
|
|
||||||
pages: Record<string, Record<number, Entry[]>> = {
|
|
||||||
movieday: {},
|
|
||||||
dayweek: {},
|
|
||||||
tvday: {},
|
|
||||||
tvweek: {},
|
|
||||||
};
|
|
||||||
currentPage: number = 1;
|
currentPage: number = 1;
|
||||||
|
|
||||||
period: string = 'day';
|
period: string = 'day';
|
||||||
@@ -38,23 +32,11 @@ export class TrendingComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetchEntries() {
|
fetchEntries() {
|
||||||
const cachKey = (this.entryType ? 'movie' : 'tv') + this.period; // ex: movieday, movieweek, tvday, tvweek
|
this.tmdbService
|
||||||
|
.getPopular(this.currentPage, this.entryType)
|
||||||
if (this.pages[cachKey][this.currentPage]) {
|
.subscribe((myEntries) => {
|
||||||
// ? not in cash > fetch entries from TMDB
|
this.entries = myEntries;
|
||||||
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];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setRange(period: string) {
|
setRange(period: string) {
|
||||||
|
|||||||
Reference in new issue
Block a user