有没有办法告诉流星一个集合是静态的(永远不会改变)? [英] Is there a way to tell meteor a collection is static (will never change)?

查看:11
本文介绍了有没有办法告诉流星一个集合是静态的(永远不会改变)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的流星项目中,用户可以发布事件,他们必须选择(通过自动完成)将在哪个城市进行.我有一个完整的法国城市列表,永远不会更新.

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:

  1. 不要使用实时查询观察数据库,因为数据永远不会改变
  2. 不要将此查询的结果存储在合并框中,因为它不需要被跟踪和与其他数据相比(节省内存和 CPU)
  1. Don't observe the database using a live query because the data will never change
  2. 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.

为避免使用实时查询,您可以手动将数据添加到发布函数而不是返回游标,这会导致调用 .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() 作为方法调用返回,尽管这没有压缩的好处.

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屋!

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