CandyCrusher
CandyCrusher

Reputation: 316

The effect of no_cache() on querySet in Mongoengine

In official documentation of mongoengine, it says that as of 0.8, no_cache() is added into the mongoengine. What's the benefit it can bring to us? What's the typical scenario no_cache apply for?

Upvotes: 6

Views: 1823

Answers (1)

bagerard
bagerard

Reputation: 6374

Mongoengine maintainer here - by default (and historically), mongoengine caches all results of a queryset as you are iterating over it. This has the benefit of not firing a query if you re-iterate over the same variable but has the drawback of keeping everything in memory. I.e:

class User(Document):
    pass

users = User.objects()         # users is a queryset, it didn't hit the db yet

_ = [us for us in in users]    # hits the db and caches all user instances in the users object
_ = [us for us in in users]    # does not hit the db anymore, uses the users cached data


users = User.objects().no_cache()
_ = [us for us in in users]    # hits the db and caches all user instances
_ = [us for us in in users]    # hits the db again

It sounds like a good idea to use the cache but in practice you rarely iterate over the same queryset 2 times and the memory consumption can become a problem if you are iterating over very large collections.

Note that in the future, it may change to use the no_cache version by default

Upvotes: 7

Related Questions