Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Angular2 & RxJS - caching a service that takes a parameter

I'm trying to add caching to an Angular2 HTTP service and just can't seem to figure it out.

I followed other posts and have it working perfectly for HTTP services that don't take any parameters e.g.

workSummaries = this.http.get('http://locahost:8080/teams')
            .map((response: Response) => response.json())
            .publishReplay(1, 3000)
            .refCount()
            .take(1);

However I'm lost in trying to figure out can I make above work when I introduce a teamId parameter that should be sent with the request.

So ideally if the same teamId has been used in the last X seconds just read from the cache else if it's a new teamId or 3 seconds has past then call the service.

Any help would be great.

Thanks, Kevin.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use a Map to maintain the teamID/team association (I didn't touch the Observable creation part):

summaries = new Map < number, Observable < Team >> ();

getTeam(id: number) {
  if (!this.summaries.get(id)) {
    let team = this.http.get('http://locahost:8080/teams/' + id)
      .map((response: Response) => response.json())
      .publishReplay(1, 3000)
      .refCount()
      .take(1);
    this.summaries.set(id, team)
  }
  return this.summaries.get(id);
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!