swenedo
swenedo

Reputation: 3114

Add data to angular $http cache

When using angular $http, you can set the request configuration cache property to true and the response will be stored. Next time the same request is made, the response is served from the cache without sending a request to the server (docs).

How do you add a (key,val) pair to this cache without performing the http request?

I want to do something like

cache.put("/api/country/uk", someData)

and when $http.get("/api/country/uk") is called later, the cached data is used instead of making the http request.

Upvotes: 1

Views: 128

Answers (1)

swenedo
swenedo

Reputation: 3114

I found the solution here. Will keep the question + answer if someone else needs it.

You can get the cache by

var cache = $cacheFactory.get('$http');

and then you can put data to it like this

cache.put("/api/country/uk", someData)

When you call

$http.get("/api/country/uk", {cache:true})

you will get the cached response.

Upvotes: 2

Related Questions