1

My angular app only needs to fetch data from the server once on app load, and then only communicate with the server to over-write data. Is there a recommended way to implement this?

So far I'm using $resource.query to fetch the initial data set, storing the resource in a service, and am thinking that now I need to overwrite the GET action, but am unsure how to go about suppressing its communication with the server.

I also have another web service called getLatest which does a lot of server side work to get additional records which I then need to append to my cached resource. How can I go about doing this? I've thought about manually creating Resource instances somehow from the retrieved data, or possibly firing a new (uncached) resource query after the getLatest call returns (though this seems inefficient to me)

2 Answers 2

2

@tschiela is right that you can use cache property on the resource to cache resource. But you should understand cache invalidation strategies.

  • When should the cache be refereshed
  • What happens when server data gets updated.
  • Can you live with inconsistency in data for anything that gets cached.
  • I am not sure but does the standard implementation $http service cache honor HTTP expiration header (if there are any).

Overall sever based HTTP expiration headers are best way to provide cache as there is more control on the server and most browsers honor in for GET request.

Sign up to request clarification or add additional context in comments.

1 Comment

Good point, although for my current needs (an app only to be used by me) data consistency shouldn't be a problem
1

You can use caching with cache:true

.factory('Data', function($resource){
      return $resource('http://some.url.of.api', {}, {
        'get' : { method:'GET', cache: true }
      });
    })

Or you can implement your own cache factory. Read more about in in the AngularJS documentation: $resource

1 Comment

Good answer, although I edited the question shortly after posting. Are you able to help with the second part of the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.