--- a/cmpuqrct/settings/base.py Mon Sep 02 03:20:10 2013 +0300
+++ b/cmpuqrct/settings/base.py Mon Sep 02 03:50:34 2013 +0300
@@ -10,6 +10,14 @@
}
+# Use a very conservative cache without any proper invalidation
+CACHES = {
+ 'default': {
+ 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
+ 'TIMEOUT': 5*60, # 5min
+ }
+}
+
# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []
--- a/qrurls/views.py Mon Sep 02 03:20:10 2013 +0300
+++ b/qrurls/views.py Mon Sep 02 03:50:34 2013 +0300
@@ -1,3 +1,4 @@
+from django.core.cache import cache
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render
from django.utils import timezone, http
@@ -34,10 +35,10 @@
# match for published items
now = timezone.now()
url_item = url_item.filter(published__lt=now).order_by('-published')
-
- if item_id:
+
+ if item_id :
return url_item.get(id=int(item_id))
- else:
+ else :
# most recent
try:
return url_item[0]
@@ -54,14 +55,24 @@
"""
Primary frontend for redirecting based on current time.
"""
+
+ key = 'qrurls/urlfeed/{}/url'.format(opts)
+ data = cache.get(key)
+
+ if data :
+ url, modified = data
+ else:
+ url_item = _get_url_item(**opts)
+ modified = url_item.last_modified()
+ url = url_item.get_absolute_url()
- url_item = _get_url_item(**opts)
+ cache.set(key, (url, modified)) # XXX: expiry
# redirect, either directly, or to image()
- response = HttpResponseRedirect(url_item.get_absolute_url())
+ response = HttpResponseRedirect(url)
# set cache
- response['Last-Modified'] = http_datetime(url_item.last_modified())
+ response['Last-Modified'] = http_datetime(modified)
return response
def item (request, **opts) :