Does this support caching GET requests?
Curious if this is meant to only handle the actually http requests, or if it handles caching.
If not, is there a way to hook into the http requests to intercept and write a caching plugin for it?
Hey,
Restangular doesn't provide cache by itself.
However, Restangular uses $http "inside" and since a commit from 2 months ago, you can actually tell $http to cache stuff.
Check this commit: https://github.com/angular/angular.js/commit/99f3b70b2d316f5bb39e21249e752c29f49c90ab
So, from version 1.1.4 and up, you can actually configure cache globally:
// Option 1, inyect $http somewhere and do
$http.defaults.cache = myCacheFactory
// Option 2
app.config(function($httpProvider) {
$httpProvider.defaults.cache = myCacheFactory;
});
I was actually thinking in implementing cache into Restangular, but as I saw that from 1.1.4 you can actually have your own cache by just configuring $http I didn't do it yet.
Do you think that this will suit you? If it does, please close the issue.
Otherwise, I can implement the following inside Restangular and you'd just have to do something like Restangular.setCacheActive(true | false)
http://stackoverflow.com/a/16080092/385982
Fantastic answer, thanks. I'm not sure which would be better, but I am a fan of only doing one thing, and doing it well. BUT I don't think it would hurt to add the option to the service.
I plan on using this alongside angular-collection to create a robust api service. Thanks for the heads up!
Hey,
That's awesome. I try to rely the most I can in Angular's already built in stuff in $http. What I add is a nice interface over $resource which in my opinion is very flawed. So I think that the best option is to uses $http built in cache :)
Regarding angular-collection, I've taken a look at it, but I liked more using Underscore or Lodash. That's why they're actually the only dependency of Restangular. With Lodash you can do the same you can with angular-collection but without that $collection.instance() in the middle:
// Lodash code
_(myCollection)
.filter(function(elem) {return elem.age > 5})
.sortBy(function(elem) {return elem.age})
.value()
The good thing about this is that this "wraps" the collection only for a moment and then returns you the an "unwrapped" collection without any other magic. Also, Lodash or Underscore are much more optimized and work in all browsers :).
Anyway, tell me what you think about Restangular after using it :). Of course I like it, but I'm kinda biassed :P
Wow, thanks for the heads up. Perhaps I'll run with that! I love lodash and use it for all my functional needs.
I'll let you know in the next few days how it works for me.
Thanks for making something better than $resource!
Thanks for making something better than $resource!
No problem man! I'll be waiting for your feedback and future PRs ;)
As implementation on Angular's side wasn't really cool about caching (Tried out today), I've implemented it on Restangular.
In order to cache, you can do this:
https://github.com/mgonto/restangular#can-i-cache-requests
SO BEAUTIFUL! THANK YOU! (yes I was yelling)
Is there a way to cache only specific requests?
I was wondering this too - It would be nice to be able to pass in an argument to get() and getList() to only use caching for the current request. Is there a way to do this or something like it?
+1 for by request caching
+1 for specific request caching
+100 for specific request caching
+1 for specific request caching
#441 meet my demands. How about u guys?
Hey everytone,
I see this has a lot of attention. As @chao787 is saying, does #441 help for you? Using withHttpConfig to cache that particular request? I don't think it's 100% practical, but maybe it suits you.
Otherwise, what DSL or API would you like / recommend?
Reopenning :)
I discovered this approach:
myApp.factory('MyRestService', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setDefaultHttpFields({cache: false});
});
});
myApp.factory('MyCachingRestService', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setDefaultHttpFields({cache: true});
});
});
Then I can use it like this:
MyRestService.all("accounts").getList(); // will not cache
MyCachingRestService.all("users").getList(); // will cache
@drmikeh's pattern doesn't help when you aren't working with the base Restangular object. I have a collection that I don't want to cache (most of it), but I do want to cache part of it:
$scope.toggleAccordion = function (connection) {
if (!connection.accordionOpen) return;
// I want to cache this response, but not the one that got me connection
connection.getList('friends').then(function (friends) {
connection.friends = friends;
});
};
Question with: Restangular.setCacheActive(true | false);
Does a successful put or save devaluate the cache, of do we have to do that manually?
Hi,
I want to cache particular request but it should get call once its parameter gets update.
I am using restangular cache is working for me but once my parameter change request does not get call.