Skip to content

epicserve/django-cache-url

Repository files navigation

django-cache-url

PyPI version Supported Python versions Tests

This simple Django utility allows you to utilize the 12factor inspired CACHE_URL environment variable to configure your Django application.

This was built with inspiration from rdegges' django-heroku-memcacheify as a way to use CACHE_URL in apps that aren't necessarily hosted on Heroku.

The internals borrow heavily from kennethreitz's dj-database-url.

Usage

Configure your cache in settings.py from CACHE_URL:

import django_cache_url
CACHES = {'default': django_cache_url.config()}

Defaults to local memory cache if CACHE_URL isn't set.

Parse an arbitrary Cache URL:

CACHES = {'default': django_cache_url.parse('memcache:https://...')}

Supported Caches

Support currently exists for:

  • locmem (default): 'locmem:https://[NAME]'
  • db: 'db:https://TABLE_NAME'
  • dummy: 'dummy:https://'
  • file: 'file:https:///PATH/TO/FILE'
  • memcached: 'memcached:https://HOST:PORT' [1]
  • pymemcached: 'pymemcached:https://HOST:PORT' For use with the python-memcached library. Useful if you're using Ubuntu <= 10.04.
  • pymemcache: 'pymemcache:https://HOST:PORT' For use with the pymemcache library.
  • djangopylibmc: 'djangopylibmc:https://HOST:PORT' For use with SASL based setups such as Heroku.
  • redis: 'redis:https://[USER:PASSWORD@]HOST:PORT[/DB]' or 'redis:https:///PATH/TO/SOCKET[/DB]' For use with django-redis. To use django-redis-cache just add ?lib=redis-cache to the URL.
  • rediss: 'rediss:https://[USER:PASSWORD@]HOST:PORT[/DB]' For use with django-redis.
  • hiredis: 'hiredis:https://[USER:PASSWORD@]HOST:PORT[/DB]' or 'hiredis:https:///PATH/TO/SOCKET[/DB]' For use with django-redis library using HiredisParser.
  • uwsgicache: 'uwsgicache:https://[CACHENAME]' For use with django-uwsgi-cache. Fallbacks to locmem if not running on uWSGI server.

All cache urls support optional cache arguments by using a query string, e.g.: 'memcached:https://HOST:PORT?key_prefix=site1'. See the Django cache arguments documentation.

[1]To specify multiple instances, separate the the HOST:PORT` pair by commas, e.g: 'memcached:https://HOST1:PORT1,HOST2:PORT2

Installation

Installation is simple too:

$ pip install django-cache-url

Tests

To run the tests install tox:

pip install tox

Then run them with:

make test