"""Recent message store for context."""
from cachetools import TTLCache
_store = TTLCache(maxsize=2000, ttl=3600)

def store(chat_id, msg_id, text, user_id):
    _store[(chat_id, msg_id)] = {"text": text, "user_id": user_id, "chat_id": chat_id}

def get_recent(chat_id, n=5):
    items = [(k, v) for k, v in _store.items() if k[0] == chat_id]
    return [v for _, v in sorted(items, key=lambda x: x[0][1])[-n:]]
