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

Commit e4682e3

Browse files
committed
feat($http): change $http to allow removing/replacing individual requests in $http cache
Adds $http.removeCache method. Adds/implements replaceCache property in $http request configuration. Closes #5968
1 parent e3764e3 commit e4682e3

File tree

2 files changed

+80
-22
lines changed

2 files changed

+80
-22
lines changed

src/ng/http.js

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,18 @@ function $HttpProvider() {
425425
* If you set the default cache to `false` then only requests that specify their own custom
426426
* cache object will be cached.
427427
*
428-
* ## Interceptors
428+
* If you need to bypass and overwrite the cache you can set the `replaceCache` property to
429+
* `true` in the request configuration.
430+
*
431+
* If you wish to purge stale data from the cache without making a request you can use the
432+
* {@link ng.$http#methods_removeCache $http.removeCache} method and pass it the configuration
433+
* object that was used to create the request you wish to purge.
434+
* ```js
435+
* $http.removeCache({method: 'GET', url: '/someUrl'})
436+
* ```
437+
*
438+
*
439+
* # Interceptors
429440
*
430441
* Before you start creating interceptors, be sure to understand the
431442
* {@link ng.$q $q and deferred/promise APIs}.
@@ -595,6 +606,8 @@ function $HttpProvider() {
595606
* GET request, otherwise if a cache instance built with
596607
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
597608
* caching.
609+
* - **replaceCache** - `{boolean}` - If true this request will bypas the cache and replace
610+
* any previously cached value.
598611
* - **timeout** – `{number|Promise}` – timeout in milliseconds, or {@link ng.$q promise}
599612
* that should abort the request when resolved.
600613
* - **withCredentials** - `{boolean}` - whether to set the `withCredentials` flag on the
@@ -653,20 +666,20 @@ function $HttpProvider() {
653666
$scope.method = 'GET';
654667
$scope.url = 'http-hello.html';
655668
656-
$scope.fetch = function() {
657-
$scope.code = null;
658-
$scope.response = null;
659-
660-
$http({method: $scope.method, url: $scope.url, cache: $templateCache}).
661-
success(function(data, status) {
662-
$scope.status = status;
663-
$scope.data = data;
664-
}).
665-
error(function(data, status) {
666-
$scope.data = data || "Request failed";
667-
$scope.status = status;
668-
});
669-
};
669+
$scope.fetch = function() {
670+
$scope.code = null;
671+
$scope.response = null;
672+
673+
$http({method: $scope.method, url: $scope.url, cache: $templateCache}).
674+
success(function(data, status) {
675+
$scope.status = status;
676+
$scope.data = data;
677+
}).
678+
error(function(data, status) {
679+
$scope.data = data || "Request failed";
680+
$scope.status = status;
681+
});
682+
};
670683
671684
$scope.updateModel = function(method, url) {
672685
$scope.method = method;
@@ -756,7 +769,7 @@ function $HttpProvider() {
756769
}
757770
});
758771

759-
while (chain.length) {
772+
while(chain.length) {
760773
var thenFn = chain.shift();
761774
var rejectFn = chain.shift();
762775

@@ -926,6 +939,29 @@ function $HttpProvider() {
926939
*/
927940
createShortMethodsWithData('post', 'put', 'patch');
928941

942+
/**
943+
* @ngdoc method
944+
* @name $http#removeCache
945+
*
946+
* @description
947+
* Removes a request from the cache without generating a new request.
948+
*
949+
* @param {Object} config configuration object (as per the request that created the cache entry)
950+
*/
951+
$http.removeCache = function(config) {
952+
953+
var url = buildUrl(config.url, config.params);
954+
955+
var cache = isObject(config.cache) ? config.cache
956+
: isObject(defaults.cache) ? defaults.cache
957+
: defaultCache;
958+
959+
if (cache) {
960+
cache.remove(url);
961+
}
962+
963+
};
964+
929965
/**
930966
* @ngdoc property
931967
* @name $http#defaults
@@ -992,7 +1028,11 @@ function $HttpProvider() {
9921028
}
9931029

9941030
if (cache) {
995-
cachedResp = cache.get(url);
1031+
if (config.replaceCache) {
1032+
cache.remove(url);
1033+
} else {
1034+
cachedResp = cache.get(url);
1035+
}
9961036
if (isDefined(cachedResp)) {
9971037
if (isPromiseLike(cachedResp)) {
9981038
// cached request has already been sent, but there is no response yet
@@ -1084,11 +1124,11 @@ function $HttpProvider() {
10841124

10851125

10861126
function buildUrl(url, params) {
1087-
if (!params) return url;
1088-
var parts = [];
1089-
forEachSorted(params, function(value, key) {
1090-
if (value === null || isUndefined(value)) return;
1091-
if (!isArray(value)) value = [value];
1127+
if (!params) return url;
1128+
var parts = [];
1129+
forEachSorted(params, function(value, key) {
1130+
if (value === null || isUndefined(value)) return;
1131+
if (!isArray(value)) value = [value];
10921132

10931133
forEach(value, function(v) {
10941134
if (isObject(v)) {

test/ng/httpSpec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,24 @@ describe('$http', function() {
12501250
});
12511251

12521252

1253+
it('should be able to remove a request from the cache', function() {
1254+
doFirstCacheRequest();
1255+
1256+
$http.removeCache({method: 'GET', url: '/url', cache: cache});
1257+
1258+
$httpBackend.expect('GET', '/url').respond();
1259+
$http({method: 'GET', url: '/url', cache: cache});
1260+
});
1261+
1262+
1263+
it('should be able to bypass and overwrite the cache for a request', function() {
1264+
doFirstCacheRequest();
1265+
1266+
$httpBackend.expect('GET', '/url').respond();
1267+
$http({method: 'GET', url: '/url', cache: cache, replaceCache: true});
1268+
});
1269+
1270+
12531271
it('should always call callback asynchronously', function() {
12541272
doFirstCacheRequest();
12551273
$http({method: 'get', url: '/url', cache: cache}).then(callback);

0 commit comments

Comments
 (0)