dimitar
dimitar

Reputation: 63

clear cache of angular $resource

In my use case I have Carts and LineItems. My REST service has the following resource urls:

get|post|delete|put api/v1/carts/:cartId

get|post|delete|put api/v1/carts/:cartId/lineItems/:lineItemId

get|post|delete|put api/v1/lineItems/:lineItemId

the probelm is that when I do:

delete api/v1/carts/Default/lineItem/:lineItemId

and then I do:

get api/v1/carts/Default 

angular does not hit the server to get the cart, but it gets it from cache which returns the deleted line item back in the cart I tried all sorts of trick, tried disabling the cache by adding the {cache: false} option, but could not get it to work the only way I was able to get it to work was if I make a POST request to

api/v1/carts/Default

with an empty body, which tells angular that the resource has changed and clears the cache. Even though it works it seams like a hacky solution, so I was wandering if someone might have a suggestion of what I am doing wrong.

Upvotes: 2

Views: 840

Answers (1)

Petr Averyanov
Petr Averyanov

Reputation: 9476

Get requests are cached by browsers. Simplest way is to use timestamp: instead of GET /user/100 use GET /user/100?time=120229393 With such request you will always hit server.

If you need this in multiple requests, you can make http interceptor to add timestamp param to request.

Upvotes: 3

Related Questions