如何在Meteor中缓存数据? [英] How can I cache the data in Meteor?

查看:120
本文介绍了如何在Meteor中缓存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢大家!
最近我想在流星上建立一个小cms,但是有一些疑问

thanks everyone! recently i want to built a small cms on meteor,but have some question

1,缓存,页面缓存,数据缓存等。

1,cache,page cache,data cache,etc..

例如,当人们在服务器端搜索某些文章

For example,when people search some article

时,

 Meteor.publist('articles',function(keyword){
   return Articles.find({keyword:keyword});
});

在客户端中:

Meteor.subscribe('articles',keyword);

可以,但是......
的问题是,每当人们在做因此,它会调用mongo查询,并降低性能,在其他框架中的
使用常见的http或https,人们可以依靠鱿鱼或清漆之类的东西来缓存页面或数据,因此每次您路由到url时,您从缓存服务器读取数据,但是Meteor构建在socket.js或websocket上,我不知道如何在套接字中缓存....我整理了清漆,但没有效果。
那么,可能会忽略websocket吗?是否有某种方法可以缓存数据,在mongodb中,在服务器中,我可以添加一些缓存服务器吗?

that's ok ,but ...... the question is ,everytime people doing so ,it invoke a mongo query,and reduce the performance, in other framework use common http or https,people can depend on something like squid or varnish to cache the page or data,so everytime you route to a url,you read data from the cache server ,but Meteor built on socket.js or websocket,and I don't know how to cache throught the socket.......I trid varnish ,but seen no effect. so,may be it ignore the websocket?is there some method to cache the data,in the mongodb,in server,can i add some cache server ?

2,聊天

我在 https:中看到了聊天室示例: //github.com/zquestz/simplechat
但是与使用socket.js的隐式不同,此示例将聊天消息保存在mongodb中,因此数据流为message-> mongo-> query-> people ,这也会调用mongo查询!
和socket.js中,只需将套接字保存在上下文(或服务器端缓存)中,这样数据就不会进入数据库。
我的问题是,Meteor中是否有一个套接字接口,所以我可以message-> socket-> people?并且如果不能,那么作为聊天室示例的生产环境中的性能如何(我看它运行的很慢...)

I see the chatroom example in https://github.com/zquestz/simplechat But unlike implyment using socket.js,this example save the chat message in the mongodb ,so the data flow is message ->mongo->query->people,this invoke the mongo query too! and in socket.js,just save the socket in the context(or the server side cache),so the data don't go throught the db. My question is , is there a socket interface in Meteor ,so I can message->socket->people? and if can't , how is the performace in the productive envirment as the chatroom example doing(i see it runs slow ...)

推荐答案

使用Meteor,您不必担心缓存Mongodb查询。流星为您做到了。根据有关数据和安全性的文档

With Meteor, you don't have to worry about caching Mongodb queries. Meteor does that for you. Per the docs on data and security:


每个Meteor客户端都包含一个内存数据库缓存。为了管理客户端缓存,服务器发布JSON文档集,然后客户端订阅这些集。当集合中的文档发生更改时,服务器会修补每个客户端的缓存。

Every Meteor client includes an in-memory database cache. To manage the client cache, the server publishes sets of JSON documents, and the client subscribes to those sets. As documents in a set change, the server patches each client's cache.

[...]

订阅后,客户端会将其缓存用作快速的本地数据库,大大简化了客户端代码。读取从不需要到服务器的昂贵往返。而且它们仅限于缓存的内容:对客户端集合中每个文档的查询只会返回服务器正在发布到该客户端的文档。

Once subscribed, the client uses its cache as a fast local database, dramatically simplifying client code. Reads never require a costly round trip to the server. And they're limited to the contents of the cache: a query for every document in a collection on a client will only return documents the server is publishing to that client.

由于Meteor确实经常轮询服务器以查看客户端的缓存是否需要打补丁,因此您可能会不时看到这些轮询。但它们可能不是很大的要求。此外,由于Meteor的一项功能(称为延迟补偿),当您更新数据源时,客户端会立即反映出更改,而无需先等待服务器。

Because Meteor does poll the server every so often to see if the client's cache needs patching, you're probably seeing those polls happening every now and then. But they probably aren't very large requests. Additionally, due to a feature of Meteor called latency compensation, when you update a data source, the client immediately reflects the change without first waiting on the server. This reduces the appearance of performance reduction to the user.

如果您在mongo中有很多文档,那么如果您仍然启用了autopublish包,您可能还会看到它们全部被获取。您可以使用 meteor remove autopublish 删除它并编写代码以仅发布相关数据而不是整个数据库来解决此问题。

If you have many documents in mongo, you may also be seeing them all get fetched if you still have the autopublish package enabled. You can fix that by removing it with meteor remove autopublish and write code to only publish the relevant data instead of the entire database.

如果您真的需要手动管理缓存,文档还可以介绍以下内容:

If you really need to manage caching manually, the docs also go into that:


复杂的客户端可以打开和关闭订阅以控制保留多少数据。缓存并管理网络流量。禁用订阅后,所有其他文档都将从缓存中删除,除非另一个活动的订阅也提供了相同的文档。

Sophisticated clients can turn subscriptions on and off to control how much data is kept in the cache and manage network traffic. When a subscription is turned off, all its documents are removed from the cache unless the same document is also provided by another active subscription.

对其他性能的改进目前正在研究流星,包括支持非常多的客户端的DDP级代理。您可以在流星路线图上看到更多详细信息。

Additional performance improvements to Meteor are currently being worked on, including a DDP-level proxy to support "very large number of clients". You can see more detail on this at the Meteor roadmap.

这篇关于如何在Meteor中缓存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆