有没有办法告诉流星集合是静态的(永远不会改变)? [英] Is there a way to tell meteor a collection is static (will never change)?
问题描述
在我的流星项目上,用户可以发布事件,并且他们必须(通过自动完成功能)选择它将在哪个城市进行.我有法国城市的完整列表,并且永远不会更新.
On my meteor project users can post events and they have to choose (via an autocomplete) in which city it will take place. I have a full list of french cities and it will never be updated.
我想基于自动完成功能的输入来使用集合和发布-订阅,因为我不希望客户端下载完整的数据库(5MB).有没有一种方法可以使流星告诉该流派静态"?还是没有区别?
I want to use a collection and publish-subscribes based on the input of the autocomplete because I don't want the client to download the full database (5MB). Is there a way, for performance, to tell meteor that this collection is "static"? Or does it make no difference?
有人可以建议一种不同的方法吗?
Could anyone suggest a different approach?
推荐答案
当您想告诉服务器集合是静态的"时,我知道有两种潜在的优化方法:
When you "want to tell the server that a collection is static", I am aware of two potential optimizations:
- 不要使用实时查询来观察数据库,因为数据永远不会改变
- 不要将此查询的结果存储在合并框中,因为它不需要跟踪和与其他数据(节省内存和CPU)相比
- Don't observe the database using a live query because the data will never change
- Don't store the results of this query in the merge box because it doesn't need to be tracked and compared with other data (saving memory and CPU)
(1)是您可以通过构造自己的发布游标而轻松完成的操作.但是,如果有任何客户端正在观察相同的查询,我相信Meteor(至少在将来)将对此进行优化,因此对于任何数量的客户端,它仍然只是一个实时查询.至于(2),我不知道有任何简单的方法可以执行此操作,因为它可能会破坏在多个出版物和订阅.
(1) is something you can do rather easily by constructing your own publish cursor. However, if any client is observing the same query, I believe Meteor will (at least in the future) optimize for that so it's still just one live query for any number of clients. As for (2), I am not aware of any straightforward way to do this because it could potentially mess up the data merging over multiple publications and subscriptions.
为避免使用实时查询,可以将数据手动添加到publish函数中,而不是返回游标,这将导致调用.observe()
函数将数据连接到预订.这是一个简单的示例:
To avoid using a live query, you can manually add data to the publish function instead of returning a cursor, which causes the .observe()
function to be called to hook up data to the subscription. Here's a simple example:
Meteor.publish(function() {
var sub = this;
var args = {}; // what you're find()ing
Foo.find(args).forEach(function(document) {
sub.added("client_collection_name", document._id, document);
});
sub.ready();
});
这将导致数据被添加到客户端的client_collection_name
中,该名称可能与Foo
引用的集合具有相同的名称,或有所不同.请注意,您可以对出版物进行许多其他操作(另请参见上面的链接.)
This will cause the data to be added to client_collection_name
on the client side, which could have the same name as the collection referenced by Foo
, or something different. Be aware that you can do many other things with publications (also, see the link above.)
更新:要解决(2)中的问题(根据集合的大小可能存在很大问题),有必要完全绕开Meteor.请参阅 https://stackoverflow.com/a/21835534/586086 .另一种方法是将集合fetch()
ed作为方法调用返回,尽管这没有压缩的好处.
UPDATE: To resolve issues from (2), which can be potentially very problematic depending on the size of the collection, it's necessary to bypass Meteor altogether. See https://stackoverflow.com/a/21835534/586086 for one way to do it. Another way is to just return the collection fetch()
ed as a method call, although this doesn't have the benefits of compression.
这篇关于有没有办法告诉流星集合是静态的(永远不会改变)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!