Skip to content

Commit

Permalink
Start storing CNAMEs seen in redis.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericholscher committed Apr 3, 2011
1 parent 85a27d8 commit 4f4d380
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions readthedocs/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from django.http import Http404
from django.utils.encoding import smart_unicode

import redis


from projects.views.public import slug_detail, subdomain_handler

#Thanks to debug-toolbar for the response-replacing code.
Expand Down Expand Up @@ -66,15 +69,16 @@ def process_request(self, request):
and 'testserver' not in host:
request.cname = True
try:
redis_conn = redis.Redis(**settings.REDIS)
slug = cache.get(host)
if not slug:
from dns import resolver
answer = [ans for ans in resolver.query(host, 'CNAME')][0]
domain = answer.target.to_unicode()
slug = domain.split('.')[0]
cache.set(host, slug, 60*60)
#Cache the slug <-> host mapping as well.
cache.set(slug, host, 60*60)
#Cache the slug -> host mapping permanently.
redis_conn.sadd("rtd_slug:v1:%s" % slug, host)
request.slug = slug
return subdomain_handler(request,
slug,
Expand Down

0 comments on commit 4f4d380

Please sign in to comment.