One thing you will quickly notice with the current state of our app is that the messages are not in any order at all. When you send a new message it could be inserted anywhere in the thread of messages, making our app pretty inconvenient, to say the least!
To remedy this, we will sort the messages by the amount of time left before they expire. First, we will have to amend our get_all_messages
method in our Redis dependency to also get the time-to-live for each message:
def get_all_messages(self): return [ { 'id': message_id, 'message': self.redis.get(message_id), 'expires_in': self.redis.pttl(message_id), } for message_id in self.redis.keys() ]
As you can see in the preceding code, we have added a new expires_in
value to each message. This uses the Redis PTTL command, which returns the time to live in milliseconds for a given key. Alternatively, we could also use the Redis TTL command, which returns...