Meteor.userCache
Meteor.userCache builds upon three simple premises:
- Every time you run Meteor.user() on the server, it has to call the database to retrieve the user document.
- Every time a user account connects to your server, Meteor automatically subscribes for their user document.
- Every time a subscription is started, the documents of subscription are cached on the server.
Instead of querying the database every time that Meteor.user()
is executed, we should first see if it's sufficient to retrieve it from the server-side cache (MergeBox). Since the server-side cache is updated in real-time, the risk of stale data may be insignificant.
The result is:
- fewer database queries
- better performance
- faster response time
How to Use
First, add to the package to your application:
meteor add msavin:usercache
Second, use Meteor.userCache()
as you would use Meteor.user()
inside of a Method or Publication
1Meteor.methods({ 2 "posts/create": function (content) { 3 var userDoc = Meteor.userCache() || Meteor.user(); 4 5 if (!userDoc.profile.roles.includes("banned")) { 6 Posts.insert({ 7 user: userDoc._id, 8 date: new Date(), 9 content: content 10 }) 11 } 12 } 13})
In the example above, instead of querying the database for the user document every time a post is made, we can:
- first, try to get the cached user data
- second, revert to a database call if no cache is available
- third, authorize the request
Not only would this reduce a significant amount of database queries, but it also speeds up how quickly messages are inserted into the database and sent out to the other users.
Status
This package works fine, but is currently out as a proof of concept.