Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3766 -- Added in-memory caching for the sites framework. Will …

…speed up all the "current site" lookups. Thanks, Matt Riggott.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6180 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 36396fb4307af9764b2f8c53f8f0fcb926be249e 1 parent bc21612
Malcolm Tredinnick authored September 14, 2007
19  django/contrib/sites/models.py
@@ -2,15 +2,32 @@
2 2
 from django.utils.translation import ugettext_lazy as _
3 3
 from django.http import get_host
4 4
 
  5
+SITE_CACHE = {}
  6
+
5 7
 class SiteManager(models.Manager):
6 8
     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
+        """
7 14
         from django.conf import settings
8 15
         try:
9 16
             sid = settings.SITE_ID
10 17
         except AttributeError:
11 18
             from django.core.exceptions import ImproperlyConfigured
12 19
             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 = {}
14 31
 
15 32
 class Site(models.Model):
16 33
     domain = models.CharField(_('domain name'), max_length=100)
25  docs/sites.txt
@@ -213,6 +213,31 @@ To do this, you can use the sites framework. A simple example::
213 213
     >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url())
214 214
     'http://example.com/mymodel/objects/3/'
215 215
 
  216
+Caching the current ``Site`` object
  217
+===================================
  218
+
  219
+**New in Django development version**
  220
+
  221
+As 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
  223
+little cleverer than that: on the first request, the current site is cached, and
  224
+any subsequent call returns the cached data instead of hitting the database.
  225
+
  226
+If for any reason you want to force a database query, you can tell Django to
  227
+clear 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()
  240
+
216 241
 The ``CurrentSiteManager``
217 242
 ==========================
218 243
 

0 notes on commit 36396fb

Please sign in to comment.
Something went wrong with that request. Please try again.