@@ -425,7 +425,18 @@ function $HttpProvider() {
425
425
* If you set the default cache to `false` then only requests that specify their own custom
426
426
* cache object will be cached.
427
427
*
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
429
440
*
430
441
* Before you start creating interceptors, be sure to understand the
431
442
* {@link ng.$q $q and deferred/promise APIs}.
@@ -595,6 +606,8 @@ function $HttpProvider() {
595
606
* GET request, otherwise if a cache instance built with
596
607
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
597
608
* caching.
609
+ * - **replaceCache** - `{boolean}` - If true this request will bypas the cache and replace
610
+ * any previously cached value.
598
611
* - **timeout** – `{number|Promise}` – timeout in milliseconds, or {@link ng.$q promise}
599
612
* that should abort the request when resolved.
600
613
* - **withCredentials** - `{boolean}` - whether to set the `withCredentials` flag on the
@@ -926,6 +939,29 @@ function $HttpProvider() {
926
939
*/
927
940
createShortMethodsWithData ( 'post' , 'put' , 'patch' ) ;
928
941
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
+
929
965
/**
930
966
* @ngdoc property
931
967
* @name $http#defaults
@@ -992,7 +1028,11 @@ function $HttpProvider() {
992
1028
}
993
1029
994
1030
if ( cache ) {
995
- cachedResp = cache . get ( url ) ;
1031
+ if ( config . replaceCache ) {
1032
+ cache . remove ( url ) ;
1033
+ } else {
1034
+ cachedResp = cache . get ( url ) ;
1035
+ }
996
1036
if ( isDefined ( cachedResp ) ) {
997
1037
if ( isPromiseLike ( cachedResp ) ) {
998
1038
// cached request has already been sent, but there is no response yet
@@ -1084,11 +1124,11 @@ function $HttpProvider() {
1084
1124
1085
1125
1086
1126
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 ] ;
1092
1132
1093
1133
forEach ( value , function ( v ) {
1094
1134
if ( isObject ( v ) ) {
0 commit comments