Commits

Jason R. Coombs  committed 6c0a9fd

Cache the channel listing because retrieval might be slow.

  • Participants
  • Parent commits 5370306
  • Tags 1101.9

Comments (0)

Files changed (1)

File pmxbot/logging.py

 import datetime
 import itertools
 import struct
+import threading
 
 import pytz
 
 		channel = channel.replace('#', '').lower()
 		self._message(channel, nick, msg)
 
+	def list_channels(self):
+		# channel listing can be expensive, so returned a cached result and
+		#  refresh the cache in the background.
+		def update_cache():
+			self._channel_cache = self._list_channels()
+		if not '_channel_cache' in vars(self):
+			update_cache()
+		else:
+			threading.Thread(target = update_cache).start()
+		return self._channel_cache
+
 init_logger = Logger.from_URI
 
 class SQLiteLogger(Logger, storage.SQLiteStorage):
 			alllines.extend(lines)
 		return matches
 
-	def list_channels(self):
+	def _list_channels(self):
 		query = "SELECT distinct channel from logs"
 		return (chan[0] for chan in self.db.execute(query).fetchall())
 
 			alllines.extend(context)
 		return matches
 
-	def list_channels(self):
+	def _list_channels(self):
 		return self.db.distinct('channel')
 
 	def last_message(self, channel):