pythonsqlalchemytwistedirc

Python IRC bot + SQLAlchemy - track users in multiple channels


I'm currently storing IRC users in a dictionary, using their nickname as the 'key' for easy retrieval. The users are retrieved from SQLAlchemy, so users['deepy'] is an SQLAlchemy object which I regularly sync with my database.

Now the problem I have is that on IRC, people can be in many channels and I'm just keeping track of one. I need a suggestion on how to improve this.

I've been thinking about doing pretty much the same, but also storing the channel's users (as a list) in a dictionary with the channel names as key, so like:

{ '#two': ['reference to user9', 'reference to user62'], '#one': ['reference to user1', 'reference to user2'] }

The references being to the users dictionary which contains the SQLAlchemy object.

Is that a sensible approach?

I am using Python 2.7, PostgreSQL, SQLAlchemy and Twisted's irc.ircclient.


Solution

  • I've found that it's best to store as few SQLALchemy objects as possible. Storing a ton of SQLAlchemy objects leads to having to worry about synchronization issues and uses up memory.

    I would keep track of usernames or user ids instead of actual user objects:

    { '#two': ['bob1', 'tom2'], '#one': ['bob1', 'mary1'] }
    

    Then whenever I needed information for a user I would fetch them from the database. If I had only a few users and needed to access them frequently then I would create a dictionary that mapped usernames to SQLAlchemy user objects.