Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$http : ability to clear a request from the cache #5968 #6334

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Build url only if cache exists.


var cache = isObject(config.cache) ? config.cache
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,24 @@ describe('$http', function() {
});


it('should be able to remove a request from the cache', function() {
doFirstCacheRequest();

$http.removeCache({method: 'GET', url: '/url', cache: cache});

$httpBackend.expect('GET', '/url').respond();
$http({method: 'GET', url: '/url', cache: cache});
});


it('should be able to bypass and overwrite the cache for a request', function() {
doFirstCacheRequest();

$httpBackend.expect('GET', '/url').respond();
$http({method: 'GET', url: '/url', cache: cache, replaceCache: true});
});


it('should always call callback asynchronously', function() {
doFirstCacheRequest();
$http({method: 'get', url: '/url', cache: cache}).then(callback);
Expand Down