在MongoDB中缓存重复的查询结果

问题描述:

我将构建一个旨在大量"查看的页面,但是更少的用户会写入"数据库.例如,每100个用户中只有1个可以将他的新闻发布到我的网站上,其余的则只会阅读该新闻.

I am going to build a page that is designed to be "viewed" alot, but much fewer users will "write" into the database. For example, only 1 in 100 users may post his news on my site, and the rest will just read the news.

在上述情况下,当他们访问我的主页时,将执行100个相同的查询,而实际的数据库更改很少.实际上,这些查询中有99个浪费了计算机能力.是否有任何方法可以缓存第一个查询的结果,并且当它们在短时间内检测到同一查询时,是否可以提供缓存的结果?

In the above case, 100 SAME QUERIES will be performed when they visit my homepage while the actual database change is little. Actually 99 of those queries are a waste of computer power. Are there any methods that can cache the results of the first query, and when they detect the same query in a short time, can deliver the cached result?

我使用MongoDB和Tornado.但是,有些帖子说MongoDB不进行缓存.

I use MongoDB and Tornado. However, some posts say that the MongoDB does not do caching.

使用Nginx之类的东西制作静态的,缓存的HTML并不是首选,因为我想每次都由Tornado呈现个性化的页面.

Making a static, cached HTML with something like Nginx is not preferred, because I want to render a personalized page by Tornado each time.

Motor(MOngo + TORnado)软件包的作者提供了一个在此处缓存其类别列表的示例:

The author of the Motor (MOngo + TORnado) package gives an example of caching his list of categories here: http://emptysquare.net/blog/refactoring-tornado-code-with-gen-engine/

基本上,他定义了一个全局类别列表,并查询数据库以进行填充;然后,每当他需要页面中的类别时,他都会检查列表:如果存在,则使用该列表;如果不存在,则再次查询并填写该列表.他将其设置为在插入数据库时​​使该列表无效. ,但是根据您的使用情况,您可以创建一个全局超时变量来跟踪何时需要再次查询.如果您正在做复杂的事情,这可能会一发不可收拾,但是如果它只是最新帖子或其他内容的列表,我认为会很好.

Basically, he defines a global list of categories and queries the database to fill it in; then, whenever he need the categories in his pages, he checks the list: if it exists, he uses it, if not, he queries again and fills it in. He has it set up to invalidate the list whenever he inserts to the database, but depending on your usage you could create a global timeout variable to keep track of when you need to re-query next. If you're doing something complicated, this could get out of hand, but if it's just a list of the most recent posts or something, I think it would be fine.