Effective content caching for mass-load site using redirect feature

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#retry/views.py file
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, HttpResponseBadRequest, Http404
from django.utils.http import urlencode
from django.conf import settings
from django.utils.encoding import iri_to_uri
import time
import logging
logger = logging.getLogger('retry')
# Create your views here.
def retry_after(request):
    default_timeout = settings.DEFAULT_RETRY_TIMEOUT
    if not 'redirect' in request.GET:
        return HttpResponseBadRequest('redirect parameter required')
    redirect = request.GET['redirect']
    time_retry = time.time() + default_timeout
    if 'retry_after' in request.GET:
        time_retry = int(float(request.GET['retry_after']))
    if settings.DEBUG: logger.debug("RETRY AFTER GOT FOR: %s" % redirect)
    time_now = time.time()
    if time_now < time_retry:
        if settings.DEBUG: logger.debug("RETRY AFTER IS FROM DUMB FOR: %s" % redirect)
        sleep_timeout = time_retry - time_now
        if sleep_timeout > default_timeout: sleep_timeout =  default_timeout
        # the client is dumb, we need to wait on the server side
        time.sleep(sleep_timeout)
    time_now = time.time()
    if time_now < time_retry:
        if settings.DEBUG: logger.debug("RESEND RETRY AFTER FOR: %s" % redirect)
        r = HttpResponseRedirect( iri_to_uri(request.path) + '?' + urlencode( {'retry_after':int(time_retry), 'redirect':redirect} ) )
        r['Retry-After'] = int(time_retry - time_now) + 1
        return r
    # Tell a client to retrieve original URL
    if settings.DEBUG: logger.debug("REDIRECT TO ORIGINAL URL FOR: %s" % redirect)
    r = HttpResponseRedirect( redirect )
    return r

More like this

  1. nginx x-accel-redirect protection of static files by sean 6 years, 4 months ago
  2. Get Client IP Behind Proxy by brianjaystanley 2 years, 5 months ago
  3. caching parsed templates by forgems 6 years, 4 months ago
  4. Workaround Firefox bug 553888 by peroksid 3 years, 1 month ago
  5. Run and cache only one instance of a heavy request by farnsworth 3 years, 8 months ago

Comments

(Forgotten your password?)