0

I've enabled cache in my $http request and now the callback for success only runs the first time. I'm wondering if this is expected or there's something I need to know about caching $http ?

This is what I've been trying:

$http.get('/foo/bar', { cache: true })
.success(function(data){
  // does foo
})
.error(function(){
  // uh oh
});

Let's say I've processed the data once, but there's also other commands that I'd like to run, every time. So, if it's true the data is cached or already available I don't want to repeat myself, but let's say, If I'm opening and closing elements with animations, where should this go ?!

Thanks for looking!

punkbit
  • 7,347
  • 10
  • 55
  • 89

2 Answers2

2

You can use the $cacheFactory object. See : http://docs.angularjs.org/api/ng.$cacheFactory

You can cache $http request like that :

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

If you want to retrieve a specific url in cache do :

var cachedData = $httpDefaultCache.get('http://myserver.com/foo/bar/123');

$You can clear the cache too :

$httpDefaultCache.remove('http://myserver.com/foo/bar/123');

or :

$httpDefaultCache.removeAll();

Complete post here : http://pseudobry.com/power-up-%24http.html

iConnor
  • 19,997
  • 14
  • 62
  • 97
Thomas Pons
  • 7,709
  • 3
  • 37
  • 55
  • thanks for looking, I've read that article in coderwall a few moments ago. What I need to understand is, if I should move my commands that I have in the callback. I treat the data, but then I start a given "animation". So, I want to run that animation every time the data is requested and ready. If that makes sense ? – punkbit Sep 23 '13 at 16:07
  • If i understand you want to notify your "directive" or run your animation from your service when the data chenges ? – Thomas Pons Sep 23 '13 at 16:12
  • Thomas Pons, thanks a lot! Actually you just answered my question correctly. I know check if the data is cached already or not and from there I know what to do. Thanks a lot! – punkbit Sep 23 '13 at 16:18
1

I'm pretty sure that the new GET request isn't running so no corresponding success callbacks are called.

scartag
  • 17,548
  • 3
  • 48
  • 52
  • thanks for looking and replying! So, it means that whenever I enable cache in $http, the new GET will never run ? Whatever, I have to do, shouldn't be expected to run inside the success callback, right ? – punkbit Sep 23 '13 at 16:03
  • Yeah i guess so. it should happen elsewhere thats guaranteed to always run. – scartag Sep 23 '13 at 16:04
  • Ok thanks alot scartag! I'll test for the data availability and run the other commands elsewhere! – punkbit Sep 23 '13 at 16:08
  • The $http GET does happen, it just doesn't go to fetch it from the server, so the success will trigger event if it's cached. – Adrian Neatu Jan 16 '14 at 14:57