Trying to understand what happens during a django low-level cache.set() Particularly, details about what part of the queryset gets stored in memcached.
First, am I interpreting the django docs correctly?
- a queryset (python object) has/maintains its own cache
- access to the database is lazy; even if the queryset.count is 1000, if I do an object.get for 1 record, then the dbase will only be accessed once, for that 1 record.
- when accessing a django view via apache prefork MPM, everytime that a particular daemon instance X ends up invoking a particular view that includes something like "tournres_qset = TournamentResult.objects.all()", this will then result, each time, in a new tournres_qset object being created. That is, anything that may have been cached internally by a tournres_qset python object from a previous (tcp/ip) visit, is not used at all by a new request's tournres_qset.
Now the questions about saving things to memcached within the view. Let's say I add something like this at the top of the view:
tournres_qset = cache.get('tournres', None)
if tournres_qset is None:
tournres_qset = TournamentResult.objects.all()
cache.set('tournres', tournres_qset, timeout)
# now start accessing tournres_qset
# ...
What gets stored during the cache.set()?
Does the whole queryset (python object) get serialized and saved?
Since the queryset hasn't been used yet to get any records, is this just a waste of time, since no particular records' contents are actually being saved in memcache? (Any future requests will get the queryset object from memcache, which will always start fresh, with an empty local queryset cache; access to the dbase will always occur.)
If the above is true, then should I just always re-save the queryset at the end of the view, after it's been used throughout the vierw to access some records, which will result in the queryset's local cache to get updated, and which should always get re-saved to memcached? But then, this would always result in once again serializing the queryset object. So much for speeding things up.
Or, does the cache.set() force the queryset object to iterate and access from the dbase all the records, which will also get saved in memcache? Everything would get saved, even if the view only accesses a subset of the query set?
I see pitfalls in all directions, which makes me think that I'm
misunderstanding a whole bunch of things.
Hope this makes sense and appreciate clarifications or pointers to some "standard" guidelines. Thanks.