我有一个很大的可搜索数据集。在渲染页面之前发送整个数据集的速度令人无法接受,因此我们目前只是发送包含搜索的部分,因此
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开箱即用,但有什么方法可以估算这种行为吗?
答案 0 :(得分:0)
如果您确实需要的数据不仅仅是计数,为什么不在客户端缓存查询中的数据?您可以创建客户端本地集合:EventCache = new Mongo.Collection(null)
并将Event.find()
查询的结果存储在缓存中。在拨打Event.find()
之前,您可以先尝试在EventCache
上运行查询。