Comparison

plugins/mod_blocklist.lua @ 6944:62b6f6d230f1

mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
author Kim Alvefur <zash@zash.se>
date Wed, 25 Nov 2015 21:06:01 +0100 (2015-11-25)
parent 6833:aeb088bb1a20
child 6967:d90a4d6a0e2c
comparison
equal deleted inserted replaced
6943:0a31ec3193f0 6944:62b6f6d230f1
17 local jid_split = require"util.jid".split; 17 local jid_split = require"util.jid".split;
18 18
19 local storage = module:open_store(); 19 local storage = module:open_store();
20 local sessions = prosody.hosts[module.host].sessions; 20 local sessions = prosody.hosts[module.host].sessions;
21 21
22 -- Cache of blocklists used since module was loaded 22 -- Cache of blocklists by username may randomly expire at any time
23 local cache = {}; 23 local cache = setmetatable({}, { __mode = "v" });
24 if module:get_option_boolean("blocklist_weak_cache") then 24
25 -- Lower memory usage, more IO and latency 25 -- Second level of caching, keeps a fixed number of items,
26 setmetatable(cache, { __mode = "v" }); 26 -- also anchors items in the above cache
27 end 27 local cache_size = module:get_option_number("blocklist_cache_size", 64);
28 local cache2 = require"util.cache".new(cache_size);
28 29
29 local null_blocklist = {}; 30 local null_blocklist = {};
30 31
31 module:add_feature("urn:xmpp:blocking"); 32 module:add_feature("urn:xmpp:blocking");
32 33
34 local ok, err = storage:set(username, blocklist); 35 local ok, err = storage:set(username, blocklist);
35 if not ok then 36 if not ok then
36 return ok, err; 37 return ok, err;
37 end 38 end
38 -- Successful save, update the cache 39 -- Successful save, update the cache
40 cache2:set(username, blocklist);
39 cache[username] = blocklist; 41 cache[username] = blocklist;
40 return true; 42 return true;
41 end 43 end
42 44
43 -- Migrates from the old mod_privacy storage 45 -- Migrates from the old mod_privacy storage
70 end 72 end
71 73
72 local function get_blocklist(username) 74 local function get_blocklist(username)
73 local blocklist = cache[username]; 75 local blocklist = cache[username];
74 if not blocklist then 76 if not blocklist then
77 blocklist = cache2:get(username);
78 end
79 if not blocklist then
75 if not user_exists(username, module.host) then 80 if not user_exists(username, module.host) then
76 return null_blocklist; 81 return null_blocklist;
77 end 82 end
78 blocklist = storage:get(username); 83 blocklist = storage:get(username);
79 if not blocklist then 84 if not blocklist then
80 blocklist = migrate_privacy_list(username); 85 blocklist = migrate_privacy_list(username);
81 end 86 end
82 cache[username] = blocklist; 87 cache2:set(username, blocklist);
83 end 88 end
89 cache[username] = blocklist;
84 return blocklist; 90 return blocklist;
85 end 91 end
86 92
87 module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event) 93 module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event)
88 local origin, stanza = event.origin, event.stanza; 94 local origin, stanza = event.origin, event.stanza;