Skip to main content

Djanginxed

Django Nginx Memcached integration.

Provides a view decorator caching content in Memcached for easy retrieval via Nginx. The cache is keyed by md5 of full request path (which includes GET parameters).

Installation

  1. Install or add djanginxed to your Python path.

  2. Setup Memcached appropriately as described in Django’s cache framework docs.

  3. Optionally, set the CACHE_MIDDLEWARE_KEY_PREFIX setting in your Django’s settings file – If the cache is shared across multiple sites using the same Django installation, set this to the name of the site, or some other string that is unique to the Django instance, to prevent key collisions:

    CACHE_MIDDLEWARE_KEY_PREFIX = "site1"
  4. Install Nginx with the set_hash module. This module is required to compute md5 cache keys from within Nginx, i.e.:

    set_md5 $memcached_key $request_uri;
  5. Configure Nginx for direct Memcached page retrieval, i.e:

    location / {
        default_type       text/html;
        set_md5 $memcached_key $request_uri;
        memcached_pass     127.0.0.1:11211;
        error_page         404 405 500 @django;
    }
    
    location @django {
        fastcgi_pass    127.0.0.1:7000;
        fastcgi_param   GATEWAY_INTERFACE   CGI/1.1;
        fastcgi_param   DOCUMENT_URI        $document_uri;
        fastcgi_param   DOCUMENT_ROOT       $document_root;
        fastcgi_param   REQUEST_METHOD      $request_method;
        fastcgi_param   REQUEST_URI         $request_uri;
        fastcgi_param   REMOTE_ADDR         $remote_addr;
        fastcgi_param   REMOTE_PORT         $remote_port;
        fastcgi_param   QUERY_STRING        $query_string;
        fastcgi_param   CONTENT_TYPE        $content_type;
        fastcgi_param   CONTENT_LENGTH      $content_length;
        fastcgi_param   SERVER_ADDR         $server_addr;
        fastcgi_param   SERVER_PROTOCOL     $server_protocol;
        fastcgi_param   SERVER_PORT         $server_port;
        fastcgi_param   SERVER_NAME         $server_name;
        fastcgi_param   SERVER_SOFTWARE     nginx/$nginx_version;
        fastcgi_param   PATH_INFO           $fastcgi_script_name;
    }
  6. Optionally, when using a cache key prefix, include it during Nginx $memcached_key generation:

    set_md5 $memcached_key site1$request_uri;

Usage

Decorators

djanginxed.decorators.cache.cache_page

The cache_page decorator caches view response content in Memcached suitable for lookup by Nginx. cache_page takes a single argument: the cache timeout, in seconds.

Example:

from djanginxed.decorators.cache import cache_page

@cache_page(60 * 15)
def my_view(request):
    ...

This will cache the view’s response string in Memcached for 15 minutes (60 * 15), with the cache key generated from the full request path.

NOTE: The resulting HttpResponse object’s content value is stored in Memcached and not the actual HttpResponse object.

cache_page can also take an optional keyword argument, key_prefix, which works in the same way as the CACHE_MIDDLEWARE_KEY_PREFIX setting for the middleware. It can be used like this:

@cache_page(60 * 15, key_prefix="site1")
def my_view(request):
    ...

Authors

  • Shaun Sephton

Changelog

0.0.5

  1. Added ability to use custom cache key generator.

0.0.4

  1. Tests and polish.

0.0.1

  1. Initial release.

Release files for djanginxed 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djanginxed 0.0.5
File Size Uploaded
djanginxed-0.0.5.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for djanginxed 0.0.5
File Interpreter ABI Platform
djanginxed-0.0.5-py2.6.egg Legacy Egg format - - Details

Total release size: 13.0 kB

Release files / djanginxed-0.0.5.tar.gz

Download URL djanginxed-0.0.5.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e13a0ce1177a79e97d98311e1066b3c08428e154c7fbad2cc17596bcb3b80dc0
BLAKE2b-256 checksum
How to use checksums
0b53c0fc6bfc62951e96ad90d40d3819d8bcfbc02fa3439e66c18899a6c16aaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / djanginxed-0.0.5-py2.6.egg

Download URL djanginxed-0.0.5-py2.6.egg
Size 7.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
ec4d65b7a3521da18d7e4a2902530f8853a332d00e19c3f06707c4defdb02e63
BLAKE2b-256 checksum
How to use checksums
137695573c8392928b8f1f1817b3c8a1f7db30c3bf1dfc5540af7eba88ef1f1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page