|Reported by:||bradcater||Owned by:||anonymous|
|Component:||Core (Cache system)||Version:||1.0|
|Cc:||Triage Stage:||Design decision needed|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||no|
- Suppose the following is very expensive:
We commonly use caching to avoid doing this operation often, which can
leave our code littered with the following:
cached = cache.get(<cachekey>) if not cached: cached = MyStuff.objects.filter(<conditions>) cache.set(<cachekey>,cached,<time>) return cached
We seek to unify the Django ORM and caching in a natural way.
- To cache QuerySets, append .cache(<time>) to the end of the Django
ORM call. This will cache the given object for <time> seconds provided
that the cachekey is not already in the cache.
- This should return all MyStuff objects with an id greater than 10 and should store the result to the cache for 3600 seconds (1 hour).
- To force caching (even if the cachekey already exists), provide the
force argument (e.g., .cache(<time>,force=True)).
- This should return all MyStuff objects with a mass of 20 and should cache the result for 3600 seconds even if it would over-write a previously-cached result.
- To ensure that a cached value is not used, use .nocache() before any
- This should return all MyStuff objects with a User whose id is 7 and should not use a previously-cached value even if it exists.
- Cachekeys are generated as the md5 hexdigest of the generated SQL.
Ultimately, this detail should not matter and is subject to change.
- It is a subtle but salient point that you can call, e.g.,
thus using cache() and nocache() together. It is desirable to do this
if you wish to get the latest results from the db (ignoring any data
currently in the cache) and caching them for later use.
- .../django/db/models/query.py :
- cache(self,time,force=False) added for QuerySet objects
- nocache(self) added for QuerySet objects
- _cachekey(self) added for QuerySet objects
- _clone(self, klass=None, setup=False, kwargs) updated for QuerySet objects
Change History (7)
comment:1 follow-up: 4 Changed 9 years ago by
|Owner:||changed from bradcater to anonymous|
|Status:||new → assigned|