Software /
code /
prosody
Diff
plugins/mod_http_file_share.lua @ 11332:3e0dcdf6283e
mod_http_file_share: Cache file metadata
For faster access by avoiding archive ID.
No benchmarks were harmed in the making of this commit.
... no benchmarks were performed at all.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 28 Jan 2021 16:34:13 +0100 |
parent | 11331:7a915fa49373 |
child | 11333:f80056b97cf0 |
line wrap: on
line diff
--- a/plugins/mod_http_file_share.lua Thu Jan 28 16:23:38 2021 +0100 +++ b/plugins/mod_http_file_share.lua Thu Jan 28 16:34:13 2021 +0100 @@ -17,6 +17,7 @@ local dataform = require "util.dataforms".new; local dt = require "util.datetime"; local hi = require "util.human.units"; +local cache = require "util.cache"; local namespace = "urn:xmpp:http:upload:0"; @@ -55,6 +56,8 @@ filesizefmt = { type = "modify"; condition = "bad-request"; text = "File size must be positive integer"; } }); +local upload_cache = cache.new(1024); + -- Convenience wrapper for logging file sizes local function B(bytes) return hi.format(bytes, "B", "b"); end @@ -214,6 +217,13 @@ if uploaded then module:log("debug", "Upload of %q completed, %s", filename, B(final_size)); assert(os.rename(filename.."~", filename)); + + upload_cache:set(upload_info.slot, { + name = upload_info.filename; + size = tostring(upload_info.filesize); + type = upload_info.filetype; + time = os.time(); + }); return 201; else assert(os.remove(filename.."~")); @@ -226,21 +236,39 @@ function handle_download(event, path) -- GET /uploads/:slot+filename local request, response = event.request, event.response; local slot_id = path:match("^[^/]+"); - -- TODO cache local basename, filetime, filetype, filesize; - local slot, when = errors.coerce(uploads:get(nil, slot_id)); - if not slot then - module:log("debug", "uploads:get(%q) --> not-found, %s", slot_id, when); + local cached = upload_cache:get(slot_id); + if cached then + module:log("debug", "Cache hit"); + -- TODO stats (instead of logging?) + basename = cached.name; + filesize = cached.size; + filetype = cached.type; + filetime = cached.time; + upload_cache:set(slot_id, cached); + -- TODO cache negative hits? else - basename = slot.attr.filename; - filesize = slot.attr.size; - filetype = slot.attr["content-type"]; - filetime = when; + module:log("debug", "Cache miss"); + local slot, when = errors.coerce(uploads:get(nil, slot_id)); + if not slot then + module:log("debug", "uploads:get(%q) --> not-found, %s", slot_id, when); + else + module:log("debug", "uploads:get(%q) --> %s, %d", slot_id, slot, when); + basename = slot.attr.filename; + filesize = slot.attr.size; + filetype = slot.attr["content-type"]; + filetime = when; + upload_cache:set(slot_id, { + name = basename; + size = slot.attr.size; + type = filetype; + time = when; + }); + end end if not basename then return 404; end - module:log("debug", "uploads:get(%q) --> %s, %d", slot_id, slot, when); local last_modified = os.date('!%a, %d %b %Y %H:%M:%S GMT', filetime); if request.headers.if_modified_since == last_modified then return 304; @@ -292,6 +320,7 @@ for slot_id in iter do i = i + 1; obsolete_files:push(get_filename(slot_id)); + upload_cache:set(slot_id, nil); end sleep(0.1);