Django

Code

Changeset 6180

Show
Ignore:
Timestamp:
09/14/07 03:37:09 (1 year ago)
Author:
mtredinnick
Message:

Fixed #3766 -- Added in-memory caching for the sites framework. Will speed up all the "current site" lookups. Thanks, Matt Riggott.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/sites/models.py

    r6174 r6180  
    33from django.http import get_host 
    44 
     5SITE_CACHE = {} 
     6 
    57class SiteManager(models.Manager): 
    68    def get_current(self): 
     9        """ 
     10        Returns the current ``Site`` based on the SITE_ID in the 
     11        project's settings. The ``Site`` object is cached the first 
     12        time it's retrieved from the database. 
     13        """ 
    714        from django.conf import settings 
    815        try: 
     
    1118            from django.core.exceptions import ImproperlyConfigured 
    1219            raise ImproperlyConfigured("You're using the Django \"sites framework\" without having set the SITE_ID setting. Create a site in your database and set the SITE_ID setting to fix this error.") 
    13         return self.get(pk=sid) 
     20        try: 
     21            current_site = SITE_CACHE[sid] 
     22        except KeyError: 
     23            current_site = self.get(pk=sid) 
     24            SITE_CACHE[sid] = current_site 
     25        return current_site 
     26 
     27    def clear_cache(self): 
     28        """Clears the ``Site`` object cache.""" 
     29        global SITE_CACHE 
     30        SITE_CACHE = {} 
    1431 
    1532class Site(models.Model): 
  • django/trunk/docs/sites.txt

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