Amir
Amir

Reputation: 720

$resource not leveraging cache parameter / $cacheFactory

Angular: 1.3

My allocation of $resource and $cacheFactory looks like this. Every request made via my defined get method was not cached:

  return $resource(
    this.path,
    null,
    {
      'get': {
        method: 'POST',
        isArray: false,
        cache: $cacheFactory('cacheId', { capacity: 10 });
        headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }
      }
    });

Upvotes: 1

Views: 57

Answers (1)

Amir
Amir

Reputation: 720

From looking at the source, the cache parameter is only used for the http GET or JSONP requests:

  if ((config.cache || defaults.cache) && config.cache !== false &&
      (config.method === 'GET' || config.method === 'JSONP')) {
    cache = isObject(config.cache) ? config.cache
          : isObject(defaults.cache) ? defaults.cache
          : defaultCache;
  }

POST requests are not cached. I was using POST because I needed to specify a long list of items to fetch as a parameter. Without POST I was exceeding the max URL len limit. Caching worked once I switched the request to GET.

FYI, for my use case I either was fetching one item or many items. Therefore I only wanted the cache for the single item fetches.

Upvotes: 1

Related Questions