diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py
index fecbff7..d5419f9 100644
a
|
b
|
|
1 | 1 | from django.db import models |
2 | 2 | from django.utils.translation import ugettext_lazy as _ |
3 | | |
4 | | |
5 | | SITE_CACHE = {} |
| 3 | from django.core.cache import cache |
6 | 4 | |
7 | 5 | |
8 | 6 | class SiteManager(models.Manager): |
… |
… |
class SiteManager(models.Manager):
|
19 | 17 | except AttributeError: |
20 | 18 | from django.core.exceptions import ImproperlyConfigured |
21 | 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.") |
22 | | try: |
23 | | current_site = SITE_CACHE[sid] |
24 | | except KeyError: |
| 20 | |
| 21 | current_site = cache.get('site-cache-%s' % sid) |
| 22 | if current_site is None: |
25 | 23 | current_site = self.get(pk=sid) |
26 | | SITE_CACHE[sid] = current_site |
| 24 | cache.set('site-cache-%s' % sid, current_site) |
27 | 25 | return current_site |
28 | 26 | |
29 | 27 | def clear_cache(self): |
30 | 28 | """Clears the ``Site`` object cache.""" |
31 | | global SITE_CACHE |
32 | | SITE_CACHE = {} |
| 29 | for s in self.all(): |
| 30 | cache.delete('site-cache-%s' % s.id) |
33 | 31 | |
34 | 32 | |
35 | 33 | class Site(models.Model): |
… |
… |
class Site(models.Model):
|
50 | 48 | def save(self, *args, **kwargs): |
51 | 49 | super(Site, self).save(*args, **kwargs) |
52 | 50 | # Cached information will likely be incorrect now. |
53 | | if self.id in SITE_CACHE: |
54 | | del SITE_CACHE[self.id] |
| 51 | cache.delete('site-cache-%s' % self.id) |
55 | 52 | |
56 | 53 | def delete(self): |
57 | 54 | pk = self.pk |
58 | 55 | super(Site, self).delete() |
59 | | try: |
60 | | del SITE_CACHE[pk] |
61 | | except KeyError: |
62 | | pass |
| 56 | cache.delete('site-cache-%s' % self.id) |
63 | 57 | |
64 | 58 | |
65 | 59 | class RequestSite(object): |
diff --git a/docs/ref/contrib/sites.txt b/docs/ref/contrib/sites.txt
index 8fc434b..7762eac 100644
a
|
b
|
fallback for cases where it is not installed.
|
178 | 178 | :class:`~django.contrib.sites.models.RequestSite` object based on |
179 | 179 | the request. |
180 | 180 | |
| 181 | Note: ``get_current_site()`` and :attr:`~Site.objects.get_current()` use the cache backend specified in your CACHE_BACKEND setting. |
| 182 | |
181 | 183 | Getting the current domain for display |
182 | 184 | -------------------------------------- |
183 | 185 | |
diff --git a/tests/regressiontests/sites_framework/tests.py b/tests/regressiontests/sites_framework/tests.py
index 8e664fd..ee0f277 100644
a
|
b
|
|
1 | 1 | from __future__ import absolute_import |
2 | 2 | |
| 3 | import time |
| 4 | from multiprocessing import Process, Queue, Value |
| 5 | |
3 | 6 | from django.conf import settings |
4 | 7 | from django.contrib.sites.models import Site |
5 | 8 | from django.test import TestCase |
… |
… |
class SitesFrameworkTestCase(TestCase):
|
36 | 39 | def test_invalid_field_type(self): |
37 | 40 | article = ConfusedArticle.objects.create(title="More Bad News!", site=settings.SITE_ID) |
38 | 41 | self.assertRaises(TypeError, ConfusedArticle.on_site.all) |
| 42 | |
| 43 | def test_multiprocess(self): |
| 44 | def check_if_domain_changed(phase, domain_changed): |
| 45 | from django.contrib.sites.models import Site |
| 46 | |
| 47 | # first phase - get domain |
| 48 | site = Site.objects.get_current() |
| 49 | first_try = site.domain |
| 50 | phase.value = 2 |
| 51 | |
| 52 | # second phase - wait |
| 53 | while phase.value == 2: |
| 54 | pass |
| 55 | |
| 56 | # third phase - again get domain |
| 57 | s2 = Site.objects.get_current() |
| 58 | second_try = s2.domain |
| 59 | |
| 60 | # compare domains |
| 61 | domain_changed.value = (first_try != second_try) |
| 62 | |
| 63 | def change_domain(phase): |
| 64 | from django.contrib.sites.models import Site |
| 65 | |
| 66 | # first phase - wait |
| 67 | while phase.value == 1: |
| 68 | pass |
| 69 | |
| 70 | # second phase |
| 71 | site = Site.objects.get_current() |
| 72 | site.domain = site.domain+'_changed' |
| 73 | site.save() |
| 74 | phase.value = 3 |
| 75 | |
| 76 | phase = Value('i', 1) |
| 77 | domain_changed = Value('b', False) |
| 78 | first_process = Process(target=check_if_domain_changed, args=(phase, domain_changed)) |
| 79 | second_process = Process(target=change_domain, args=(phase,)) |
| 80 | |
| 81 | first_process.start() |
| 82 | second_process.start() |
| 83 | first_process.join() |
| 84 | second_process.join() |
| 85 | |
| 86 | self.assertTrue(domain_changed.value) |