Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

dj-pylibmc

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

This is a fork of the django-pylibmc package.

Do you need dj-pylibmc?

Django now has a built-in pylibmc backend, and as of Django 1.11 also supports the binary, username and password options natively. As such, in most cases the built-in backend should be used instead of dj-pylibmc, since it will be more actively maintained.

To use Django’s own backend, configure CACHES like so:

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.

Reasons to use dj-pylibmc instead, are:

  • You would like to use pylibmc’s compression feature

  • You would rather pylibmc connection/server exceptions be caught/logged and not raised (though this may be added upstream soon).

Requirements

dj-pylibmc requires pylibmc 1.4.1 or above. It supports Django 2.2 through 3.1, and Python versions >=3.6, <3.9

Installation

Get it from pypi:

pip install dj-pylibmc

or github:

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

Usage

Your cache backend should look something like this:

CACHES = {
    'default': {
        'BACKEND': 'dj_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.

Since 0.6.2 connections are long-living and not closed after each request. It could cause unwanted ConnectionErrors being raised by libmemcached when connection is broken. dj-pylibmc will make one more attempt when a request to memcached fails with ConnectionError. If this behavior doesn’t fit your needs, it can be disabled by setting DJPYLIBMC_RETRY_ON_BROKEN_CONNECTION Django setting to False.

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 dj-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, dj-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.4 - 2020-11-30

  • Correctly handles MemcachedError with no retcode attribute

  • Adds black formatiing

  • Supports Python 3.6 through 3.8

0.6.3 - 2020-10-21

  • Makes retry on ConnectionError

  • Drops support of old Python and Django version

  • Supports Django 2.2 through 3.1

  • Supports Python 3.5 through 3.8

0.6.2 - 2020-10-16

  • Supports Django 1.7 through 3.1

  • Supports Python 2.7 through 3.8

0.6.1 - 2015-12-28

  • Supports Django 1.7 through 1.11

  • 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”.

_(Older changes can be found in the full documentation)._

Metadata

Release files for dj-pylibmc 0.6.4

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

Source distribution (sdist)

Source distribution for dj-pylibmc 0.6.4
File Size Uploaded Provenance
dj-pylibmc-0.6.4.tar.gz 16.8 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 24.2 kB

Release files / dj-pylibmc-0.6.4.tar.gz

Download URL dj-pylibmc-0.6.4.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a546424e2a99ee68f158eba630d35d13e5917ae56969650a14a04c7310084c95
BLAKE2b-256 checksum
How to use checksums
1a3462b01a7b364debca448118ffb50698e57aff60da4e63724d9b88c02bc54c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

Release files / dj_pylibmc-0.6.4-py2.py3-none-any.whl

Download URL dj_pylibmc-0.6.4-py2.py3-none-any.whl
Size 7.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
881e083863f4a0c4cd480135c45292c0de1e572af667a3b475dc19e99b83116e
BLAKE2b-256 checksum
How to use checksums
a402ae644aef843cd3e342844467da1fd4f1336a7396ccc98bd195353128fad4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

Security for dj-pylibmc 0.6.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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