简体   繁体   English

在NodeJS中存储很少修改的数据库值的最佳实践是什么?

[英]What is the best practice for storing rarely modified database values in NodeJS?

I've got a node app that works with Salesforce for a few different things. 我有一个可以与Salesforce一起使用的节点应用程序,可以处理一些不同的事情。 One of the features is letting users fill in a form and pushing it to Salesforce. 其中一项功能是让用户填写表格并将其推送到Salesforce。

The form has a dropdown list, so I query salesforce to get the list of available dropdown items and make them available to my form via res.locals. 该表单具有一个下拉列表,因此我查询salesforce以获取可用下拉列表,并通过res.locals将其提供给我的表单。 Currently I'm getting these values via some middleware, storing them in the users session, and then checking if the session value is set, use them, if not, query salesforce and pull them in. 目前,我是通过一些中间件获取这些值的,将它们存储在用户会话中,然后检查是否设置了会话值,如果没有使用,请使用它们,查询salesforce并将其引入。

This works, but it means every users session data in Mongo holds a whole bunch of picklist vals (they are the same for all users). 这行得通,但是这意味着Mongo中的每个用户会话数据都拥有一大堆选择列表值(它们对于所有用户而言都是相同的)。 I very rarely make changes to the values on the Salesforce side of things, so I'm wondering if there is a "proper" way of storing these vals in my app? 我很少在Salesforce方面更改值,所以我想知道是否存在“正确”的方法来将这些值存储在我的应用程序中?

I could pull them into a Mongo collection, and trigger a manual refresh of them whenever they change. 我可以将它们放入Mongo集合中,并在它们更改时触发它们的手动刷新。 I could expire them in Mongo (but realistically if they do need to change, it's because someone needs to access the new values immediately), so not sure that makes the most sense... 我可以在Mongo中使它们过期(但实际上,如果确实需要更改它们,这是因为有人需要立即访问新值),因此不确定这是否最有意义...

Is storing them in everyone's session the best way to tackle this, or is there something else I should be doing? 将它们存储在每个人的会话中是解决此问题的最佳方法,还是我应该做些其他事情?

To answer your question quickly, you could add them to a singleton object (instead of session data, which is per user). 为了快速回答您的问题,您可以将它们添加到单个对象中(而不是会话数据(按用户))。 But not sure how you will manage their lifetime (ie pull them again when they change). 但不确定如何管理它们的寿命(即,在它们改变时再次拉动它们)。 A singleton can be implemented using a simple script file that can be required which returns a simple object... 可以使用一个简单的脚本文件来实现单例,该脚本文件需要返回一个简单的对象...

But if I was to do something like this, I would go about doing it differently: 但是,如果我要做这样的事情,我会做不同的事情:

  1. I would create an API endpoint that returns your list data (possibly giving it a query parameters to return different lists). 我将创建一个API端点来返回您的列表数据(可能给它一个查询参数以返回不同的列表)。

If you can afford the data being outdated for a short period of time then, you can write your API so that it returns the response cached (http cache, for a short period of time) 如果您可以承受短时间内过时的数据,则可以编写您的API,以便它返回已缓存的响应(http缓存,短时间)

  1. If your data has to be realtime fresh, then your API should return an eTag in the response of the API. 如果您的数据必须实时更新,则您的API应该在API的响应中返回eTag。 The eTag header basically acts like a checksum for your data, a good checksum would be "last updated date" of all the records in a collection. eTag标头基本上就像是数据的校验和,一个好的校验和应该是集合中所有记录的“最后更新日期”。 Upon receiving a request you check if you have the header "if-none-match" which would contain the checksum, at this point, you do a "lite" call to your database to just pull the checksum, if it matches then you return 304 http code (not modified), otherwise you actually pull the full data you need and return it (alongside the new checksum in the response eTag). 收到请求后,您检查是否具有包含校验和的标头“ if-none-match”,此时,您对数据库执行“精简”调用以仅提取校验和,如果匹配则返回304 http代码(未修改),否则,您实际上会提取所需的全部数据并返回它(与响应eTag中的新校验和一起)。 Basically you are letting your browser do the caching... 基本上,您是让浏览器进行缓存...

Note that you can also combine caching in points 1 and 2 and use them together. 请注意,您还可以在第1点和第2点中组合缓存并一起使用。

More resources on this here: https://devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers https://developers.facebook.com/docs/marketing-api/etags 此处的更多资源: https : //devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers https://developers.facebook.com/docs/marketing-api/etags

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

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