5

I have a page which makes http requests to the same location, just with different parameters depending on what the user wants. So my code looks something like this:

this.http.post( //I am open to POST methods or GET methods as I have security in the back to prevent malicious writes.
   'http://192.168.1.45:3000/mylocation',
   'p1=' + param1 + '&p2=' + param2, 
   {headers: headers}
)

In JQuery for example you have build into the framework a cache attribute which caches automatically and is very easy to implement:

$.ajax({
  cache: true,
  //other options...
});

Does Angular2 have something similar to this? I would like to cache these dynamic responses as long as the user is in the application. So if a user requests the same url with the same parameters then it would just grab it from the cache, and if the params were never yet used then it would make the network call.

I could not find anything in the Angular2 docs in the request options:

https://angular.io/docs/js/latest/api/http/RequestOptions-class.html

user2924127
  • 6,034
  • 16
  • 78
  • 136

1 Answers1

10

Cache the data, and if cached data is available return this, otherwise make the HTTP request. If you want to reuse for different requests (parameters) you can adjust to store the references in an array instead.

getData() {
    if(this.data) {
      // if `data` is available just return it as `Observable`
      return Observable.of(this.data); 
    else if(this.observable) {
      // if `this.observable` is set then the request is in progress
      // return the `Observable` for the ongoing request
      return this.observable;
    } else {
      // create the request, store the `Observable` for subsequent subscribers
      this.observable = this.http.get('/someUrl')
          .map(res => res.json())
          .do(val => {
            this.data = val;
            // when the cached data is available we don't need the `Observable` reference anymore
            this.observable = null;
          })
          // make it shared so more than one subscriber can get the result
          .share();
      return this.observable;
    }
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks for the reply. I am having a little trouble understanding as I am very new to observable so I am sure I am missing something here. Do I make the this.data and array if I need to store more? And if so how then would it go through the array and see if the parameters are the same as data would only store the response(data) and not the parameters? Thanks. – user2924127 Mar 29 '16 at 20:23
  • You would need to store the data with the parameter to get the cache for a parameter when the method is called again with the same parameter – Günter Zöchbauer Mar 29 '16 at 20:25
  • Thanks. I will look into this to see if I can implement it. – user2924127 Mar 29 '16 at 21:05
  • 1
    I found a more elegant solution that I successfully implemented here: http://www.syntaxsuccess.com/viewarticle/caching-with-rxjs-observables-in-angular-2.0 – JLavoie Jan 29 '17 at 20:11