@@ -338,6 +338,17 @@ function $HttpProvider() {
338
338
* If you set the default cache to `false` then only requests that specify their own custom
339
339
* cache object will be cached.
340
340
*
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
+ *
341
352
* # Interceptors
342
353
*
343
354
* Before you start creating interceptors, be sure to understand the
@@ -551,6 +562,8 @@ function $HttpProvider() {
551
562
* GET request, otherwise if a cache instance built with
552
563
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
553
564
* caching.
565
+ * - **replaceCache** - `{boolean}` - If true this request will bypas the cache and replace
566
+ * any previously cached value.
554
567
* - **timeout** – `{number|Promise}` – timeout in milliseconds, or {@link ng.$q promise}
555
568
* that should abort the request when resolved.
556
569
* - **withCredentials** - `{boolean}` - whether to to set the `withCredentials` flag on the
@@ -872,6 +885,29 @@ function $HttpProvider() {
872
885
*/
873
886
createShortMethodsWithData ( 'post' , 'put' ) ;
874
887
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
+
875
911
/**
876
912
* @ngdoc property
877
913
* @name $http#defaults
@@ -937,7 +973,11 @@ function $HttpProvider() {
937
973
}
938
974
939
975
if ( cache ) {
940
- cachedResp = cache . get ( url ) ;
976
+ if ( config . replaceCache ) {
977
+ cache . remove ( url ) ;
978
+ } else {
979
+ cachedResp = cache . get ( url ) ;
980
+ }
941
981
if ( isDefined ( cachedResp ) ) {
942
982
if ( cachedResp . then ) {
943
983
// cached request has already been sent, but there is no response yet
0 commit comments