bozhao:publish-cache

v0.2.1Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Publish Cache

A Meteor package designed to provide a snapshot of dataset to the clients.

About

Publish Cache is a Meteor package designed to provide a new way to access the Read heavy collection without the expensive livedata from regular Meteor pubsub.

API

It is designed to be similiar to Meteor's own pubsub API.

Server

Meteor.publishCache(name, func, options): publish a record set

Arguments:

name(String, required):

Name of the record set. It can't not be null or undefined.

func(Function, required):

Function called on the server each time a client subscribeCaches. Inside the function, this is bound to a method invocation object, which has the same properties as Meteor.methods. This function can return a Collection cursor, or array of collection cursors.

Client

Meteor.subscribeCache(name [, arg1, arg2, ...] [, callbacks])

Arguments:

name (String, required):

Name of the subscription cache. Matches the name of ther server's publishCache() call.

arg1, arg2, ... (Any optional):

Optional arguments passed to the publish Cache function on server

callbacks (Function or Object optional):

Optional. May include onError and onReady callbacks. If a function is passed instead of an object, it will interpreted as an onReady callback.

Usage Pattern and Examples

Basic: return a cursor

1    Items = new Meteor.Collection('items');
2    if (Meteor.isServer) {
3      //we want to get info on other items that share the same tag.
4      Meteor.publishCache('additionalItems', function (tag) {
5        return Items.find({tag: tag});
6      });
7    }
8    if (Meteor.isClient) {
9      Meteor.subscribeCache('additionalItems', 'someTag');
10    }

Return Array of cursors

We can return an array of cursors. Instead of populated 1 collection, we can populated more.

1    Books = new Meteor.Collection('books');
2    Authors = new Meteor.Collection('authors');
3    
4    if (Meteor.isServer) {
5      Meteor.publishCache('authorInfo', function (name) {
6        var bookCursor = Books.find({author: name});
7        var authroCursor = Authors.find({name: name});
8        return [bookCursor, authorCursos];
9      };
10    }
11    if (Meteor.isClient) {
12      Meteor.Cache('authorInfo', name);
13    }

Unblock DDP message queue

Since DDP messages (subscription and method) are processed in a sequence, sometimes, we want the subscription that router are depend on executes first.

In this example, we want to render the page, as soon as the first 5 game scores loaded to client. So we could create a small pub that grab the first five. And use publishCache package to get rest of them without blocking the DDP message queue.

1    SportsScores = new Meteor.Collection('sports_scores');
2   
3    if (Meteor.isServer) {
4      Meteor.publish('todayScores', function (sportType) {
5        return SportsScores.find({type: sportType}, {limit: 5, sort: {date: -1}});
6      }):
7      Meteor.publishCache('todayScoresAll', function (sportType) {
8        return SportsScores.find({type: sportType}, {sort: {date: -1}});
9      }, {unblock: true});
10    }
11    if (Meteor.isClient) {
12    //Using Iron router as an example
13      Router.map(function () {
14        this.route('todayScores', {
15          path: '/scores/:type',
16          waitOn: function () {
17            Meteor.subscribeCache('todayScoresAll', this.params.type);
18            
19            return Meteor.subscribe('todayScores', this.params.type);
20          }
21        });
22      });
23    }

License

MIT