4.4. Settings Caches

4.4.1. DummyCache

  • For development and testing purposes

>>> 
... CACHES = {
...     'default': {
...         'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
...     }
... }

4.4.2. Database

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.db.DatabaseCache",
        "LOCATION": "django_cache",
    }
}
$ python manage.py createcachetable

4.4.3. Filesystem

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
        "LOCATION": "/var/tmp/django_cache",
    }
}

4.4.4. Local-memory

  • Not a good choice for production environments

  • This is the default cache if another is not specified in your settings file

  • This cache is per-process (see below) and thread-safe

  • If you only have one locmem cache, you can omit the LOCATION

  • If you have more than one local memory cache, you will need to assign a name to at least one of them in order to keep them separate

  • Each process will have its own private cache instance, which means no cross-process caching is possible

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
        "LOCATION": "unique-snowflake",
    }
}

4.4.5. PyLibMCCache

$ python -m pip install pylibmc
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
        "LOCATION": "unix:/tmp/memcached.sock",
    }
}
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
        "LOCATION": "127.0.0.1:11211",
    }
}
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
        "LOCATION": [
            "172.19.26.240:11211",
            "172.19.26.242:11212",
            "172.19.26.244:11213",
        ],
    }
}
>>> 
... CACHES = {
...     'default': {
...         'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
...         'LOCATION': '/data/memcached.sock',
...
...         # Use binary memcache protocol (needed for authentication)
...         'BINARY': True,
...
...         # TIMEOUT is not the connection timeout! It's the default expiration
...         # timeout that should be applied to keys! Setting it to `None`
...         # disables expiration.
...         'TIMEOUT': None,
...
...         'OPTIONS': {
...             # Enable faster IO
...             'tcp_nodelay': True,
...
...             # Keep connection alive
...             'tcp_keepalive': True,
...
...             # Timeout settings
...             'connect_timeout': 2000,  # ms
...             'send_timeout': 750 * 1000,  # us
...             'receive_timeout': 750 * 1000,  # us
...             '_poll_timeout': 2000,  # ms
...
...             # Better failover
...             'ketama': True,
...             'remove_failed': 1,
...             'retry_timeout': 2,
...             'dead_timeout': 30,
...         }
...     }
... }

4.4.6. Redis

$ python -m pip install redis
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
    }
}
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": "redis://username:password@127.0.0.1:6379",
    }
}
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": [
            "redis://127.0.0.1:6379",  # leader
            "redis://127.0.0.1:6378",  # read-replica 1
            "redis://127.0.0.1:6377",  # read-replica 2
        ],
    }
}