skip to Main Content

I am building a web-app using django channels. I also have a task function which i run periodically ( every 30s) using celery beat. This task function sends data to my web-socket every 30s.

Now, if a consumer joins my channel group, he has to wait for some time until my next data arrives. But what I want is to show the new consumer my previous data, until new one arrives. So i have to cache my previous data somewhere.

What is the correct way to do that? I know using redis but how in django-channels?

2

Answers


  1. It is quite simple.
    to append data to cache simply do:

    cache.set(<key>, <value>, <timeout>)
    

    I hope it works for you!.

    Login or Signup to reply.
  2. If you use AsyncConsumer, you should call cache methods with sync_to_async wrapper:

    await sync_to_async(cache.set)(<key>, <value>, <timeout>)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search