Expiring messages in Redis

We are now onto the last requirement for our app, expiring messages. Since we are using Redis to store our messages, this becomes a trivial task.

Let's look back at our save_message method in our Redis dependency. Redis' SET has some optional parameters; the two we are most interested in here are ex and px. Both allow us to set the expiry of the data we are about to save, with one difference: ex is in seconds and px is in milliseconds:

def save_message(self, message): 
    message_id = uuid4().hex 
    self.redis.set(message_id, message, ex=10) 
 
    return message_id 

In the preceding code, you can see that the only amendment to the code I've made is to add ex=10 to the redis.set method; this will cause all of our messages to ...

Get Python Programming Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.