9

I cannot make memcached work with GAE. When I use Google cache backend, following the tutorial on GAE website, the views are not cached. So I used caching the urls as suggested in Django tutorial (e.g:

`(r'^example$', cache_page(60*15)(views.example)),

then I get this:

File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/middleware/cache.py", line 205, in __init__
self.cache_timeout = self.cache.default_timeout

 AttributeError: 'Client' object has no attribute 'default_timeout'

AttributeError: 'Client' object has no attribute 'default_timeout', if I use google backend (django.core.cache.backends.memcached.MemcachedCache) I get

Error: ImproperlyConfigured: Error importing middleware django.contrib.sessions.middleware: "No module named memcache".

Here somebody asked previously about using Django caching backend and suggest to install python-memcached, I did that and it still does not work.

Somebody suggested to write backend for GAE. I could not quite understand. If the best response to this question will be explaining step by step how to write a backend very roughly, then I will accept that answer.

Emmet B
  • 5,341
  • 6
  • 34
  • 47
  • 1
    I think that you will find your answers in here: http://stackoverflow.com/questions/1138715/will-djangos-cache-modules-work-on-google-app-engine – nizz Feb 11 '13 at 04:11
  • thanks, well I went those answers many times before posting this question above. But it is really confusing as two best voted answers contradict themselves, and also it is an old discussion. – Emmet B Feb 13 '13 at 13:48

2 Answers2

4

Not all Django's functional works on App Engine. So, the functional you trying to use is inadmissible for App Engine Django library because of App Engine infrastructure restricts.

If I understand you correctly, you want to cache a whole page or in other words whole View's response? You can do this in that way (just example):

# Django on App Engine view example
from google.appengine.api import memcache
from django.http import HttpResponse

def cached_index_page(request):
  output_html = memcache.get('index_page')  # here we "take" from cache
  if output is not None:
    pass
  else:
    output_html = get_page_content()
    memcache.add('index_page', output_html, 60)  # here we "put" to cache" (timeout also here)
  HttpResponse(output_html)

For your purpose you may create Django's Middleware with auto caching any page you need.

Also make sure you removed all unrelated/not acceptable on App Engine stuff from configuration file. Considering help page (https://developers.google.com/appengine/articles/django), the minimal config looks like:

import os

# 'project' refers to the name of the module created with django-admin.py
ROOT_URLCONF = 'project.urls'

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
#    'django.contrib.sessions.middleware.SessionMiddleware',
#    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'google.appengine.ext.ndb.django_middleware.NdbDjangoMiddleware', # NoSQL db middleware
)

INSTALLED_APPS = (
#    'django.contrib.auth',
    'django.contrib.contenttypes',
#    'django.contrib.sessions',
    'django.contrib.sites',
)

ROOT_PATH = os.path.dirname(__file__)
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or
    # "C:/www/django/templates".  Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    ROOT_PATH + '/templates',
)

Remember, that App Engine has own native caching, e.g. The Python runtime environment caches imported modules between requests on a single web server and you can tell App Engine to cache the CGI handler script itself, in addition to imported modules.

Helpful links: https://developers.google.com/appengine/articles/django-nonrelhttps://developers.google.com/appengine/docs/python/tools/libraries27

Nikolay Baluk
  • 2,245
  • 1
  • 19
  • 22
0

IMHO adding yet another question on this doesn't make this any less confusing.. Django-nonrel ≤1.3 and Memcache on Google App Engine might have an answer to your question.

Community
  • 1
  • 1
Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72