Ticket #15894: sites-cache.diff
File sites-cache.diff, 3.5 KB (added by , 13 years ago) |
---|
-
tests/regressiontests/sites_framework/tests.py
1 import time 2 from multiprocessing import Process, Queue 3 1 4 from django.conf import settings 2 5 from django.contrib.sites.models import Site 3 6 from django.test import TestCase … … 32 35 def test_invalid_field_type(self): 33 36 article = ConfusedArticle.objects.create(title="More Bad News!", site=settings.SITE_ID) 34 37 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
1 1 from django.db import models 2 2 from django.utils.translation import ugettext_lazy as _ 3 from django.core.cache import cache 3 4 4 5 5 SITE_CACHE = {}6 7 8 6 class SiteManager(models.Manager): 9 7 10 8 def get_current(self): … … 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_site24 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_CACHE32 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): … … 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):