I have this code, its idea is to save the product in localstorage and when the product is already in localstorage it doesn't make the request again
getItemById(id) {
// return this.http.get(`${environment.API_URL}item/getById/${id}`);
return this.cacheS.getOrSetCache(`StoreService_getItemById_${id}_${this.layout.emp.id}`, this.http.get(`${environment.API_URL}item/getById/${id}`), 300000);
}
getOrSetCache(key: string, request: Observable<any>, msToExpire = 3600000): Observable<any> {
let cache: any = {};
const keyy = 'StoreService_getItemById_teste';
cache = JSON.parse(localStorage.getItem(keyy));
return (cache?.data && (cache?.exp > Date.now())) ?
of(cache.data) :
request.pipe(
tap(v => {
let arr: any[] = [];
let string = localStorage.getItem(keyy);
if (string) arr = JSON.parse(string);
console.log(arr)
arr.push({data: v, exp: (Date.now() + msToExpire)});
localStorage.setItem(keyy, JSON.stringify(arr));
})
);
}
how could i map the ids so that when i already have it in localstorage it doesn't make the request for that id ?
also wanted to know how I could see about the expiration date if the current time is equal to or greater than this timestamp, it removes it from the cache and does the query again

