Skip to main content
https://travis-ci.org/django-pylibmc/django-pylibmc.svg

This package provides a memcached cache backend for Django using pylibmc. You want to use pylibmc because it’s fast.

Do you need django-pylibmc?

Django has direct support for pylibmc. To use it, set you cache backend:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        'LOCATION': '127.0.0.1.11211',
    }
}

See the Django documentation for details about using this cache backend.

Two reasons to use django-pylibmc instead are:

  • You need to use the binary protocol

  • You need to use a username and password to access the memcached server (such as with Memcachier on Heroku).

Requirements

django-pylibmc requires pylibmc 1.4.1 or above. It supports Django 1.7 through 1.9, and Python versions 2.7, 3.4, and 3.5.

Installation

Get it from pypi:

pip install django-pylibmc

or github:

pip install -e git://github.com/django-pylibmc/django-pylibmc.git#egg=django-pylibmc

Usage

Your cache backend should look something like this:

CACHES = {
    'default': {
        'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
        'LOCATION': 'localhost:11211',
        'TIMEOUT': 500,
        'BINARY': True,
        'OPTIONS': {  # Maps to pylibmc "behaviors"
            'tcp_nodelay': True,
            'ketama': True
        }
    }
}

To use a memcached local socket connection, set LOCATION to the path to the file, i.e. '/var/run/memcached/memcached.sock'.

If you want to use the memcached binary protocol, set the BINARY key’s value to True as shown above. BINARY is False by default.

If you want to control pylibmc behaviors, use the OPTIONS. OPTIONS is an empty dict by default.

Pylibmc supports compression and the minimum size (in bytes) of values to compress can be set via the Django setting PYLIBMC_MIN_COMPRESS_LEN. The default is 0, which is disabled.

Pylibmc 1.3.0 and above allows to configure compression level, which can be set via the Django setting PYLIBMC_COMPRESS_LEVEL. It accepts the same values as the Python zlib module. Please note that pylibmc changed the default from 1 (Z_BEST_SPEED) to -1 (Z_DEFAULT_COMPRESSION) in 1.3.0.

Configuration with Environment Variables

Optionally, the memcached connection can be configured with environment variables (on platforms like Heroku). To do so, declare the following variables:

  • MEMCACHE_SERVERS

  • MEMCACHE_USERNAME

  • MEMCACHE_PASSWORD

Caching Timouts

When setting a cache value, memcache allows you to set an expiration for the value. Commonly, the value is set to a timeout in seconds. However, other values are allowed including Unix timestamps and 0 for “never expire”. The highest number of seconds is 30 days - more than that, and the value is treated like a timestamp.

Django instead tries to work with cache timeouts in seconds after the current time. 0 is treated as 0 seconds, meaning the item should expire immediately. A timeout of None signals that the item should not expire. There is some support for memcache-style Unix timestamps as well.

In the distant past (Django 1.3?), a timeout of 0 was converted to the default timeout.

The current django-pylibmc behaviour is to pass 0 to the backend, which should be interpreted as “never expire”. Omiting the timeout will get the Django default.

In the future, django-pylibmc will adopt the latest Django behaviour. The safest solution for your own code is to omit the timeout parameter (and get the default timeout), or set it to a timeout in seconds (less than 30 days). This way, your code will work when the Django behaviour is adopted. Avoid using a timeout of 0, None, or a negative number.

Testing

Install tox:

pip install tox

Run the tests like this:

tox

Changelog

0.6.1 - 2015-12-28

  • Supports Django 1.7 through 1.9

  • Supports Python 2.7, 3.4, and 3.5

0.6.0 - 2015-04-01

  • Requires pylibmc 1.4.1 or greater

  • Supports Django 1.4 through 1.8.

  • Supports Python 2.5 through 2.7, and Python 3.3 through 3.4

  • In Django 1.6 and higher, when the timeout is omitted, the default timeout is used, rather than set to “never expire”.

Release files for django-pylibmc 0.6.1

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

Source distribution (sdist)

Source distribution for django-pylibmc 0.6.1
File Size Uploaded
django-pylibmc-0.6.1.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-pylibmc 0.6.1
File Interpreter ABI Platform
django_pylibmc-0.6.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size:19.1 kB

Release files / django-pylibmc-0.6.1.tar.gz

Download URL django-pylibmc-0.6.1.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
02b591933a029eb552388cced713028f3c6cbb021639fc8de388bd1ca87981d4
BLAKE2b-256 checksum
How to use checksums
6d350cd2c2b72f6690aa0563544e11e9d121e31af343f828ce7d14622aa087d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_pylibmc-0.6.1-py2.py3-none-any.whl

Download URL django_pylibmc-0.6.1-py2.py3-none-any.whl
Size 8.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
9cffdee703aaf9ebc029d9dbdee8abdd0723564b95e4b2ac59e4a668b8e58f93
BLAKE2b-256 checksum
How to use checksums
1b2bcbc55be77062f783b2a28dcc0aa6bb8f55ebbbf2c186b76e58ea79e25ee5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files

0.6.0

2 release files

0.5.0

1 release file

0.2.3

1 release file

0.2.1

1 release file

0.2

1 release file

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