views:

22

answers:

0

I've got strange issue - I hope someone run similar problem before.

I'm trying to cache content from different subdomains connected via wildcard to my django application: poczta-online.com .

So when I get to krakow.poczta-online.com i run differend code in view than poczta-online.com - simple middleware for that like here: http://stackoverflow.com/questions/772050/django-caching-for-subdomains/1069854#1069854

The thing is going wrong when I refresh simultonously pages form different domains (eg. using different tab browser). The result produce output of last refreshed page. And such behaviour do not depend on IP (if someone else is going onto some other subdomain in the same time you are entering main page you will get that subdomain content).

If I wait for loading page first then i move to other everythins is loaded correctly :|

If I turn off caching the problem does not exists.

My soft spec:

  • Ubuntu 8.04 LTS
  • Apache + mod-wsgi
    • threads 10 processes not defined multiprocess=false
  • Django 1.23
  • file caching backend

MIDDLEWARE:

class Subdomains:
    def process_request(self, request):
        u'''
            przekierowuje na stronę główną, jeżeli subdomena z której weszliśmy nie jest subdomeną miasta znajdującego się w bazie. Oraz ustawia zmienną request.META['city']!
        '''
        city = get_city_from_host(request.get_host())
        request.city=None
        if city:
            try:
                city = City.objects.filter(slug__exact=city)
                request.city=city[0].slug
            except:
                return HttpResponsePermanentRedirect(ROOT_URL)   

VIEW:

def post_data(request,address,id):
    url_root = settings.ROOT_URL
    city_subdomain = request.city
    if city_subdomain:
        random_posts = Post.objects.filter(city__slug=city_subdomain).order_by('?')
        if random_posts.count() <= 10:
            pass
        else:
            random_posts = random_posts[:10]

        city = City.objects.filter(slug__exact =  city_subdomain)[0]
        try:
            post = Post.objects.get(id = int(id), city__slug__exact=city.slug)
            nearestinposts = post.nearestinpost_set.select_related(depth=2).all()
            return render_to_response('post_data.html', locals())
        except:
            return HttpResponsePermanentRedirect('http://%s.%s/' % (city_subdomain, settings.ROOT_URL))

    return HttpResponsePermanentRedirect('http://%s' % settings.ROOT_URL)

SETTINGS.PY

CACHE_BACKEND = 'file://%s/cache/' % PROJECT_DIR
CACHE_MIDDLEWARE_SECONDS = 6000
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'middleware.default.Subdomains',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
)

To modify CACHE_KEY i use hack: http://stackoverflow.com/questions/772050/django-caching-for-subdomains/1364942#1364942