Django

Code

Ticket #3766: sites_docs.diff

File sites_docs.diff, 1.3 kB (added by Matt Riggott, 1 year ago)

Documentation on the caching and the Site.objects.clear_cache() method.

  • django/trunk/docs/sites.txt

    old new  
    213213    >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url()) 
    214214    'http://example.com/mymodel/objects/3/' 
    215215 
     216Caching the current ``Site`` object 
     217=================================== 
     218 
     219As the current site is stored in the database, each call to ``Site.objects.get_current()`` could result in a database query. But Django is a little cleverer than that: on the first request, the current site is cached, and any subsequent call returns the cached data instead of hitting the database. 
     220 
     221If for any reason you want to force a database query, you can tell Django to clear the cache using ``Site.objects.clear_cache()``:: 
     222 
     223    # First call; current site fetched from database. 
     224    current_site = Site.objects.get_current() 
     225    # ... 
     226 
     227    # Second call; current site fetched from cache. 
     228    current_site = Site.objects.get_current() 
     229    # ... 
     230 
     231    # Force a database query for the third call. 
     232    Site.objects.clear_cache() 
     233    current_site = Site.objects.get_current() 
     234 
    216235The ``CurrentSiteManager`` 
    217236========================== 
    218237