Code

Ticket #3766: sites_docs.2.diff

File sites_docs.2.diff, 1.3 KB (added by Matt Riggott, 7 years ago)

Formatting changes to the docs diff: lines wrapped at 80 characters and a "New in Django development version" notice added.

Line 
1Index: django/trunk/docs/sites.txt
2===================================================================
3--- django/trunk/docs/sites.txt (revision 5823)
4+++ django/trunk/docs/sites.txt (working copy)
5@@ -213,6 +213,31 @@
6     >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url())
7     'http://example.com/mymodel/objects/3/'
8 
9+Caching the current ``Site`` object
10+===================================
11+
12+**New in Django development version**
13+
14+As the current site is stored in the database, each call to
15+``Site.objects.get_current()`` could result in a database query. But Django is a
16+little cleverer than that: on the first request, the current site is cached, and
17+any subsequent call returns the cached data instead of hitting the database.
18+
19+If for any reason you want to force a database query, you can tell Django to
20+clear the cache using ``Site.objects.clear_cache()``::
21+
22+    # First call; current site fetched from database.
23+    current_site = Site.objects.get_current()
24+    # ...
25+
26+    # Second call; current site fetched from cache.
27+    current_site = Site.objects.get_current()
28+    # ...
29+
30+    # Force a database query for the third call.
31+    Site.objects.clear_cache()
32+    current_site = Site.objects.get_current()
33+
34 The ``CurrentSiteManager``
35 ==========================
36