aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2018-12-16 02:56:11 +0100
committerKim Alvefur <zash@zash.se>2018-12-16 02:56:11 +0100
commitbb3d150756001ccc5a8e0721b7f242e78d1a0ae0 (patch)
tree1dd288f731ecb948d074e05874ccfc7ab57020f3 /core
parent9beff3d274b457408f4561fe75f156ccf2109fd7 (diff)
downloadprosody-bb3d150756001ccc5a8e0721b7f242e78d1a0ae0.tar.gz
prosody-bb3d150756001ccc5a8e0721b7f242e78d1a0ae0.zip
core.rostermanager: Cache rosters of offline users for faster access (fixes #1233)
(grafted from 42a3e3a2824822cef7640ac56d182c59bdd4224e)
Diffstat (limited to 'core')
-rw-r--r--core/rostermanager.lua22
1 files changed, 22 insertions, 0 deletions
diff --git a/core/rostermanager.lua b/core/rostermanager.lua
index 61b08002..7bfad0a0 100644
--- a/core/rostermanager.lua
+++ b/core/rostermanager.lua
@@ -12,6 +12,7 @@
local log = require "util.logger".init("rostermanager");
local new_id = require "util.id".short;
+local new_cache = require "util.cache".new;
local pairs = pairs;
local tostring = tostring;
@@ -111,6 +112,23 @@ local function load_roster(username, host)
else -- Attempt to load roster for non-loaded user
log("debug", "load_roster: loading for offline user: %s", jid);
end
+ local roster_cache = hosts[host] and hosts[host].roster_cache;
+ if not roster_cache then
+ if hosts[host] then
+ roster_cache = new_cache(1024);
+ hosts[host].roster_cache = roster_cache;
+ end
+ else
+ roster = roster_cache:get(jid);
+ if roster then
+ log("debug", "load_roster: cache hit");
+ roster_cache:set(jid, roster);
+ if user then user.roster = roster; end
+ return roster;
+ else
+ log("debug", "load_roster: cache miss, loading from storage");
+ end
+ end
local roster_store = storagemanager.open(host, "roster", "keyval");
local data, err = roster_store:get(username);
roster = data or {};
@@ -134,6 +152,10 @@ local function load_roster(username, host)
if not err then
hosts[host].events.fire_event("roster-load", { username = username, host = host, roster = roster });
end
+ if roster_cache and not user then
+ log("debug", "load_roster: caching loaded roster");
+ roster_cache:set(jid, roster);
+ end
return roster, err;
end