1

As per the AngularJS documentation, $resource service now has support for caching as well.

cache – {boolean|Cache} – If true, a default $http cache will be used to cache the GET request, otherwise if a cache instance built with $cacheFactory, this cache will be used for caching.

So I may write $resource as follows:

app.factory('myResourceWithCache', function($resource, $cacheFactory) {
    var myCache = $cacheFactory('MyCache');
    return $resource(apiBaseUrl + '/myservice/:id', {id: '@id'}, {
        'get': { method:'GET', cache: myCache },
        'query': { method:'GET', cache: myCache , isArray:true }
    });
});

What I want to know is that:

  1. what will be the name of key in the cache? For $http cache, key is the path of service/api, is it going to be the same if we use custom cache?
  2. Is there a way to provide key name on our own for the custom cache (myCache here)

I tried AngularJS documentation and other stackoverflow threads but did not find any answer for this. Please help.

||||||
  • What is your use case for needing to access that cache directly yourself? whenever you need the data again it will be returned by $resource from cache – charlietfl Jan 12 '16 at 19:45
  • Hi charlietfl, Use case is as follows: In our application we get some data from Database via WebAPI, that data is of user preference which is not going to change very often but user can change it. As it is not going to be changed very often so we thought to cache it in Angular. But when user changes it we need to send the changes to Db to persist and remove the cache so that any next call will again get the data from Db and populate the cache. I need to give my own key value for want of better key name rather than URL of API. – alencdave Jan 13 '16 at 15:25
  • so isn't it already in $cacheFactory('MyCache') ? – charlietfl Jan 13 '16 at 15:28
  • When I put any value in MyCache we do it via MyCache.Put(key, value). When we want to access the value put in Cache we do it like MyCache.get(key). What I need to know if is there anyway to specify my own key name while using Cache with $resource as mentioned in the question above. – alencdave Jan 14 '16 at 15:13
0

I'm not sure if you still need answer for this one year old question.

Angular Documentation $http - Caching section doesn't mention about custom key

and from the $http library, either custom or default cache will be using this line to "put" cache which is the request url.

so answer for your questions:

  1. Request URL. Yes, it's same for custome cache
  2. No (at least until now)
||||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.