restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Does this support caching GET requests?

Open scottcorgan opened this issue 13 years ago • 19 comments

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?

scottcorgan avatar Apr 19 '13 04:04 scottcorgan

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

mgonto avatar Apr 19 '13 04:04 mgonto

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!

scottcorgan avatar Apr 19 '13 04:04 scottcorgan

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

mgonto avatar Apr 19 '13 04:04 mgonto

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!

scottcorgan avatar Apr 19 '13 04:04 scottcorgan

Thanks for making something better than $resource!

No problem man! I'll be waiting for your feedback and future PRs ;)

mgonto avatar Apr 19 '13 04:04 mgonto

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

mgonto avatar May 10 '13 00:05 mgonto

SO BEAUTIFUL! THANK YOU! (yes I was yelling)

scottcorgan avatar May 10 '13 16:05 scottcorgan

Is there a way to cache only specific requests?

cletourneau avatar Sep 10 '13 14:09 cletourneau

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?

adamalbrecht avatar Sep 20 '13 21:09 adamalbrecht

+1 for by request caching

dgageot avatar Jan 31 '14 12:01 dgageot

+1 for specific request caching

ferry77 avatar Feb 16 '14 05:02 ferry77

+100 for specific request caching

cornel-masson avatar Feb 25 '14 13:02 cornel-masson

+1 for specific request caching

ghost avatar Feb 28 '14 03:02 ghost

#441 meet my demands. How about u guys?

ghost avatar Feb 28 '14 03:02 ghost

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 :)

mgonto avatar Feb 28 '14 03:02 mgonto

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 avatar Mar 04 '14 19:03 drmikeh

@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;
    });
};

CWSpear avatar May 28 '14 22:05 CWSpear

Question with: Restangular.setCacheActive(true | false);

Does a successful put or save devaluate the cache, of do we have to do that manually?

ddevries avatar Sep 30 '14 06:09 ddevries

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.

riyazbhanvadia avatar Sep 15 '16 08:09 riyazbhanvadia