So I had an idea: I have a quite expensive method call which is called quite frequently due to (React) UI changes, but 99.99% of the time returns the same result for the same parameters.
So I hacked together my own little caching function (without any invalidation yet), which actually works beautifully. It uses localForage to store the method results on the clients.
Please let me know what you think, if this is for some reason a bad idea, any improvement suggestions … if someone is interested I can make a small package out of it and release it on github.
It looks pretty nice - my one suggestion would be caching on the server, if many users frequently call this function then this caching will have a limited improvement when compared to caching on the server which will impact everyone. We use this pattern in our admin panel for some analytics that are quite heavy to compute - if two users hit at the same time (within 1 minute of each other) they share the same result, the next person to call after 1 minute gets the new result, and everyone who calls within the next minute shares that
Hmm that makes sense to also cache it on the server for the most common parameters. Some of the parameters really depend from user to user, so this doesn’t always make sense.
Thanks for your input!
I was just wondering if Meteor isn’t doing something like this already out of the box and I was missing something?! It baffles me that there is no caching at all in place by default?! Am I missing something? Is this somehow bad practice for a reason I can’t see yet?
It’s not bad practice - but its uncommon, calling your meteor methods too frequently could be considered poor reactivity in your UI - if you are happy to say “don’t ever rerun the same method call with the same arguments on a reactive data change” then you shouldn’t be making the call when the arguments don’t change.
Something like this will trigger a meteor call whenever any data passed into the template changes, when you only care about 3 parameters:
It is here you should really be caching the result, you could compare data.arg{1-3} against the arguments used when the method call was last ran, and if different you could re-run, otherwise skip. A nice way to do this is Tracker.guard it allows you to depend on a subset of a reactive data source:
Thanks a bunch for your insights.
The thing is: in my case the parameters do change during a typical user session. but if the parameters are the same, 99.9% of the time the call will return the same result.
In my case (React) I would compare my prevProps with this.props - but this does not help in this case, as the parameters definitely changed but still, I have to somehow cache the results if the same params already occured some time ago.
I’m not sure I follow - though I’m not really familiar with react, but if the parameters don’t change you should be able to not call the method, can you show your UI code that triggers the method?
I think I can explain:
My app is called Orderlion - with this app a restaurant owner can do his procurement over a single platform. Meaning, the restaurant f&b manager can order all his good from all his suppliers over Orderlion.
If the user chooses a supplier, in the sidebar, the (sometimes huge!) item categories tree is loaded via a Meteor.call() and displayed. So the this.props of the tree basically are the supplierId and some other stuff. As long as the user stays within this one supplier the tree in the sidebar isn’t even re-rendered, so all is fine.
now the user navigates to another supplier --> this.props (or supplierId for that matter) changed --> new method call --> new item categories tree.
If the user now navigates back to the first supplier the supplierId changed again, so only comparing the new supplierId to the old supplierId isn’t enough - I need to cache the exact item categories tree for the first supplierId and just return it from cache.
To keep all this as general as possible I decided to code my own Meteor.callCached function.
Ah, I understand now - you probably do have the right idea then, I don’t think you need localForage for this though, unless you care about caching across browser reloads? Also, how do you invalidate this cache (e.g., when will you call it again?)
I also use something similar to this but I actually do the caching on the server. I use a modified version of validated method that allows to pass a property called cacheTTL to each method and uses memory-cache package for caching the method response.
Not yet, but im thinking about it. It fixes an issue with npm simple-schema validation and adds this cache feature. Also thinking about adding a redis option so that the cache can be shared between multiple servers.
Ill post it on on the forum when I publish it, soon.
This really sounds to me that you should be using a database subscription! Instead of calling a Method, you could simply subscribe to “supplierItems” passing the supplier ID. When the user navigates to another supplier, just do not stop the first subscription so all data will be kept on the minimongo… You could even leverage this with a “favorites” subscription, that could be grounded on the client so it would not even need to be loaded from the server (maybe without the price field, which would need re-actualisation) so that would load super fast, and even offline for that matter.
Although I see a great opportunity for your method caching mechanic, I do feel that your exact problem has a way easier solution already provided by Meteor.
Have you thought about storing the cached tree in the database? In this way, you can cache across different servers and also invalidate the database object, when the data changes.
@klabauter, nice idea I was thinking the same for my app, but with some difference.
Why not use Session package which comes built in meteor instead of localForage
For invalidating cache, why not maintain the lastupdated field on the intended table for query, and check only this field and replace the cache if the lastupdated field on client and table are not same? In this approach ofcourse we first render screen with local cache and then after render we check for changes to be made in cache.
And ofcourse I also use react so this can be handled in react in a smooth way, may be also with some animation if required.
I am not using the Session package, because Session is lost after a page reload. I want my cache to be persistent even after a page reload or after closing and reopening the browser. That’s why I am using the localforage package.
About the invalidation: I am not sure if I understand you correctly. Please feel free to adapt my code and maybe we want to release a small package on github?! Any plans on doing that any maybe get some contributors on board?
Of course, I should have clarified, I use persistent session package - https://github.com/cult-of-coders/meteor-persistent-session.
For invalidation of cache, instead of invalidating the cache based on time, I want to maintain a last updated field on table and refresh client cache based on this.
For example lets consider a inventory stock table. I want to check when is the last updated date and if this is not same as my local cache, I want to replace cache in backend. Of course on initial render data will be wrong, but by cache will be refreshed in the background and we can always show a badge to the user, saying data is updated.
A package will be good, but just to mention that I am a novice coder, but will be happy to contribute.