diff mod_push2/mod_push2.lua @ 6337:0570f1e2edf4

mod_push2: Implement cache for user push registrations
author Matthew Wild <mwild1@gmail.com>
date Thu, 09 Oct 2025 14:17:11 +0100
parents 2488254c1bc2
children c9836434a9d5
line wrap: on
line diff
--- a/mod_push2/mod_push2.lua	Fri Oct 03 11:53:29 2025 +0100
+++ b/mod_push2/mod_push2.lua	Thu Oct 09 14:17:11 2025 +0100
@@ -20,6 +20,7 @@
 
 local host_sessions = prosody.hosts[module.host].sessions
 local push2_registrations = module:open_store("push2_registrations", "keyval")
+local push2_registrations_cache = require "prosody.util.cache".new(10);
 
 if _VERSION:match("5%.1") or _VERSION:match("5%.2") then
 	module:log("warn", "This module may behave incorrectly on Lua before 5.3. It is recommended to upgrade to a newer Lua version.")
@@ -114,6 +115,7 @@
 	if not push2_registrations:set(origin.username, registrations) then
 		origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
 	else
+		push2_registrations_cache:set(origin.username, registrations);
 		origin.push_registration_id = registration_id
 		origin.push_registration = push_registration
 		origin.first_hibernated_push = nil
@@ -136,6 +138,7 @@
 	if not push2_registrations:set(origin.username, registrations) then
 		origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
 	else
+		push2_registrations_cache:set(origin.username, nil);
 		origin.push_registration_id = nil
 		origin.push_registration = nil
 		origin.first_hibernated_push = nil
@@ -446,7 +449,11 @@
 local function get_push_settings(stanza, session)
 	local to = stanza.attr.to
 	local node = to and jid.split(to) or session.username
-	local user_push_services = push2_registrations:get(node)
+	local user_push_services = push2_registrations_cache:get(node);
+	if not user_push_services then
+		user_push_services = push2_registrations:get(node);
+		push2_registrations_cache:set(node, user_push_services);
+	end
 	return node, (user_push_services or {})
 end