0

我有一个大型可搜索数据集。在呈现页面之前发送整个数据集的速度令人无法接受,因此我们目前只是发送包含搜索的部分,因此

if ( Meteor.isClient ) {
  Meteor.subscribe('events', Session.get('searchQuery'));
}

if ( Meteor.isServer ) {
  Meteor.publish('events', function(searchQuery) {
    return Events.find(searchQuery);
  }
}

假设我在 1 月份有 10,000 个事件,在 2 月份有 5,000 个事件。

//forgive the psuedo code
Session.set('searchQuery', { startDate:  "2015-01-01", endDate: "2015-02-28"});
//wait a bit...
Event.find().count() // => 15,000, as expected.

//now I want to look at just February events
Session.set('searchQuery', { startDate:  "2015-02-01", endDate: "2015-02-28"});
Event.find().count() // => 5,000, I've lost the January events.

// now I want to look at *just* the January events
// (I previously fetched these but subsequently lost them)
Session.set('searchQuery', { startDate:  "2015-01-01", endDate: "2015-01-31"});
Event.find().count() // => 10,000, it takes a while to get all 10,000 records again.

我想要的是只需要发布一次这些记录,并且由此产生的客户端集合是所有先前结果的联合。

Session.set('searchQuery', { startDate:  "2015-01-01", endDate: "2015-02-28"});
//wait a bit...
Event.find().count() // => 15,000, as expected.

//now I want to look at just February events
Session.set('searchQuery', { startDate:  "2015-02-01", endDate: "2015-02-28"});
Event.find().count() // => 15,000, I've retained the January events.
Event.find({ startDate:  "2015-02-01", endDate: "2015-02-28"}).count() // => 5,000

// now I want to look at *just* the January events
// (I previously fetched these but subsequently lost them)
Session.set('searchQuery', { startDate:  "2015-01-01", endDate: "2015-02-31"});
Event.find().count() // => 15,000, still have all the records
Event.find({ startDate:  "2015-01-01", endDate: "2015-01-31"}).count() // => 10,000, instantly without needing the records to be republished

我不认为 pub/sub 像这样开箱即用,但是有什么方法可以近似这种行为吗?

4

1 回答 1

0

如果您确实需要计数以外的数据,为什么不在客户端缓存来自查询的数据呢?您可以创建客户端本地集合:EventCache = new Mongo.Collection(null)并将Event.find()查询结果存储在缓存中。在您再次调用之前,Event.find()您可以先尝试在EventCache.

于 2015-04-28T19:01:50.753 回答