update to Django 1.3; cache some api calls
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Thu, 12 Jan 2012 14:02:43 +0000 (15:02 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Thu, 12 Jan 2012 14:02:43 +0000 (15:02 +0100)
apps/api/handlers.py
requirements.txt
wolnelektury/settings.py

index 8f06dea..eb15a4e 100644 (file)
@@ -7,6 +7,7 @@ import json
 
 from django.conf import settings
 from django.contrib.sites.models import Site
+from django.core.cache import get_cache
 from django.core.urlresolvers import reverse
 from piston.handler import AnonymousBaseHandler, BaseHandler
 from piston.utils import rc
@@ -532,6 +533,16 @@ class CatalogueHandler(BaseHandler):
     def changes(cls, request=None, since=0, until=None, book_fields=None,
                 tag_fields=None, tag_categories=None):
         until = cls.until(until)
+        since = int(since)
+
+        if not since:
+            cache = get_cache('api')
+            key = hash((book_fields, tag_fields, tag_categories,
+                    tuple(sorted(request.GET.items()))
+                  ))
+            value = cache.get(key)
+            if value is not None:
+                return value
 
         changes = {
             'time_checked': timestamp(until)
@@ -547,6 +558,10 @@ class CatalogueHandler(BaseHandler):
                 if field == 'time_checked':
                     continue
                 changes.setdefault(field, {})[model] = changes_by_type[model][field]
+
+        if not since:
+            cache.set(key, changes)
+
         return changes
 
 
index 4fd263e..bc8b138 100644 (file)
@@ -1,7 +1,7 @@
 --find-links=http://www.pythonware.com/products/pil/
 
 # django
-Django>=1.2.4,<1.3
+Django>=1.3,<1.4
 South>=0.7 # migrations for django
 django-pagination>=1.0
 django-rosetta>=0.5.3
index 45dfe37..0c5917e 100644 (file)
@@ -58,8 +58,8 @@ USE_I18N = True
 
 # Absolute path to the directory that holds media.
 # Example: "/home/media/media.lawrence.com/"
-MEDIA_ROOT = path.join(PROJECT_DIR, '../media')
-STATIC_ROOT = path.join(PROJECT_DIR, 'static')
+MEDIA_ROOT = path.join(PROJECT_DIR, '../media/')
+STATIC_ROOT = path.join(PROJECT_DIR, 'static/')
 
 # URL that handles the media served from MEDIA_ROOT. Make sure to use a
 # trailing slash if there is a path component (optional in other cases).
@@ -76,13 +76,13 @@ ADMIN_MEDIA_PREFIX = '/admin-media/'
 
 # List of callables that know how to import templates from various sources.
 TEMPLATE_LOADERS = [
-    'django.template.loaders.filesystem.load_template_source',
-    'django.template.loaders.app_directories.load_template_source',
-#     'django.template.loaders.eggs.load_template_source',
+    'django.template.loaders.filesystem.Loader',
+    'django.template.loaders.app_directories.Loader',
+#     'django.template.loaders.eggs.Loader',
 ]
 
 TEMPLATE_CONTEXT_PROCESSORS = (
-    'django.core.context_processors.auth',
+    'django.contrib.auth.context_processors.auth',
     'django.core.context_processors.debug',
     'django.core.context_processors.i18n',
     'django.core.context_processors.media',
@@ -153,8 +153,20 @@ INSTALLED_APPS = [
     'suggest',
 ]
 
-#CACHE_BACKEND = 'locmem:///?max_entries=3000'
-CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
+CACHES = {
+    'default': {
+        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
+        'LOCATION': [
+            '127.0.0.1:11211',
+        ]
+    },
+    'api': {
+        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
+        'LOCATION': '/srv/wolnelektury.pl/django_cache/',
+        'KEY_PREFIX': 'api',
+        'TIMEOUT': 86400,
+    },
+}
 CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True
 
 # CSS and JavaScript file groups