Saving messages
Earlier, I introduced the Redis SET
method. This will allow us to save a message to Redis, but first, we need to create a new method in our dependency provider that will handle this.
We could simply create a new method that called redis.set(message_id, message)
, but how would we handle new message IDs? It would be a bit troublesome if we expected the user to input a new message ID for each message they wanted to send, right? An alternative is to have the message service generate a new random message ID before it calls the dependency provider, but that would clutter our service with logic that could be handled by the dependency itself.
We'll solve this by having the dependency create a random string to be used as the message ID.
Adding a save message method to our Redis client
In redis.py
, let's amend our imports to include uuid4
:
from uuid import uuid4
uuid4
generates us a unique random string that we can use for our message.
We can now add our new save_message
method to the RedisClient...