0

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?

DigBick
  • 21
  • 4

2 Answers2

0

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

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

I hope it works for you!.

0

If you use AsyncConsumer, you should call cache methods with sync_to_async wrapper:

await sync_to_async(cache.set)(<key>, <value>, <timeout>)
Evgeni Shudzel
  • 231
  • 2
  • 5