Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cache the data in Meteor?

Tags:

caching

meteor

thanks everyone! recently i want to built a small cms on meteor,but have some question

1,cache,page cache,data cache,etc..

For example,when people search some article

in server side:

 Meteor.publist('articles',function(keyword){
   return Articles.find({keyword:keyword});
});

in client:

Meteor.subscribe('articles',keyword);

that's ok ,but ...... the question is ,everytime people doing so ,it invoke a mongo query,and reduce the performance, in other framework use common http or https,people can depend on something like squid or varnish to cache the page or data,so everytime you route to a url,you read data from the cache server ,but Meteor built on socket.js or websocket,and I don't know how to cache throught the socket.......I trid varnish ,but seen no effect. so,may be it ignore the websocket?is there some method to cache the data,in the mongodb,in server,can i add some cache server ?

2, chat

I see the chatroom example in https://github.com/zquestz/simplechat But unlike implyment using socket.js,this example save the chat message in the mongodb ,so the data flow is message ->mongo->query->people,this invoke the mongo query too! and in socket.js,just save the socket in the context(or the server side cache),so the data don't go throught the db. My question is , is there a socket interface in Meteor ,so I can message->socket->people? and if can't , how is the performace in the productive envirment as the chatroom example doing(i see it runs slow ...)

like image 671
user1208300 Avatar asked Dec 25 '12 04:12

user1208300


1 Answers

With Meteor, you don't have to worry about caching Mongodb queries. Meteor does that for you. Per the docs on data and security:

Every Meteor client includes an in-memory database cache. To manage the client cache, the server publishes sets of JSON documents, and the client subscribes to those sets. As documents in a set change, the server patches each client's cache.

[...]

Once subscribed, the client uses its cache as a fast local database, dramatically simplifying client code. Reads never require a costly round trip to the server. And they're limited to the contents of the cache: a query for every document in a collection on a client will only return documents the server is publishing to that client.

Because Meteor does poll the server every so often to see if the client's cache needs patching, you're probably seeing those polls happening every now and then. But they probably aren't very large requests. Additionally, due to a feature of Meteor called latency compensation, when you update a data source, the client immediately reflects the change without first waiting on the server. This reduces the appearance of performance reduction to the user.

If you have many documents in mongo, you may also be seeing them all get fetched if you still have the autopublish package enabled. You can fix that by removing it with meteor remove autopublish and write code to only publish the relevant data instead of the entire database.

If you really need to manage caching manually, the docs also go into that:

Sophisticated clients can turn subscriptions on and off to control how much data is kept in the cache and manage network traffic. When a subscription is turned off, all its documents are removed from the cache unless the same document is also provided by another active subscription.

Additional performance improvements to Meteor are currently being worked on, including a DDP-level proxy to support "very large number of clients". You can see more detail on this at the Meteor roadmap.

like image 152
Rahul Avatar answered Oct 20 '22 21:10

Rahul