Skip to content

Commit a6e8464

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 angular#5968
1 parent cbcfaa2 commit a6e8464

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/ng/http.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@ function $HttpProvider() {
338338
* If you set the default cache to `false` then only requests that specify their own custom
339339
* cache object will be cached.
340340
*
341+
* If you need to bypass and overwrite the cache you can set the `replaceCache` property to
342+
* `true` in the request configuration.
343+
*
344+
* If you wish to purge stale data from the cache without making a request you can use the
345+
* {@link ng.$http#methods_removeCache $http.removeCache} method and pass it the configuration
346+
* object that was used to create the request you wish to purge.
347+
* ```js
348+
* $http.removeCache({method: 'GET', url: '/someUrl'})
349+
* ```
350+
*
351+
*
341352
* # Interceptors
342353
*
343354
* Before you start creating interceptors, be sure to understand the
@@ -551,6 +562,8 @@ function $HttpProvider() {
551562
* GET request, otherwise if a cache instance built with
552563
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
553564
* caching.
565+
* - **replaceCache** - `{boolean}` - If true this request will bypas the cache and replace
566+
* any previously cached value.
554567
* - **timeout** – `{number|Promise}` – timeout in milliseconds, or {@link ng.$q promise}
555568
* that should abort the request when resolved.
556569
* - **withCredentials** - `{boolean}` - whether to to set the `withCredentials` flag on the
@@ -871,6 +884,29 @@ function $HttpProvider() {
871884
*/
872885
createShortMethodsWithData('post', 'put');
873886

887+
/**
888+
* @ngdoc method
889+
* @name $http#removeCache
890+
*
891+
* @description
892+
* Removes a request from the cache without generating a new request.
893+
*
894+
* @param {Object} config configuration object (as per the request that created the cache entry)
895+
*/
896+
$http.removeCache = function(config) {
897+
898+
var url = buildUrl(config.url, config.params);
899+
900+
var cache = isObject(config.cache) ? config.cache
901+
: isObject(defaults.cache) ? defaults.cache
902+
: defaultCache;
903+
904+
if (cache) {
905+
cache.remove(url);
906+
}
907+
908+
};
909+
874910
/**
875911
* @ngdoc property
876912
* @name $http#defaults
@@ -937,7 +973,11 @@ function $HttpProvider() {
937973
}
938974

939975
if (cache) {
940-
cachedResp = cache.get(url);
976+
if (config.replaceCache) {
977+
cache.remove(url);
978+
} else {
979+
cachedResp = cache.get(url);
980+
}
941981
if (isDefined(cachedResp)) {
942982
if (cachedResp.then) {
943983
// cached request has already been sent, but there is no response yet

test/ng/httpSpec.js

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

11401140

1141+
it('should be able to clear the cache for a request', function() {
1142+
doFirstCacheRequest();
1143+
1144+
$http.removeCache({method: 'GET', url: '/url', cache: cache});
1145+
1146+
$httpBackend.expect('GET', '/url').respond();
1147+
$http({method: 'GET', url: '/url', cache: cache});
1148+
});
1149+
1150+
1151+
it('should be able to bypass and overwrite the cache for a request', function() {
1152+
doFirstCacheRequest();
1153+
1154+
$httpBackend.expect('GET', '/url').respond();
1155+
$http({method: 'GET', url: '/url', cache: cache, replaceCache: true});
1156+
});
1157+
1158+
11411159
it('should always call callback asynchronously', function() {
11421160
doFirstCacheRequest();
11431161
$http({method: 'get', url: '/url', cache: cache}).then(callback);

0 commit comments

Comments
 (0)