diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py
index bbd85ed..4875a64 100644
a
|
b
|
from django.utils.encoding import python_2_unicode_compatible
|
9 | 9 | from django.core.exceptions import ValidationError |
10 | 10 | |
11 | 11 | |
12 | | SITE_CACHE = {} |
13 | | |
14 | | |
15 | 12 | def _simple_domain_name_validator(value): |
16 | 13 | """ |
17 | 14 | Validates that the given value contains no whitespaces to prevent common |
… |
… |
def _simple_domain_name_validator(value):
|
29 | 26 | |
30 | 27 | class SiteManager(models.Manager): |
31 | 28 | |
| 29 | _cache = {} |
| 30 | |
32 | 31 | def get_current(self): |
33 | 32 | """ |
34 | 33 | Returns the current ``Site`` based on the SITE_ID in the |
… |
… |
class SiteManager(models.Manager):
|
41 | 40 | except AttributeError: |
42 | 41 | from django.core.exceptions import ImproperlyConfigured |
43 | 42 | 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.") |
| 43 | return self.get_for_pk(sid) |
| 44 | |
| 45 | def get_for_pk(self, pk): |
| 46 | """Lookup a Site by ID.""" |
44 | 47 | try: |
45 | | current_site = SITE_CACHE[sid] |
| 48 | site = self.__class__._cache[self.db][pk] |
46 | 49 | except KeyError: |
47 | | current_site = self.get(pk=sid) |
48 | | SITE_CACHE[sid] = current_site |
49 | | return current_site |
| 50 | # This could raise a DoesNotExist; that's correct behavior and will |
| 51 | # make sure that only correct sites get stored in the cache dict. |
| 52 | site = self.get(pk=pk) |
| 53 | self._add_to_cache(self.db, site) |
| 54 | return site |
50 | 55 | |
51 | | def clear_cache(self): |
| 56 | def clear_cache(self, instance=None): |
52 | 57 | """Clears the ``Site`` object cache.""" |
53 | | global SITE_CACHE |
54 | | SITE_CACHE = {} |
| 58 | if instance: |
| 59 | using = instance._state.db |
| 60 | try: |
| 61 | del self.__class__._cache[using][instance.pk] |
| 62 | except KeyError: |
| 63 | pass |
| 64 | else: |
| 65 | self.__class__._cache.clear() |
| 66 | |
| 67 | def _add_to_cache(self, using, site): |
| 68 | """Insert a Site into the cache.""" |
| 69 | self.__class__._cache.setdefault(using, {})[site.pk] = site |
55 | 70 | |
56 | 71 | |
57 | 72 | @python_2_unicode_compatible |
… |
… |
def clear_site_cache(sender, **kwargs):
|
111 | 126 | Clears the cache (if primed) each time a site is saved or deleted |
112 | 127 | """ |
113 | 128 | instance = kwargs['instance'] |
114 | | try: |
115 | | del SITE_CACHE[instance.pk] |
116 | | except KeyError: |
117 | | pass |
| 129 | Site.objects.clear_cache(instance) |
118 | 130 | pre_save.connect(clear_site_cache, sender=Site) |
119 | 131 | pre_delete.connect(clear_site_cache, sender=Site) |