Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use caching with Django? (Preferably in GAE)

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.

like image 973
Emmet B Avatar asked Feb 05 '13 09:02

Emmet B


People also ask

How do I use Django cache?

To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.

Which mechanism is used to enable caching for an individual view in Django?

Using Memcached One of the most popular and efficient types of cache supported natively by Django is MEMCACHED .

How do I know if Django cache is working?

If cache. get() returns the set value it means that cache is working as it should. Otherwise it will return None . An other option is to start memcached with $ memcached -vv , since it will log all the cache accesses to the terminal.


1 Answers

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

like image 177
Nikolay Baluk Avatar answered Nov 10 '22 19:11

Nikolay Baluk