Comparison

plugins/muc/mod_muc.lua @ 7371:d5ba0dec0c95

MUC: Cache public rooms and their names to speed up disco#items
author Kim Alvefur <zash@zash.se>
date Mon, 18 Apr 2016 19:19:33 +0200
parent 7370:0ebc7ff1fff5
child 7372:b2d7e04eb922
comparison
equal deleted inserted replaced
7370:0ebc7ff1fff5 7371:d5ba0dec0c95
93 93
94 local persistent_rooms_storage = module:open_store("persistent"); 94 local persistent_rooms_storage = module:open_store("persistent");
95 local persistent_rooms = module:open_store("persistent", "map"); 95 local persistent_rooms = module:open_store("persistent", "map");
96 local room_configs = module:open_store("config"); 96 local room_configs = module:open_store("config");
97 97
98 local room_items_cache = {};
99
98 local function room_save(room, forced) 100 local function room_save(room, forced)
99 local node = jid_split(room.jid); 101 local node = jid_split(room.jid);
100 local is_persistent = persistent.get(room); 102 local is_persistent = persistent.get(room);
103 room_items_cache[room.jid] = room:get_public() and room:get_name() or nil;
101 if is_persistent or forced then 104 if is_persistent or forced then
102 persistent_rooms:set(nil, room.jid, true); 105 persistent_rooms:set(nil, room.jid, true);
103 local data = room:freeze(forced); 106 local data = room:freeze(forced);
104 return room_configs:set(node, data); 107 return room_configs:set(node, data);
105 else 108 else
145 148
146 function delete_room(room) 149 function delete_room(room)
147 module:log("debug", "Deleting %s", room); 150 module:log("debug", "Deleting %s", room);
148 room_configs:set(jid_split(room.jid), nil); 151 room_configs:set(jid_split(room.jid), nil);
149 persistent_rooms:set(nil, room.jid, nil); 152 persistent_rooms:set(nil, room.jid, nil);
153 room_items_cache[room.jid] = nil;
150 end 154 end
151 155
152 function module.unload() 156 function module.unload()
153 for room in rooms:values() do 157 for room in rooms:values() do
154 room:save(true); 158 room:save(true);
189 end 193 end
190 194
191 module:hook("host-disco-items", function(event) 195 module:hook("host-disco-items", function(event)
192 local reply = event.reply; 196 local reply = event.reply;
193 module:log("debug", "host-disco-items called"); 197 module:log("debug", "host-disco-items called");
194 for room in each_room() do 198 if next(room_items_cache) ~= nil then
195 if not room:get_hidden() then 199 for jid, room_name in pairs(room_items_cache) do
196 reply:tag("item", {jid=room.jid, name=room:get_name()}):up(); 200 reply:tag("item", { jid = jid, name = room_name }):up();
201 end
202 else
203 for room in each_room() do
204 if not room:get_hidden() then
205 local jid, room_name = room.jid, room:get_name();
206 room_items_cache[jid] = name;
207 reply:tag("item", { jid = jid, name = room_name }):up();
208 end
197 end 209 end
198 end 210 end
199 end); 211 end);
200 212
201 module:hook("muc-room-pre-create", function(event) 213 module:hook("muc-room-pre-create", function(event)