简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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