Ticket #15894: sites-cache.diff

File sites-cache.diff, 3.5 KB (added by Flavio Curella, 13 years ago)

better patch, with tests

  • tests/regressiontests/sites_framework/tests.py

     
     1import time
     2from multiprocessing import Process, Queue
     3
    14from django.conf import settings
    25from django.contrib.sites.models import Site
    36from django.test import TestCase
     
    3235    def test_invalid_field_type(self):
    3336        article = ConfusedArticle.objects.create(title="More Bad News!", site=settings.SITE_ID)
    3437        self.assertRaises(TypeError, ConfusedArticle.on_site.all)
     38   
     39    def test_multiprocess(self):
     40        def first_process(q):
     41            from django.contrib.sites.models import Site
     42            s = Site.objects.get_current()
     43            first_try = s.domain
     44            time.sleep(2)
     45            s2 = Site.objects.get_current()
     46            second_try = s2.domain
     47            are_equals = (first_try == second_try)
     48            q.put([are_equals])
     49
     50        def second_process(q):
     51            from django.contrib.sites.models import Site
     52            s = Site.objects.get_current()
     53            time.sleep(1)
     54            s.domain = s.domain+'_changed'
     55            s.save()
     56
     57        q = Queue()
     58        p1 = Process(target=first_process, args=(q,))
     59        p2 = Process(target=second_process, args=(q,))
     60        try:
     61            p1.start()
     62            p2.start()
     63            p1.join()
     64            p2.join()
     65        except:
     66            p1.terminate()
     67            p2.terminate()
     68        self.assertFalse(q.get()[0])
     69        q.close()
  • django/contrib/sites/models.py

     
    11from django.db import models
    22from django.utils.translation import ugettext_lazy as _
     3from django.core.cache import cache
    34
    45
    5 SITE_CACHE = {}
    6 
    7 
    86class SiteManager(models.Manager):
    97
    108    def get_current(self):
     
    1917        except AttributeError:
    2018            from django.core.exceptions import ImproperlyConfigured
    2119            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:
    2523            current_site = self.get(pk=sid)
    26             SITE_CACHE[sid] = current_site
     24            cache.set('site-cache-%s' % sid, current_site)
    2725        return current_site
    2826
    2927    def clear_cache(self):
    3028        """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)
    3331
    3432
    3533class Site(models.Model):
     
    5048    def save(self, *args, **kwargs):
    5149        super(Site, self).save(*args, **kwargs)
    5250        # 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)
    5552
    5653    def delete(self):
    5754        pk = self.pk
    5855        super(Site, self).delete()
    59         try:
    60             del SITE_CACHE[pk]
    61         except KeyError:
    62             pass
     56        cache.delete('site-cache-%s' % self.id)
    6357
    6458
    6559class RequestSite(object):
Back to Top