简体   繁体   English

为什么要在服务器上缓存 graphql 字段。 (阿波罗服务器)

[英]Why to cache graphql fields on server. ( apollo-server )

example(from apollo-server):示例(来自阿波罗服务器):

type Post {
  id: ID!
  title: String
  author: Author
  votes: Int @cacheControl(maxAge: 30)
  comments: [Comment]
  readByCurrentUser: Boolean! @cacheControl(maxAge: 10, scope: PRIVATE)
}

I can't understand where this can be useful, can you provide some examples please.我不明白这在哪里有用,请您提供一些示例。

Well imagine the case that when the value of a field requires some time to compute and the value of such field is seldom changed (eg number of stocks in Nasdaq), caching that field will improve the query performance involving that field because the time to get its value from the cache is much faster than the time is needed to compute its value.想象一下,当一个字段的值需要一些时间来计算并且该字段的值很少改变(例如纳斯达克的股票数量)时,缓存该字段将提高涉及该字段的查询性能,因为获取时间它从缓存中的值比计算它的值所需的时间快得多。

So will an user cannot get the latest actual value for that field?那么用户是否无法获得该字段的最新实际值? Yes.是的。 But if the possibility is very low and the business can tolerate it even he get the outdated value, there is not much issue, just a trade-off between performance and strong consistency.但是如果可能性很低,即使他得到过时的值,业务也可以容忍,那么问题不大,只是性能和强一致性之间的权衡。

Also, if you cannot tolerate user get an outdated value, you can consider to invalidate the related cache whenever some request updates the value of that field.此外,如果您不能容忍用户获取过时的值,则可以考虑在某些请求更新该字段的值时使相关缓存无效。 (eg on an IPO listing day) (例如在 IPO 上市日)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM