I'm trying to implement a caching HTTP network call that dumps the result from the cache after a specific amount of time, so I implemented this operator:
export const cacheForMinutes = <T>(minutes?: number) => <T>(source: Observable<T>) => {
if (!(minutes && minutes > 0))
return source
return source.pipe(share<T>({
connector: () => new ReplaySubject(1),
resetOnComplete: () => timer(minutes * 60_000)
}))
}
Then in my service, I use it like this:
getDtoSingle(..., minutesToCache: number) {
return this.http.get(...).pipe(
map(...),
cacheForMinutes(minutesToCache)
)
}
When I watch the network calls via Chrome developer tools, I can see that it's not actually caching the results for the given time, it's still making the network call each time. What have I done wrong here?
You will need to cache the result in an external variable as you said, and access it with some deep comparison method (like using
object-hash) and then remove it when it is done. Example: