Skip to content

Commit bf7030f

Browse files
authored
Merge pull request CloudBotIRC#167 from linuxdaemon/gonzobot+autojoin-cache
Add cache to autojoin.py
2 parents 261cc6f + 445d2b2 commit bf7030f

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

plugins/autojoin.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio
2+
from collections import defaultdict
3+
from threading import RLock
24

35
from sqlalchemy import PrimaryKeyConstraint, Column, String, Table, and_
4-
from sqlalchemy.exc import IntegrityError
56

67
from cloudbot import hook
78
from cloudbot.util import database
@@ -14,40 +15,54 @@
1415
PrimaryKeyConstraint('conn', 'chan')
1516
)
1617

18+
chan_cache = defaultdict(set)
19+
db_lock = RLock()
20+
1721

1822
def get_channels(db, conn):
1923
return db.execute(table.select().where(table.c.conn == conn.name.casefold())).fetchall()
2024

2125

26+
@hook.on_start
27+
def load_cache(db):
28+
with db_lock:
29+
chan_cache.clear()
30+
for row in db.execute(table.select()):
31+
chan_cache[row['conn']].add(row['chan'])
32+
33+
2234
@asyncio.coroutine
2335
@hook.irc_raw('376')
24-
def do_joins(db, conn, async_call):
25-
chans = yield from async_call(get_channels, db, conn)
36+
def do_joins(conn):
2637
join_throttle = conn.config.get("join_throttle", 0.4)
27-
for chan in chans:
28-
conn.join(chan[1])
38+
for chan in chan_cache[conn.name]:
39+
conn.join(chan)
2940
yield from asyncio.sleep(join_throttle)
3041

3142

3243
@hook.irc_raw('JOIN', singlethread=True)
3344
def add_chan(db, conn, chan, nick):
34-
if nick.casefold() == conn.nick.casefold():
35-
try:
45+
chans = chan_cache[conn.name]
46+
chan = chan.casefold()
47+
if nick.casefold() == conn.nick.casefold() and chan not in chans:
48+
with db_lock:
3649
db.execute(table.insert().values(conn=conn.name.casefold(), chan=chan.casefold()))
3750
db.commit()
38-
except IntegrityError:
39-
pass
51+
52+
load_cache(db)
4053

4154

4255
@hook.irc_raw('PART', singlethread=True)
4356
def on_part(db, conn, chan, nick):
4457
if nick.casefold() == conn.nick.casefold():
45-
db.execute(table.delete().where(and_(table.c.conn == conn.name.casefold(), table.c.chan == chan.casefold())))
46-
db.commit()
58+
with db_lock:
59+
db.execute(
60+
table.delete().where(and_(table.c.conn == conn.name.casefold(), table.c.chan == chan.casefold())))
61+
db.commit()
62+
63+
load_cache(db)
4764

4865

4966
@hook.irc_raw('KICK', singlethread=True)
5067
def on_kick(db, conn, chan, target):
51-
if target.casefold() == conn.nick.casefold():
52-
db.execute(table.delete().where(and_(table.c.conn == conn.name.casefold(), table.c.chan == chan.casefold())))
53-
db.commit()
68+
on_part(db, conn, chan, target)

0 commit comments

Comments
 (0)