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 expire in 10 seconds. Restart your Nameko services now and try this out. When you send a new message, wait 10 seconds and refresh the page, and it should be gone.