I’m iterating over objects using the same cursor a few times, so I’m assuming that keeping the objects in memory would be faster.
I tried to put all the objects in an array before I used them with objects = cursor.to_a, but the the call blocks any further calculations while it waits for the data to download and is ultimately slower.
Another way I thought of is to append the documents in an array as I’m doing the calculations, then use the array to do further calculations. Though this is pretty unclean and hard to maintain.
Is there any code out there that already does this?
So there’s already a method that does this in Mongoid. It caches per-query and per iteration, so it really only keeps the data in memory when you query for it, so it doesn’t block.
It’s just a function on the cursor and you can call it like this:
Model.where(:name => "John").cacheMore info here: http://mongoid.org/docs/extras.html