Using fakeredis in a Django development settings file?

355 Views Asked by At

My Django settings.py file contains the following configuration options:

# Caches

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': 'redis://redis:6379',
    }
}

# Queues

RQ_QUEUES = {
    'default': {
        'HOST': 'redis',
        'PORT': 6379,
        'DB': 0,
        'DEFAULT_TIMEOUT': 360,
    },
}

Both CACHES and RQ_QUEUES contain configuration details that point to a redis server.

Is it possible to reconfigure these settings to point to an instance of fakeredis instead ?

I have reviewed the fakeredis documentation and so far I have only seen examples where the redis connection is manually over-ridden, every time a call to redis is made.

It seems to me that when running tests, it would be much more convenient to simply point the Django CACHE location directly to fakeredis. Is this possible?

1

There are 1 best solutions below

0
On

You can starting fakeredis v2.10.1.

Change your cache settings:

from fakeredis import FakeConnection
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': [
            'redis://127.0.0.1:6379',
        ],
        'OPTIONS': { ### <<<---- here
            'connection_class': FakeConnection
        }
    }
}