﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
8907	ORM-level Caching	bradcater	anonymous	"MOTIVATION:
- Suppose the following is very expensive:

    {{{ MyStuff.objects.filter(<conditions>) }}}

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.

USE CASES:
- 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.
  - Example: {{{ MyStuff.objects.filter(extra_type=4).cache(3600) }}}
  - 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)).
  - Example: {{{ MyStuff.objects.filter(mass=20).cache(3600,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
specific call.
  - Example: {{{ MyStuff.objects.nocache().filter(user__id=7) }}}
  - 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.

NOTES:
- 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., 
  {{{ MyStuff.objects.nocache().filter(contact__id=2).cache() }}}
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 CHANGES:
- .../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
"		closed	Core (Cache system)	1.0		duplicate			Design decision needed	1	0	1	0	0	0
