Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { setupCache, serializeQuery } from 'axios-cache-adapter';
- const cache = new LRU(/*...*/);
- export default function({ $axios, store, isDev }) {
- // https://github.com/RasCarlito/axios-cache-adapter/blob/98415a2d872954562c36df31a96b64427bbb2e4f/src/memory.js
- class LruStore {
- constructor() {
- this.state = cache;
- }
- getItem(key) {
- const item = this.state.get(key) || null;
- return item;
- }
- setItem(key, value) {
- if (!value.expires) return;
- return this.state.set(key, value.data, value.expires - Date.now());
- }
- removeItem(key) {
- return this.state.del(key);
- }
- clear() {
- this.state.reset();
- }
- length() {
- return _.size(this.state);
- }
- iterate(fn) {
- return this.state.forEach(fn);
- }
- }
- let axiosCache = setupCache({
- store: new LruStore(),
- readHeaders: true,
- key: req => req.url + (_.last(req.url) == '/' ? '' : '/') + serializeQuery(req),
- exclude: {
- query: false, // не игнорируем query в запросе
- filter(req) {
- return false; // по умолчанию всё пишем в кэш
- },
- },
- });
- const defaults = $axios.defaults;
- defaults.adapter = axiosCache.adapter;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement