This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
$http : ability to clear a request from the cache #5968 #6334
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -425,7 +425,18 @@ function $HttpProvider() { | |
* If you set the default cache to `false` then only requests that specify their own custom | ||
* cache object will be cached. | ||
* | ||
* ## Interceptors | ||
* If you need to bypass and overwrite the cache you can set the `replaceCache` property to | ||
* `true` in the request configuration. | ||
* | ||
* If you wish to purge stale data from the cache without making a request you can use the | ||
* {@link ng.$http#methods_removeCache $http.removeCache} method and pass it the configuration | ||
* object that was used to create the request you wish to purge. | ||
* ```js | ||
* $http.removeCache({method: 'GET', url: '/someUrl'}) | ||
* ``` | ||
* | ||
* | ||
* # Interceptors | ||
* | ||
* Before you start creating interceptors, be sure to understand the | ||
* {@link ng.$q $q and deferred/promise APIs}. | ||
|
@@ -595,6 +606,8 @@ function $HttpProvider() { | |
* GET request, otherwise if a cache instance built with | ||
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for | ||
* caching. | ||
* - **replaceCache** - `{boolean}` - If true this request will bypas the cache and replace | ||
* any previously cached value. | ||
* - **timeout** – `{number|Promise}` – timeout in milliseconds, or {@link ng.$q promise} | ||
* that should abort the request when resolved. | ||
* - **withCredentials** - `{boolean}` - whether to set the `withCredentials` flag on the | ||
|
@@ -926,6 +939,29 @@ function $HttpProvider() { | |
*/ | ||
createShortMethodsWithData('post', 'put', 'patch'); | ||
|
||
/** | ||
* @ngdoc method | ||
* @name $http#removeCache | ||
* | ||
* @description | ||
* Removes a request from the cache without generating a new request. | ||
* | ||
* @param {Object} config configuration object (as per the request that created the cache entry) | ||
*/ | ||
$http.removeCache = function(config) { | ||
|
||
var url = buildUrl(config.url, config.params); | ||
|
||
var cache = isObject(config.cache) ? config.cache | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should be moved into a reusable function. |
||
: isObject(defaults.cache) ? defaults.cache | ||
: defaultCache; | ||
|
||
if (cache) { | ||
cache.remove(url); | ||
} | ||
|
||
}; | ||
|
||
/** | ||
* @ngdoc property | ||
* @name $http#defaults | ||
|
@@ -992,7 +1028,11 @@ function $HttpProvider() { | |
} | ||
|
||
if (cache) { | ||
cachedResp = cache.get(url); | ||
if (config.replaceCache) { | ||
cache.remove(url); | ||
} else { | ||
cachedResp = cache.get(url); | ||
} | ||
if (isDefined(cachedResp)) { | ||
if (isPromiseLike(cachedResp)) { | ||
// cached request has already been sent, but there is no response yet | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: Build
url
only ifcache
exists.