title: Caching Data From HttpClient in Angular 4
body: <p>i have a problem in making my caching more simpler. I think there is a better way to do this. My problem is that i have to do this "caching" codes in every get() function which is results in longer codes. Anyone help on how to do this the best way? Thanks. Here's my codes below. What i did in my codes is i do the get() function in my news.service.ts to get data from the http and i subscribe it in my news-list.</p>
  
  <blockquote>
    <p>news.service.ts</p>
  </blockquote>
  
  <pre><code>getAllNews() {
  
      if(this.newslist != null) {
        return Observable.of(this.newslist);
      } 
  
      else {
  
        return this.httpClient
          .get('http://sample.com/news')
          .map((response =&gt; response))
          .do(newslist =&gt; this.newslist = newslist)
          .catch(e =&gt; {
              if (e.status === 401) {
                  return Observable.throw('Unauthorized');           
              }
  
          });
      }
    }
  </code></pre>
  
  <blockquote>
    <p>news-list.service.ts</p>
  </blockquote>
  
  <pre><code> this.subscription = this.newsService.getAllNews()
        .subscribe(
          (data:any) =&gt; {
            console.log(data);
            this.newslists = data.data.data;
          },
          error =&gt; {
            this.authService.logout()
            this.router.navigate(['signin']);
          });
    }
  </code></pre>