Comparison

core/rostermanager.lua @ 10569:fdc2bae50100 0.11

core.rostermanager: Cache rosters of offline users for faster access (fixes #1233) (grafted from 42a3e3a2824822cef7640ac56d182c59bdd4224e)
author Kim Alvefur <zash@zash.se>
date Sun, 16 Dec 2018 02:56:11 +0100
parent 9303:72f0862978ef
child 10570:962efe23975d
comparison
equal deleted inserted replaced
10567:3df479098696 10569:fdc2bae50100
10 10
11 11
12 local log = require "util.logger".init("rostermanager"); 12 local log = require "util.logger".init("rostermanager");
13 13
14 local new_id = require "util.id".short; 14 local new_id = require "util.id".short;
15 local new_cache = require "util.cache".new;
15 16
16 local pairs = pairs; 17 local pairs = pairs;
17 local tostring = tostring; 18 local tostring = tostring;
18 local type = type; 19 local type = type;
19 20
109 if roster then return roster; end 110 if roster then return roster; end
110 log("debug", "load_roster: loading for new user: %s", jid); 111 log("debug", "load_roster: loading for new user: %s", jid);
111 else -- Attempt to load roster for non-loaded user 112 else -- Attempt to load roster for non-loaded user
112 log("debug", "load_roster: loading for offline user: %s", jid); 113 log("debug", "load_roster: loading for offline user: %s", jid);
113 end 114 end
115 local roster_cache = hosts[host] and hosts[host].roster_cache;
116 if not roster_cache then
117 if hosts[host] then
118 roster_cache = new_cache(1024);
119 hosts[host].roster_cache = roster_cache;
120 end
121 else
122 roster = roster_cache:get(jid);
123 if roster then
124 log("debug", "load_roster: cache hit");
125 roster_cache:set(jid, roster);
126 if user then user.roster = roster; end
127 return roster;
128 else
129 log("debug", "load_roster: cache miss, loading from storage");
130 end
131 end
114 local roster_store = storagemanager.open(host, "roster", "keyval"); 132 local roster_store = storagemanager.open(host, "roster", "keyval");
115 local data, err = roster_store:get(username); 133 local data, err = roster_store:get(username);
116 roster = data or {}; 134 roster = data or {};
117 if user then user.roster = roster; end 135 if user then user.roster = roster; end
118 local legacy_pending = roster.pending and type(roster.pending.subscription) ~= "string"; 136 local legacy_pending = roster.pending and type(roster.pending.subscription) ~= "string";
131 log("warn", "Could not remove self-contact from roster for %s", jid); 149 log("warn", "Could not remove self-contact from roster for %s", jid);
132 end 150 end
133 end 151 end
134 if not err then 152 if not err then
135 hosts[host].events.fire_event("roster-load", { username = username, host = host, roster = roster }); 153 hosts[host].events.fire_event("roster-load", { username = username, host = host, roster = roster });
154 end
155 if roster_cache and not user then
156 log("debug", "load_roster: caching loaded roster");
157 roster_cache:set(jid, roster);
136 end 158 end
137 return roster, err; 159 return roster, err;
138 end 160 end
139 161
140 function save_roster(username, host, roster, jid) 162 function save_roster(username, host, roster, jid)