Ticket #23661: #23661-sites_cache_using.diff

File #23661-sites_cache_using.diff, 2.6 KB (added by German M. Bravo, 10 years ago)
  • django/contrib/sites/models.py

    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  
    99from django.core.exceptions import ValidationError
    1010
    1111
    12 SITE_CACHE = {}
    13 
    14 
    1512def _simple_domain_name_validator(value):
    1613    """
    1714    Validates that the given value contains no whitespaces to prevent common
    def _simple_domain_name_validator(value):  
    2926
    3027class SiteManager(models.Manager):
    3128
     29    _cache = {}
     30
    3231    def get_current(self):
    3332        """
    3433        Returns the current ``Site`` based on the SITE_ID in the
    class SiteManager(models.Manager):  
    4140        except AttributeError:
    4241            from django.core.exceptions import ImproperlyConfigured
    4342            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."""
    4447        try:
    45             current_site = SITE_CACHE[sid]
     48            site = self.__class__._cache[self.db][pk]
    4649        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
    5055
    51     def clear_cache(self):
     56    def clear_cache(self, instance=None):
    5257        """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
    5570
    5671
    5772@python_2_unicode_compatible
    def clear_site_cache(sender, **kwargs):  
    111126    Clears the cache (if primed) each time a site is saved or deleted
    112127    """
    113128    instance = kwargs['instance']
    114     try:
    115         del SITE_CACHE[instance.pk]
    116     except KeyError:
    117         pass
     129    Site.objects.clear_cache(instance)
    118130pre_save.connect(clear_site_cache, sender=Site)
    119131pre_delete.connect(clear_site_cache, sender=Site)
Back to Top