在Keystone 5中将数据源添加到Apollo配置选项时,缓存问题5

发布于 2025-01-22 07:56:41 字数 728 浏览 2 评论 0原文

当第二次执行对服务器的请求时,即使第一个请求突变了实体,返回的结果始终是缓存的结果。

这是导入每个dataSource的两个实例的dataSources.js文件:

  1. legacyuserapi = new legacyuserapi();
  2. LegacyMailapi = New LegacyMailapi();
import legacyUserApi from './LegacyUserApi';
import legacyMailApi from './LegacyMailApi';

export default () => ({
  legacyUserApi,
  legacyMailApi
});

keystone.js文件中,我导入它:

import dataSources from './dataSources';

const apps = [
  new GraphQLApp({
    apiPath: API_PATH,
    apollo: {
      dataSources,
      introspection: isDev
    }
  })
]

When executing a request to the server for the second time, the result that returns is always the cached one, even if the first request mutated the entity.

here is the dataSources.js file that imports two instances of each class of dataSource:

  1. legacyUserApi = new LegacyUserApi();
  2. legacyMailApi = new LegacyMailApi();
import legacyUserApi from './LegacyUserApi';
import legacyMailApi from './LegacyMailApi';

export default () => ({
  legacyUserApi,
  legacyMailApi
});

and in the keystone.js file I import it:

import dataSources from './dataSources';

const apps = [
  new GraphQLApp({
    apiPath: API_PATH,
    apollo: {
      dataSources,
      introspection: isDev
    }
  })
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野味少女 2025-01-29 07:56:41

Keystone JS使用Apollo Server因此,我已经查看了他们的文档

Apollo服务器为每个传入操作调用此功能。
另外,如图所示,该函数应为每个操作创建每个数据源的新实例

dataSources.js文件应导入而不是实例,以便Apollo Server在每个请求中都会创建一个新实例。

import LegacyUserApi from './LegacyUserApi';
import LegacyMailApi from './LegacyMailApi';

export default () => ({
  legacyUserApi: new LegacyUserApi(),
  legacyMailApi: new LegacyMailApi()
});

Keystone js uses Apollo Server so I've looked at their documentation:

Apollo Server calls this function for every incoming operation.
Also as shown, the function should create a new instance of each data source for each operation.

so dataSources.js file should import the class and not the instance so that Apollo Server will create a new instance with every request.

import LegacyUserApi from './LegacyUserApi';
import LegacyMailApi from './LegacyMailApi';

export default () => ({
  legacyUserApi: new LegacyUserApi(),
  legacyMailApi: new LegacyMailApi()
});
怎会甘心 2025-01-29 07:56:41

可以使用许多方法您的问题并不表示您正在使用的问题。最终,他们中的大多数设置a 控制标题在HTTP响应上,让我们假设在这里就是这样。

该标头通过浏览器和服务器之间的任何共享缓存来解释此标头,例如CDN或代理。缓存后,服务器通常没有任何使存储副本无效的机制。可以通过为最初被缓存的响应设置正确的标头指令来减轻这一点。例如,包括必备指令或设置简短的max-age

另外,设置私有指令将仅允许在浏览器中缓存请求。然后,随后的HTTP请求可以指定max-age = 0,no-cache以强制再次获取数据(例如,在进行突变之后)。这可能使您可以为单个用户解决问题,但是显然,您会失去共享缓存的好处。

在不了解您的缓存配置,基础架构和所提出的请求的情况下,很难提供具体建议。也许您可以使用Curl复制来更新问题?这将使您将服务器行为与浏览器的行为隔离。

There are many ways caching can be used on top of Apollo Server but your question doesn't indicate which you're using. Ultimately, most of them set a Cache-Control header on the HTTP response so let's assume that's the case here.

This header is interpreted by both the browser and any shared caches that sit between it and the server, such as a CDN or proxy. Once a cached, the server generally doesn't have any mechanism to invalidate the stored copy. This can be mitigated by setting the correct header directives for the response that will initially be cached. For example, including the must-revalidate directive or setting a short max-age.

Alternatively, setting the private directive will only allow the request to be cached in the browser. Subsequent HTTP requests could then specify max-age=0, no-cache to force the data to be fetched again (for example, after a mutation was made). This may allow you to solve your problem for individual users but, obviously, you loose the benefits of a shared cache.

It's difficult to give specific advice without knowing more about your caching configuration, infrastructure and the requests being made. Maybe you can update your question with a reproduction using curl? That'll let you isolate the server behaviour from that of the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文