AlbinoDrought / cachios

Simple axios cache wrapper using node-cache
MIT License
88 stars 10 forks source link

LRU cache and headers #60

Closed Zyles closed 3 years ago

Zyles commented 3 years ago

Hello again.

I need headers saved in my cache using LRU.

I tried the:

cachios.cache = new LRU({ max: cacheMaxAmount
      , length: function (n, key) { return n * 2 + key.length }
      , dispose: function (key, n) { n.close() }
      , maxAge: cacheMaxAge })

cachios.getCacheIdentifier = function (config) {
      return {
        method: config.method,
        url: config.url,
        params: config.params,
        data: config.data,
        headers: config.headers,
      }
    }

But it did not make a difference and still only saves status and data.

So I was wondering wether LRU cache does not support this?

How can I save headers with LRU cache?

Thanks.

AlbinoDrought commented 3 years ago

Hey,

Try this: https://github.com/AlbinoDrought/cachios#custom-response-copier

cachios.getResponseCopy = function (response) {
  return {
    status: response.status,
    headers: response.headers,
    data: response.data,
  };
};

getResponseCopy is used when modifying a response before storing it in cache. By default it trims responses down to just status and data, which is likely the issue here.

getCacheIdentifier is used when determining the cache key used for a request. The default implementation looks like this:

function defaultCacheIdentifer(config) {
  return {
    method: config.method,
    url: config.url,
    params: config.params,
    data: config.data,
  };
}

Which generally covers most cases. Your above example will change the cache key and send a new request if method, url, query params, body, or headers do not match a request in cache.

Zyles commented 3 years ago

Oh derp. I realized the first function was for the cache key.

Thanks.