Skip to content

Commit 95836a4

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 7c34e1f commit 95836a4

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
@@ -872,6 +885,29 @@ function $HttpProvider() {
872885
*/
873886
createShortMethodsWithData('post', 'put');
874887

888+
/**
889+
* @ngdoc method
890+
* @name $http#removeCache
891+
*
892+
* @description
893+
* Removes a request from the cache without generating a new request.
894+
*
895+
* @param {Object} config configuration object (as per the request that created the cache entry)
896+
*/
897+
$http.removeCache = function(config) {
898+
899+
var url = buildUrl(config.url, config.params);
900+
901+
var cache = isObject(config.cache) ? config.cache
902+
: isObject(defaults.cache) ? defaults.cache
903+
: defaultCache;
904+
905+
if (cache) {
906+
cache.remove(url);
907+
}
908+
909+
};
910+
875911
/**
876912
* @ngdoc property
877913
* @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
@@ -1157,6 +1157,24 @@ describe('$http', function() {
11571157
});
11581158

11591159

1160+
it('should be able to remove a request from the cache', function() {
1161+
doFirstCacheRequest();
1162+
1163+
$http.removeCache({method: 'GET', url: '/url', cache: cache});
1164+
1165+
$httpBackend.expect('GET', '/url').respond();
1166+
$http({method: 'GET', url: '/url', cache: cache});
1167+
});
1168+
1169+
1170+
it('should be able to bypass and overwrite the cache for a request', function() {
1171+
doFirstCacheRequest();
1172+
1173+
$httpBackend.expect('GET', '/url').respond();
1174+
$http({method: 'GET', url: '/url', cache: cache, replaceCache: true});
1175+
});
1176+
1177+
11601178
it('should always call callback asynchronously', function() {
11611179
doFirstCacheRequest();
11621180
$http({method: 'get', url: '/url', cache: cache}).then(callback);

0 commit comments

Comments
 (0)