#[apollo-cache-persist] 清除缓存数据 | apollo-cache-persist 错误 | apollo-cache-persist 不工作

问题描述

这是我使用 'apollo3-cache-persist' 用于缓存持久性的代码,似乎在初始缓存后自动清除了缓存数据。清除会导致用于持久性的存储中的所有内容都被清除。因此导致不持久。

import { persistCache,LocalStorageWrapper,LocalForageWrapper } from 
'apollo3-cache-persist';

const httpLink = createHttpLink({
uri: 'http://localhost:4000/'
});

const cache = new InMemoryCache();

persistCache({
  cache,storage: new LocalStorageWrapper(window.localStorage),debug: true,})
 .then(() => {

  const client = new ApolloClient({
    link: httpLink,cache,connectToDevTools: true
  });

  ReactDOM.render(
  <ApolloProvider client={client}>
      <browserRouter>
        <App />
      </browserRouter>
  </ApolloProvider>,document.getElementById('root')
  );
})

解决方法

使用“apollo3-cache-persist”时要持久化的最大缓存大小(以字节为单位)默认为 1048576 (1 MB)。即如果超过,则持久性将暂停,应用程序将在下次启动时冷启动。

  • maxSize?: number |假的,

因此对于无限缓存大小,请提供 false。

persistCache({
  cache,storage: new LocalStorageWrapper(window.localStorage),debug: true,maxSize: false
})
  .then(() => { 
  ...
})