From: Radek Czajka Date: Thu, 12 Jan 2012 14:02:43 +0000 (+0100) Subject: update to Django 1.3; cache some api calls X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/e5f7721a8da48c8c77900a58333e945a63c7e116?ds=inline;hp=--cc update to Django 1.3; cache some api calls --- e5f7721a8da48c8c77900a58333e945a63c7e116 diff --git a/apps/api/handlers.py b/apps/api/handlers.py index 8f06deafc..eb15a4edd 100644 --- a/apps/api/handlers.py +++ b/apps/api/handlers.py @@ -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 diff --git a/requirements.txt b/requirements.txt index 4fd263ef3..bc8b138d9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/wolnelektury/settings.py b/wolnelektury/settings.py index 45dfe3709..0c5917e7e 100644 --- a/wolnelektury/settings.py +++ b/wolnelektury/settings.py @@ -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