django-redis
django-redis copied to clipboard
Difference between REDIS_CLIENT_CLASS and CLIENT_CLASS
When reading the documentation I can't see an explanation of the differences between CLIENT_CLASS
and REDIS_CLIENT_CLASS
. Is there a difference?
I was also confused by this while reading through the docs. From my reading/understanding of the source code, yes, there is a substantial difference:
-
CLIENT_CLASS
allows using a different implementation of the Django cache backend to e.g. support master/slave connections. This is the Pluggable clients section in the docs. -
REDIS_CLIENT_CLASS
allows using a different raw/low-level Redis client. The default is to use redis-py's own (redis.client.StrictRedis
), but you can use e.g.fakeredis.FakeStrictRedis
for testing scenarios. This is the Pluggable redis client section in the docs.
I believe the fact that both options use a CLIENT_CLASS nomenclature results confusing for those who are not familiar with the internals. Perhaps renaming the CLIENT_CLASS
option to something like CACHE_BACKEND_CLASS
would make things clearer.
@julen would you be interested in clearing out the doubt on the docs, maybe with a PR?
Maybe in a FAQ
section
I have no time to do so now, sorry. Feel free to reuse the words from my past comment if that's useful in some way.
This document provides an outline of the differences between REDIS_CLIENT_CLASS and CLIENT_CLASS settings in Django-Redis.
Introduction
Django-Redis is a full-featured Redis cache backend for Django that provides advanced configurations and flexibility. It allows you to plug in different clients and Redis client implementations to suit your specific requirements.
The two main settings that control these configurations are REDIS_CLIENT_CLASS and CLIENT_CLASS. Let's dive into the differences between them.
REDIS_CLIENT_CLASS
REDIS_CLIENT_CLASS is a setting that allows you to use a different raw/low-level Redis client. The default client used by Django-Redis is redis.client.StrictRedis provided by the redis-py library. However, you may want to use a different client in specific scenarios, such as using fakeredis.FakeStrictRedis for testing purposes.
To configure a different Redis client, you can set the REDIS_CLIENT_CLASS in your Django settings:
DJANGO_REDIS = {
"REDIS_CLIENT_CLASS": "fakeredis.FakeStrictRedis",
}
This setting is related to the "Pluggable Redis Client" section in the Django-Redis documentation: https://github.com/jazzband/django-redis/blob/b5fd77b/docs/index.rst#pluggable-redis-client.
CLIENT_CLASS
CLIENT_CLASS, on the other hand, allows you to use a different implementation of the Django cache backend. This can be useful if you need to support specific configurations, such as master/slave connections or other custom behaviors.
To configure a different cache backend client, you can set the CLIENT_CLASS in your Django settings:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "my_project.custom_clients.MyCustomClient",
},
},
}
This setting is related to the "Pluggable Cache Client" section in the Django-Redis documentation: https://github.com/jazzband/django-redis/blob/b5fd77b/docs/index.rst#pluggable-cache-client.
Conclusion
In summary, REDIS_CLIENT_CLASS and CLIENT_CLASS are two different settings in Django-Redis that allow you to customize the underlying Redis client and the cache backend implementation, respectively.
REDIS_CLIENT_CLASS is used to configure a different low-level Redis client, while CLIENT_CLASS is used to configure a different cache backend implementation. Both settings provide flexibility and enable you to adapt Django-Redis to your specific requirements and use cases.