Add message limit

This commit is contained in:
Joscha 2016-05-11 19:45:37 +02:00
parent b03c7c75a4
commit f38c62e8db
2 changed files with 18 additions and 6 deletions

View file

@ -7,7 +7,14 @@ class Messages():
Message storage class which preserves thread hierarchy. Message storage class which preserves thread hierarchy.
""" """
def __init__(self): def __init__(self, message_limit=500):
"""
message_limit - maximum amount of messages that will be stored at a time
None - no limit
"""
self.message_limit = message_limit
self._by_id = {} self._by_id = {}
self._by_parent = {} self._by_parent = {}
@ -47,6 +54,9 @@ class Messages():
self._by_parent[mes.parent] = [] self._by_parent[mes.parent] = []
self._by_parent[mes.parent].append(mes) self._by_parent[mes.parent].append(mes)
if self.message_limit and len(self._by_id) > self.message_limit:
self.remove(self.get_oldest().id)
def remove(self, mid): def remove(self, mid):
""" """
remove(message_id) -> None remove(message_id) -> None

View file

@ -26,11 +26,13 @@ class Room():
sessions - session data has changed sessions - session data has changed
""" """
def __init__(self, room, nick=None, password=None): def __init__(self, room, nick=None, password=None, message_limit=500):
""" """
room - name of the room to connect to room - name of the room to connect to
nick - nick to assume, None -> no nick nick - nick to assume, None -> no nick
password - room password (in case the room is private) password - room password (in case the room is private)
message_limit - maximum amount of messages that will be stored at a time
None - no limit
""" """
self.room = room self.room = room
@ -46,7 +48,7 @@ class Room():
self.ping_next = 0 self.ping_next = 0
self.ping_offset = 0 # difference between server and local time self.ping_offset = 0 # difference between server and local time
self._messages = messages.Messages() self._messages = messages.Messages(message_limit=message_limit)
self._sessions = sessions.Sessions() self._sessions = sessions.Sessions()
self._callbacks = callbacks.Callbacks() self._callbacks = callbacks.Callbacks()