Did you know that every time you run Meteor.user(), the function has to call the database in order to retrieve the user document?
For those of us that store things like roles data on the user document, it means that nearly every time we run a method, we have to make a database request.
For those of that do not, well… once you see this package, you might want to start
This is an interesting idea! I know we could do it in our app code if we use this package, but I think providing a way to configure it so that it monkey patches Meteor.user() to internally check MergeBox first would be helpful so that we don’t have to change the possibly hundreds of places we use it, and would make for a cleaner API. Something like
import UserCache from 'meteor/msavin:usercache':
UserCache.configure({
automatic: true
});
I’m not big on monkey patching, but maybe this can be a good time for doing that.
What do you folks think about this kind of API:
Meteor.user() // works as usual
Meteor.user(true) // gets data from mergebox
Meteor.user(['profile.name', 'email']) // verifies if the fields are in cache. If not, retrieves them from database
This shouldn’t break anything, and would provide a clear integration path.
This looks great! How does the cache get updated? For example, if I have a user logged in that has an Admin role, and the role is removed - when will the cache be updated such that they no longer have any associated Admin privileges?
It should be fine. As I understand, redis-oplog (currently) only replaces the parts around oplog tailing, but everything from mergebox to pub/sub is Meteor’s code.
I think it’s fine to use with redis-oplog but are you absolutely sure it queries the database everytime I always thought it reads from the local collection?
You subscribe to the current user, it pushes it to the client. And that’s it. Also I’ve never seen multiple data coming down the pipe in websockets when Meteor.user() was called.
So I am curious to find out how you reached this conclusion ?
On the client, this.users.findOne(userId) will query minimongo (local cache). On the server, this.users.findOne(userId) would query the MongoDB collection. This package targets the server-side usecase.
If you look at this snippet, posted a few posts ago, it would literally query the database four times.
Oh, I now completely understand. I never used Meteor.user() anywhere on the server it’s just too magical. But for those who use it I think that’s fine. I don’t see there how do you invalidate the cache ? I’m curious to understand your approach. What happens if the user’s role gets demoted, or he is suspended ?