Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USE_REDIS_CACHE with Django 4.0's native Redis backend? #571

Open
rstmsn opened this issue Feb 14, 2023 · 5 comments
Open

USE_REDIS_CACHE with Django 4.0's native Redis backend? #571

rstmsn opened this issue Feb 14, 2023 · 5 comments

Comments

@rstmsn
Copy link

rstmsn commented Feb 14, 2023

Django has native support for Redis since version 4.0 via the django.core.cache.backends.redis.RedisCache backend.

If I create a Django cache, as follows:

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

Is it possible to configure django-rq to use this redis connection, using the USE_REDIS_CACHE option?

@obi-081
Copy link

obi-081 commented Apr 11, 2023

I'm interested too.
I'll try next week locally and then on a dev server.
If it works, I'll put a message here.

@obi-081
Copy link

obi-081 commented Apr 12, 2023

Ok, it fails indeed. I got this stacktrace:

Traceback (most recent call last):
   File "/app/manage.py", line 25, in <module>
     main()
   File "/app/manage.py", line 21, in main
     execute_from_command_line(sys.argv)
   File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
     utility.execute()
   File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 440, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 402, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 448, in execute
     output = self.handle(*args, **options)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/management/commands/rqworker.py", line 120, in handle
     w = get_worker(*args, **worker_kwargs)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/workers.py", line 46, in get_worker
     queues = get_queues(*queue_names, **{'job_class': job_class,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/queues.py", line 208, in get_queues
     return [get_queue(*queue_names, **kwargs)]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/queues.py", line 165, in get_queue
     connection = get_connection(name)
                  ^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/queues.py", line 142, in get_connection
     return get_redis_connection(QUEUES[name], use_strict_redis)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/queues.py", line 97, in get_redis_connection
     return get_redis(config['USE_REDIS_CACHE'])
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_redis/__init__.py", line 15, in get_redis_connection
     raise NotImplementedError("This backend does not support this feature")
 NotImplementedError: This backend does not support this feature

So, I changed the django-rq settings like this:

RQ_QUEUES = {
     BACKGROUND_QUEUE: {
-        "USE_REDIS_CACHE": "default",
+        "HOST": REDIS_HOST,
+        "PORT": REDIS_PORT,
+        "USERNAME": REDIS_USERNAME,
+        "PASSWORD": REDIS_PASSWORD,
+        "DEFAULT_TIMEOUT": BACKGROUND_TIMEOUT,
     },
 }

And everything works fine.
So the workaround is clean and simple for now.

@selwin
Copy link
Collaborator

selwin commented May 2, 2023

@rstmsn @obi-jerome I'd love to have a PR to implement this.

@mohamed99elsokary
Copy link

@selwin @rstmsn @obi-jerome can you guys please give me a hint "What is that changes?"

@obi-081
Copy link

obi-081 commented Nov 21, 2023

@mohamed99elsokary
Before Django 4.0 we used django-redis to get a redis cache.
And django-rq could be simply configured then by setting "USE_REDIS_CACHE": "default".
Now we configure our cache with the new native Django Redis cache backend django.core.cache.backends.redis.RedisCache
But it is not declared as supported by django-rq, so we configured Redis manually for django-rq.

The native cache backend allows us to have one less dependency and to rely on the work of the Django Project that will probably give us a cache backend that is more reliable, integrated and performant, I guess.

It would be cool if django-rq was compatible with the Native Redis Backend configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants