#8907: ORM-level Caching
----------------------------+-----------------------------------------------
   Reporter:  bradcater     |                Owner:  bradcater             
     Status:  new           |            Milestone:  post-1.0              
  Component:  Cache system  |              Version:  1.0                   
   Keywords:                |                Stage:  Design decision needed
  Has_patch:  1             |           Needs_docs:  0                     
Needs_tests:  0             |   Needs_better_patch:  0                     
----------------------------+-----------------------------------------------
 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

-- 
Ticket URL: <http://code.djangoproject.com/ticket/8907>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to