plugins/mod_http_file_share.lua
changeset 11352 f076199b4d38
parent 11351 5b3ad3c7fe47
child 11353 a219001b449d
--- a/plugins/mod_http_file_share.lua	Sun Jan 31 17:56:35 2021 +0100
+++ b/plugins/mod_http_file_share.lua	Sun Jan 31 17:56:49 2021 +0100
@@ -60,6 +60,7 @@
 });
 
 local upload_cache = cache.new(1024);
+local quota_cache = cache.new(1024);
 
 -- Convenience wrapper for logging file sizes
 local function B(bytes) return hi.format(bytes, "B", "b"); end
@@ -68,17 +69,23 @@
 	return dm.getpath(slot, module.host, module.name, "bin", create)
 end
 
--- TODO cache
 function get_daily_quota(uploader)
 	local now = os.time();
 	local max_age = now - 86400;
+	local cached = quota_cache:get(uploader);
+	if cached and cached.time > max_age then
+		return cached.size;
+	end
 	local iter, err = uploads:find(nil, {with = uploader; start = max_age });
 	if not iter then return iter, err; end
 	local total_bytes = 0;
-	for _, slot in iter do
+	local oldest_upload;
+	for _, slot, when in iter do
 		local size = tonumber(slot.attr.size);
 		if size then total_bytes = total_bytes + size; end
+		if not oldest_upload then oldest_upload = when; end
 	end
+	quota_cache:set(uploader, { time = oldest_upload or now, size = total_bytes });
 	return total_bytes;
 end
 
@@ -160,6 +167,9 @@
 		return true;
 	end
 
+	-- Invalidate cache
+	quota_cache:set(uploader, nil);
+
 	local authz = get_authz(uploader, filename, filesize, filetype, slot);
 	local slot_url = get_url(slot, filename);
 	local upload_url = slot_url;