Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query AngularJS's $cacheFactory values

I've correctly set Restangular to cache my http requests via:

Restangular.withConfig(function(RestangularConfigurer) {
    RestangularConfigurer.setDefaultHttpFields({cache: true});
});

I would like, however, to be able to manually certain cached elements at a given point in time, eg when they become obsolete due to the user modifying these objects. Is there a way to do this? I've tried:

$cacheFactory.get('$http').info()
Object {id: "$http", size: 7}
like image 393
rebelliard Avatar asked Jul 12 '26 20:07

rebelliard


1 Answers

Just make use of the $cacheFactory API as documented here:

  • https://docs.angularjs.org/api/ng/service/$cacheFactory

For instance, to invalidate a given cache entry:

$cacheFactory.get('$http').remove(myGetUrlToInvalidate);

myGetUrlToInvalidate is a string representation of your GET request URL. You may have to test to find out whether its the relative or absolute URL, check out following stackoverflow discussion for more details:

  • How to refresh / invalidate $resource cache in AngularJS

Also of interest this stackoverflow discussion pointing to an alternate cache implementation:

  • Is there a way to get all of the keys out of a cache in Angular, specifically the template cache?

Restangular resources:

  • https://github.com/mgonto/restangular#can-i-cache-requests
  • https://github.com/mgonto/restangular#setdefaulthttpfields
like image 67
Patrick Refondini Avatar answered Jul 14 '26 11:07

Patrick Refondini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!