Total Pageviews

Sunday, 1 June 2014

Django-gae-cache

Use Google App Engine as cache for your static resources!

This project contains Django middleware and Google App Engine application.

Motivation

I have few small web application on poor VPS (Virtual Private Server). Because there is everything on single server (Django, MySQL server, static content), I though how to lower server load/save memory/whatever. And this project is a result of my invention.
Using this application, you will save bandwidth, memory and CPU load of your web server, because all static resources will be served with Google App Engine.

Installation Instructions

These instructions was written for version 1.0.0, but following versions are backward compatible.

Download and install

Install package using distutils:
svn checkout http://django-gae-cache.googlecode.com/svn/trunk/ django-gae-cache
cd django-gae-cache
python setup.py install

Update your settings.py

... and add the middleware and installed apps settings:
# Something long and unpredictable
GAE_CACHE_SECRET_KEY = 'qwertyuiop'
# URL of your google app
GAE_CACHE_URL = 'http://yourapp.appspot.com'
# Turn on/off GAE cache
GAE_CACHE_USE = True

MIDDLEWARE_CLASSES = (
    ...
    'django_gae_cache.middleware.DjangoGaeCacheMiddleware',
    ...
)

INSTALLED_APPS = (
    ...
    'django_gae_cache',
    ...
)

Signup for Google app engine

... and download Google SDK to your computer.
http://code.google.com/appengine/

Configure GAE application

  1. Go to PACKAGE_DIR/gae-cache, copy _app.yaml to app.yaml and _config.py to config.py
  2. In app.yaml, replace YourAppName with name of your Google App Engine application.
  3. In config.py, fill SOURCE_URL with name of your site.
  4. Also set up SECRET_KEY to the same string, as is settings.GAE_CACHE_SECRET_KEY in your Django application.
  5. Also set MEDIA_URL to the same string, as is settings.MEDIA_URL in your Django application.

Deploy GAE application

In PACKAGE_DIR/gae-cache, run "appcfg.py update ." (appcfg.py is part of GAE SDK, which you downloaded in 3rd step).
  • Now visit http://yourapp.appspot.com. You should be redirected to your site or see 403 Forbidden (depends on gae-cache configuration). It is OK.
  • Find some existing resource file on your site (http://yourdjangosite.com/media_url/some_file.jpg and call them thru appspot: http://yourapp.appspot.com/media_url/some_file.jpg. Working? Great!

Usage

Caching mechanism is working out of the box. Immediately after setting up middleware and GAE application, your HTML pages are searched for hyperlinks to static resources and replaced by URL to the same resource using GAE application. On first call of resource, GAE application downloads static resource from your server and save it into memcache. On next request, resource is loaded from memcache, without request to your web server.

Invalidate cache

Sometimes you want to change or delete resource. You can invalidate one specific file or whole GAE cache:
  from django_gae_cache import api
  a = api.Api()

  a.Invalidate('/relative/path/to/resource.jpg')
  # or
  a.InvalidateAll()
That's all! You can use that for example in save()/delete() method of your Resource model.

Memcache principe

On GAE application, resources are stored in memcache structures (there is 10GB free quota on GAE, which is enough for middle-size site). Memcache will free the oldest unused resources, when there application need space for newer resource. It means, we don't care of any advanced cache management, because in memcache are the most useful items at every time.

from http://code.google.com/p/django-gae-cache/
http://django-gae-cache.googlecode.com/files/django-gae-cache-1.1.0.tgz