211

I want to be able to create a custom AngularJS service that makes an HTTP 'Get' request when its data object is empty and populates the data object on success.

The next time a call is made to this service, I would like to bypass the overhead of making the HTTP request again and instead return the cached data object.

Is this possible?

0

7 Answers 7

318

Angular's $http has a cache built in. According to the docs:

cache – {boolean|Object} – A boolean value or object created with $cacheFactory to enable or disable caching of the HTTP response. See $http Caching for more information.

Boolean value

So you can set cache to true in its options:

$http.get(url, { cache: true}).success(...);

or, if you prefer the config type of call:

$http({ cache: true, url: url, method: 'GET'}).success(...);

Cache Object

You can also use a cache factory:

var cache = $cacheFactory('myCache');

$http.get(url, { cache: cache })

You can implement it yourself using $cacheFactory (especially handly when using $resource):

var cache = $cacheFactory('myCache');

var data = cache.get(someKey);

if (!data) {
   $http.get(url).success(function(result) {
      data = result;
      cache.put(someKey, data);
   });
}
18
  • 47
    Question: what's the point of saving cached data into $cacheFactory.. why not just save it into a local object in the Service? Any good reasons?
    – Spock
    Sep 10, 2013 at 10:36
  • 7
    Check this out. It gives you lots of customizability including localStorage support, timeout support, all kinds of goodies http://jmdobry.github.io/angular-cache/ Feb 1, 2014 at 18:06
  • 4
    I'm especially curious about status code 304 - does browser cache work without enabling cache:true? If not, does cache:true make it work? Is caching permanent or it's just in RAM and is unloaded when the page is closed? Feb 4, 2015 at 0:01
  • 3
    Any way to specify a time limit on this cache without manually implementing it?
    – Mark
    Mar 13, 2015 at 18:25
  • 11
    @Spock, $cacheFactory itself is a service that can be used across multiple controllers and angular components. It can be used as a generic api service to cache all your $http's into a single service obj rather than having different service objects for each one of them. Jul 9, 2015 at 5:10
48

I think there's an even easier way now. This enables basic caching for all $http requests (which $resource inherits):

 var app = angular.module('myApp',[])
      .config(['$httpProvider', function ($httpProvider) {
            // enable http caching
           $httpProvider.defaults.cache = true;
      }])
3
  • 47
    You hardly want to cache every single http request. I don't see when that would ever be the case?
    – Spock
    Sep 5, 2013 at 11:10
  • 1
    Every app/module is different, no?! Sep 11, 2014 at 14:53
  • 13
    If you want to cache the majority of requests then setting the default to true is handy. Oct 22, 2014 at 8:04
12

An easier way to do this in the current stable version (1.0.6) requires a lot less code.

After setting up your module add a factory:

var app = angular.module('myApp', []);
// Configure routes and controllers and views associated with them.
app.config(function ($routeProvider) {
    // route setups
});
app.factory('MyCache', function ($cacheFactory) {
    return $cacheFactory('myCache');
});

Now you can pass this into your controller:

app.controller('MyController', function ($scope, $http, MyCache) {
    $http.get('fileInThisCase.json', { cache: MyCache }).success(function (data) {
        // stuff with results
    });
});

One downside is that the key names are also setup automatically, which could make clearing them tricky. Hopefully they'll add in some way to get key names.

0
7

Check out the library angular-cache if you like $http's built-in caching but want more control. You can use it to seamlessly augment $http cache with time-to-live, periodic purges, and the option of persisting the cache to localStorage so that it's available across sessions.

FWIW, it also provides tools and patterns for making your cache into a more dynamic sort of data-store that you can interact with as POJO's, rather than just the default JSON strings. Can't comment on the utility of that option as yet.

(Then, on top of that, related library angular-data is sort of a replacement for $resource and/or Restangular, and is dependent upon angular-cache.)

2
  • 3
    Please note that angular-data is deprecated now. The latest on is js-data-angular js-data.io/v1.8.0/docs/js-data-angular
    – demisx
    May 28, 2015 at 6:37
  • The angular-cache library has the features that should have been built into Angular's $cacheFactory. The built-in solution seems almost useless given it's limitations in being able to expire specific caches. The angular-cache factory was one of the easiest 3rd party libraries to implement as well.
    – Darryl
    Oct 19, 2015 at 17:01
5

As AngularJS factories are singletons, you can simply store the result of the http request and retrieve it next time your service is injected into something.

angular.module('myApp', ['ngResource']).factory('myService',
  function($resource) {
    var cache = false;
    return {
      query: function() {
        if(!cache) {
          cache = $resource('http://example.com/api').query();
        }
        return cache;
      }
    };
  }
);
2
  • I have one question how to check if GET failed and in that case not to put into cache the $resource...query()
    – robert
    Jul 7, 2016 at 21:31
  • @robert you can check on the second argument of the .then method or better yet, use the .catch callback. For example $http .get(url) .then(successCallback, failCallback) or $http .get(url) .then(successCallback, failCallback) .catch(errorCallback) The error callback will be executed even if something bad happens in the failCallback, although it's more common to avoid the fail callback at all and use .then(success).catch(manageRequestFail). Hope that helps to grasp the idea, more info in the angular $http documentation.
    – Faito
    Sep 19, 2016 at 14:15
2
angularBlogServices.factory('BlogPost', ['$resource',
    function($resource) {
        return $resource("./Post/:id", {}, {
            get:    {method: 'GET',    cache: true,  isArray: false},
            save:   {method: 'POST',   cache: false, isArray: false},
            update: {method: 'PUT',    cache: false, isArray: false},
            delete: {method: 'DELETE', cache: false, isArray: false}
        });
    }]);

set cache to be true.

1
  • This would be as secure as the client application withing the browser itself just like any other web app.
    – bhantol
    Nov 9, 2016 at 16:10
-2

In Angular 8 we can do like this:

import { Injectable } from '@angular/core';
import { YourModel} from '../models/<yourModel>.model';
import { UserService } from './user.service';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class GlobalDataService {

  private me: <YourModel>;

  private meObservable: Observable<User>;

  constructor(private yourModalService: <yourModalService>, private http: HttpClient) {

  }

  ngOnInit() {

  }


  getYourModel(): Observable<YourModel> {

    if (this.me) {
      return of(this.me);
    } else if (this.meObservable) {
      return this.meObservable;
    }
    else {
      this.meObservable = this.yourModalService.getCall<yourModel>() // Your http call
      .pipe(
        map(data => {
          this.me = data;
          return data;
        })
      );
      return this.meObservable;
    }
  }
}

You can call it like this:

this.globalDataService.getYourModel().subscribe(yourModel => {


});

The above code will cache the result of remote API at first call so that it can be used on further requests to that method.

1
  • The question is related to AngularJS not Angular which are totally different frameworks Aug 16, 2020 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.