Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate All Cache #76

Open
ivanfuzuli opened this issue Jun 23, 2020 · 3 comments
Open

Invalidate All Cache #76

ivanfuzuli opened this issue Jun 23, 2020 · 3 comments

Comments

@ivanfuzuli
Copy link

Is there any way for resetting all cached items? For example when user logout, i wanna reset cache.

@ivanfuzuli ivanfuzuli changed the title Reset All Cache Invalidate All Cache Jun 23, 2020
@Helaers
Copy link

Helaers commented Jul 27, 2020

You can do this by clearing the cache manually. Here's an example:

import LRUCache from 'lru-cache';
import api from '@/shared/services/apiService';

class ResourceService {
  private _resourceCache: LRUCache<string, string>;

  constructor() {
    this._resourceCache = new LRUCache();
  }

  public async getSomeResource(): Promise<any> {
    const result = await api.get('/some-endpoint', { cache: this._resourceCache });
    return result.data;
  }

  public resetCache(): void {
    this._resourceCache.reset();
  }
}

export default new ResourceService();

Then when you logout, you just call

resourceService.resetCache();

and you can start with a clean slate

@crawazaky
Copy link

crawazaky commented Jul 29, 2020

Thanks @Helaers

But I have been following axis-extensions github's doc and cannot understand where to use the reset() function when using cacheAdapterEnhancer. There is nothing about invalidating the cache in axios-extensions's doc or can you point me to a additional doc I didn't see ?

I'm using axis & axis-extensions with VuejS. I create my axios' instance like that:

      const axios = ax.create({
        baseURL: API_URL,
        headers: { 'Cache-Control': 'no-cache' },
        adapter: cacheAdapterEnhancer(ax.defaults.adapter),
        crossdomain: true
      });

I'd like to be able to do something like axios.cache.reset()
Is there something I am missing ?

Thanks a lot

@Helaers
Copy link

Helaers commented Jul 29, 2020

@crawazaky If you want to use the approach above you'll probably have to disable the cache by default...

adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false }),

Then define your own cache with via

const myCache = new LRUCache();

Add that to you endpoint:

await axios.get('/some-endpoint', { cache: myCache});

And when you want to clear the cache you created, call the reset function on myCache:

myCache.reset();

I hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants