Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 99f3b70

Browse files
ashtuchkinjbdeboer
authored andcommitted
feat(http): set custom default cache in $http.defaults.cache
When we need more control over http caching, we may want to provide a custom cache to be used in all http requests by default. To skip default cache, set {cache: false} in request configuration. To use other cache, set {cache: cache} as before. See #2079
1 parent 603fe0d commit 99f3b70

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

src/ng/http.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ function $HttpProvider() {
306306
* cache, but the cache is not populated yet, only one request to the server will be made and
307307
* the remaining requests will be fulfilled using the response for the first request.
308308
*
309+
* A custom default cache built with $cacheFactory can be provided in $http.defaults.cache.
310+
* To skip it, set configuration property `cache` to `false`.
311+
*
309312
*
310313
* # Response interceptors
311314
*
@@ -733,8 +736,10 @@ function $HttpProvider() {
733736
promise.then(removePendingReq, removePendingReq);
734737

735738

736-
if (config.cache && config.method == 'GET') {
737-
cache = isObject(config.cache) ? config.cache : defaultCache;
739+
if ((config.cache || defaults.cache) && config.cache !== false && config.method == 'GET') {
740+
cache = isObject(config.cache) ? config.cache
741+
: isObject(defaults.cache) ? defaults.cache
742+
: defaultCache;
738743
}
739744

740745
if (cache) {

test/ng/httpSpec.js

+71
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,77 @@ describe('$http', function() {
923923
expect(callback).toHaveBeenCalledOnce();
924924
})
925925
);
926+
927+
describe('$http.defaults.cache', function () {
928+
929+
it('should be undefined by default', function() {
930+
expect($http.defaults.cache).toBeUndefined()
931+
});
932+
933+
it('should cache requests when no cache given in request config', function() {
934+
$http.defaults.cache = cache;
935+
936+
// First request fills the cache from server response.
937+
$httpBackend.expect('GET', '/url').respond(200, 'content');
938+
$http({method: 'GET', url: '/url'}); // Notice no cache given in config.
939+
$httpBackend.flush();
940+
941+
// Second should be served from cache, without sending request to server.
942+
$http({method: 'get', url: '/url'}).success(callback);
943+
$rootScope.$digest();
944+
945+
expect(callback).toHaveBeenCalledOnce();
946+
expect(callback.mostRecentCall.args[0]).toBe('content');
947+
948+
// Invalidate cache entry.
949+
$http.defaults.cache.remove("/url");
950+
951+
// After cache entry removed, a request should be sent to server.
952+
$httpBackend.expect('GET', '/url').respond(200, 'content');
953+
$http({method: 'GET', url: '/url'});
954+
$httpBackend.flush();
955+
});
956+
957+
it('should have less priority than explicitly given cache', inject(function($cacheFactory) {
958+
var localCache = $cacheFactory('localCache');
959+
$http.defaults.cache = cache;
960+
961+
// Fill local cache.
962+
$httpBackend.expect('GET', '/url').respond(200, 'content-local-cache');
963+
$http({method: 'GET', url: '/url', cache: localCache});
964+
$httpBackend.flush();
965+
966+
// Fill default cache.
967+
$httpBackend.expect('GET', '/url').respond(200, 'content-default-cache');
968+
$http({method: 'GET', url: '/url'});
969+
$httpBackend.flush();
970+
971+
// Serve request from default cache when no local given.
972+
$http({method: 'get', url: '/url'}).success(callback);
973+
$rootScope.$digest();
974+
expect(callback).toHaveBeenCalledOnce();
975+
expect(callback.mostRecentCall.args[0]).toBe('content-default-cache');
976+
callback.reset();
977+
978+
// Serve request from local cache when it is given (but default filled too).
979+
$http({method: 'get', url: '/url', cache: localCache}).success(callback);
980+
$rootScope.$digest();
981+
expect(callback).toHaveBeenCalledOnce();
982+
expect(callback.mostRecentCall.args[0]).toBe('content-local-cache');
983+
}));
984+
985+
it('should be skipped if {cache: false} is passed in request config', function() {
986+
$http.defaults.cache = cache;
987+
988+
$httpBackend.expect('GET', '/url').respond(200, 'content');
989+
$http({method: 'GET', url: '/url'});
990+
$httpBackend.flush();
991+
992+
$httpBackend.expect('GET', '/url').respond();
993+
$http({method: 'GET', url: '/url', cache: false});
994+
$httpBackend.flush();
995+
});
996+
});
926997
});
927998

928999

0 commit comments

Comments
 (0)