通过 pm2 使用 React Apollo 在 Next.js 中的实例之间出现 Apollo 客户端缓存同步问题

问题描述 投票:0回答:1

我在我的应用程序中使用 Next.js 和 React Apollo,并将我的应用程序设置为在具有两个实例的集群模式下与 pm2 一起运行。我面临的问题是,当我从一个用户配置文件切换到另一个用户配置文件时,由于实例之间缺乏缓存同步,前一个配置文件中的帖子仍然会出现。 我这样使用缓存

const client = new ApolloClient({
    link: splitLink,
    cache: new InMemoryCache(),
});

我使用命令

pm2-runtime ecosystem.config.js
启动应用程序。

这是我的ecosystem.config.js 文件:

 module.exports = {
    apps: [
        {
            name: 'web_prod',
            script: 'server.js',
            instances: '2',
            exec_mode: 'cluster',
            watch: false,
            max_memory_restart: '2G',
            autorestart: true,
            env: {
                PORT: 3001,
            },
        },
    ],
};

您能否指导我如何配置 Apollo 客户端缓存以在这两个或更多实例之间无缝工作?我们将非常感谢您的帮助!

reactjs next.js apollo-client pm2
1个回答
0
投票

如果您仅在客户端进行缓存,那么服务器实例的数量并不重要,因为每个实例都是无状态的。如果您在服务器上进行缓存,那么您将需要在服务器之间共享缓存(通常是 Redis)。

您描述了在用户个人资料之间切换时的问题 - 帖子是否与用户相关?换句话说,如果您检查缓存是否发布了用户的属性或查询的属性?在后一种情况下,当您使用

network-only
缓存策略切换用户配置文件时,您将需要重新获取帖子集。

© www.soinside.com 2019 - 2024. All rights reserved.