If you have a message object like this:
And your key could be like this. Assume no sender can send more than 1 msg per second
You could also add globally unique serial IDs to messages or keys when msgs are created if you want.
How can I cache these so that I always have the most recent N messages from each sender?
Essentially a FIFO eviction policy so if N=10 and some sender sends 10 messages those live in the cache until that sender sends his 11th message. Then that sender's oldest message is evicted.