Changeset

9885:64e16d1e91f6

mod_storage_internal,_sql: Key item count cache on both username and store
author Kim Alvefur <zash@zash.se>
date Fri, 22 Mar 2019 18:02:27 +0100
parents 9884:9751c17f5281
children 9886:710a116341cd
files plugins/mod_storage_internal.lua plugins/mod_storage_sql.lua
diffstat 2 files changed, 19 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_storage_internal.lua	Fri Mar 22 18:01:24 2019 +0100
+++ b/plugins/mod_storage_internal.lua	Fri Mar 22 18:02:27 2019 +0100
@@ -5,6 +5,7 @@
 local st = require "util.stanza";
 local now = require "util.time".now;
 local id = require "util.id".medium;
+local jid_join = require "util.jid".join;
 
 local host = module.host;
 
@@ -64,7 +65,8 @@
 	value.attr.stamp = datetime.datetime(when);
 	value.attr.stamp_legacy = datetime.legacy(when);
 
-	local item_count = archive_item_count_cache:get(username);
+	local cache_key = jid_join(username, host, self.store);
+	local item_count = archive_item_count_cache:get(cache_key);
 
 	if key then
 		local items, err = datamanager.list_load(username, host, self.store);
@@ -72,7 +74,7 @@
 
 		-- Check the quota
 		item_count = items and #items or 0;
-		archive_item_count_cache:set(username, item_count);
+		archive_item_count_cache:set(cache_key, item_count);
 		if item_count >= archive_item_limit then
 			module:log("debug", "%s reached or over quota, not adding to store", username);
 			return nil, "quota-limit";
@@ -89,7 +91,7 @@
 			items:push(value);
 			local ok, err = datamanager.list_store(username, host, self.store, items);
 			if not ok then return ok, err; end
-			archive_item_count_cache:set(username, #items);
+			archive_item_count_cache:set(cache_key, #items);
 			return key;
 		end
 	else
@@ -98,7 +100,7 @@
 			local items, err = datamanager.list_load(username, host, self.store);
 			if not items and err then return items, err; end
 			item_count = items and #items or 0;
-			archive_item_count_cache:set(username, item_count);
+			archive_item_count_cache:set(cache_key, item_count);
 		end
 		if item_count >= archive_item_limit then
 			module:log("debug", "%s reached or over quota, not adding to store", username);
@@ -113,7 +115,7 @@
 
 	local ok, err = datamanager.list_append(username, host, self.store, value);
 	if not ok then return ok, err; end
-	archive_item_count_cache:set(username, item_count+1);
+	archive_item_count_cache:set(cache_key, item_count+1);
 	return key;
 end
 
@@ -195,8 +197,9 @@
 end
 
 function archive:delete(username, query)
+	local cache_key = jid_join(username, host, self.store);
 	if not query or next(query) == nil then
-		archive_item_count_cache:set(username, nil);
+		archive_item_count_cache:set(cache_key, nil);
 		return datamanager.list_store(username, host, self.store, nil);
 	end
 	local items, err = datamanager.list_load(username, host, self.store);
@@ -204,7 +207,7 @@
 		if err then
 			return items, err;
 		end
-		archive_item_count_cache:set(username, 0);
+		archive_item_count_cache:set(cache_key, 0);
 		-- Store is empty
 		return 0;
 	end
@@ -254,7 +257,7 @@
 	end
 	local ok, err = datamanager.list_store(username, host, self.store, items);
 	if not ok then return ok, err; end
-	archive_item_count_cache:set(username, #items);
+	archive_item_count_cache:set(cache_key, #items);
 	return count;
 end
 
--- a/plugins/mod_storage_sql.lua	Fri Mar 22 18:01:24 2019 +0100
+++ b/plugins/mod_storage_sql.lua	Fri Mar 22 18:02:27 2019 +0100
@@ -7,6 +7,7 @@
 local xml_parse = require "util.xml".parse;
 local uuid = require "util.uuid";
 local resolve_relative_path = require "util.paths".resolve_relative_path;
+local jid_join = require "util.jid".join;
 
 local is_stanza = require"util.stanza".is_stanza;
 local t_concat = table.concat;
@@ -237,7 +238,8 @@
 };
 archive_store.__index = archive_store
 function archive_store:append(username, key, value, when, with)
-	local item_count = archive_item_count_cache:get(username);
+	local cache_key = jid_join(username, host, self.store);
+	local item_count = archive_item_count_cache:get(cache_key);
 	if not item_count then
 		local ok, ret = engine:transaction(function()
 			local count_sql = [[
@@ -255,7 +257,7 @@
 			module:log("error", "Failed while checking quota for %s: %s", username, ret);
 			return nil, "Failure while checking quota";
 		end
-		archive_item_count_cache:set(username, item_count);
+		archive_item_count_cache:set(cache_key, item_count);
 	end
 
 	module:log("debug", "%s has %d items out of %d limit", username, item_count, archive_item_limit);
@@ -280,7 +282,7 @@
 			local result, err = engine:delete(delete_sql, host, user or "", store, key);
 			if result then
 				item_count = item_count - result:affected();
-				archive_item_count_cache:set(username, item_count);
+				archive_item_count_cache:set(cache_key, item_count);
 			end
 		else
 			item_count = item_count + 1;
@@ -288,7 +290,7 @@
 		end
 		local t, encoded_value = assert(serialize(value));
 		engine:insert(insert_sql, host, user or "", store, when, with, key, t, encoded_value);
-		archive_item_count_cache:set(username, item_count+1);
+		archive_item_count_cache:set(cache_key, item_count+1);
 		return key;
 	end);
 	if not ok then return ok, ret; end
@@ -460,7 +462,8 @@
 		end
 		return engine:delete(sql_query, unpack(args));
 	end);
-	archive_item_count_cache:set(username, nil);
+	local cache_key = jid_join(username, host, self.store);
+	archive_item_count_cache:set(cache_key, nil);
 	return ok and stmt:affected(), stmt;
 end