kuitos / axios-extensions

🍱 axios extensions lib, including throttle, cache, retry features etc...
MIT License
831 stars 49 forks source link

How to custom cache for each APIs? #56

Closed nghiepdev closed 5 years ago

nghiepdev commented 5 years ago

Hi guys,

Thanks for this package?

But, I have an issue with the custom cache.

Sometimes, I want to custom cache for each APIs value different.

Ex: GET /users have cache options: {maxAge: FIVE_MINUTES, max: 100} but GET /posts have cache options {maxAge: TEN_MINUTES}

Thank you in advance.

kuitos commented 5 years ago

you can define a custom cache by yourself

import { Cache } from 'axios-extensions';

const cache = new Cache({maxAge: FIVE_MINUTES, max: 100})
http.get('/users', { cache: cacheA });

see https://github.com/kuitos/axios-extensions#more-advanced

piotrku commented 2 years ago

Does it work in conjunction with cacheFlag? Local { cache: cacheA } configuration seems to be totally ignored in my case and cacheFlag setting, always has the priority - turning off the caching or turning it on but with its own timings, not the local ones that I'm trying to set for some requests.

my cache config looks like this:

import { cacheAdapterEnhancer, throttleAdapterEnhancer } from 'axios-extensions';
import LRUCache from 'lru-cache';

const FIVE_MINUTES = 1000 * 60 * 5;
const MAX_ITEMS_CACHED = 250;

const defaultCache = new LRUCache({
  maxAge: FIVE_MINUTES,
  max: MAX_ITEMS_CACHED,
});

export default ({ $axios, app, env, store }, inject) => {
  inject('cache', {
    name: 'cache',
    init() {
      const defaults = $axios.defaults;

      defaults.adapter = throttleAdapterEnhancer(
        cacheAdapterEnhancer(defaults.adapter, {
          enabledByDefault: false,
          cacheFlag: 'useCache',
          defaultCache,
        }),
      );
    },
    reset() {
      defaultCache.reset();
    },
  });
};