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
+82 -57

No files matched your search

@@ -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,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) {
@@ -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,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) {