Software /
code /
prosody
Comparison
plugins/mod_http_file_share.lua @ 11349:a219001b449d
mod_http_file_share: Update cached value while it is reasonably fresh
This should ensure that cache entries until the oldest file that counted
to the last 24h becomes older than 24h.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 31 Jan 2021 17:44:19 +0100 |
parent | 11348:f076199b4d38 |
child | 11350:3287dbdde33e |
comparison
equal
deleted
inserted
replaced
11348:f076199b4d38 | 11349:a219001b449d |
---|---|
77 return cached.size; | 77 return cached.size; |
78 end | 78 end |
79 local iter, err = uploads:find(nil, {with = uploader; start = max_age }); | 79 local iter, err = uploads:find(nil, {with = uploader; start = max_age }); |
80 if not iter then return iter, err; end | 80 if not iter then return iter, err; end |
81 local total_bytes = 0; | 81 local total_bytes = 0; |
82 local oldest_upload; | 82 local oldest_upload = now; |
83 for _, slot, when in iter do | 83 for _, slot, when in iter do |
84 local size = tonumber(slot.attr.size); | 84 local size = tonumber(slot.attr.size); |
85 if size then total_bytes = total_bytes + size; end | 85 if size then total_bytes = total_bytes + size; end |
86 if not oldest_upload then oldest_upload = when; end | 86 if when < oldest_upload then oldest_upload = when; end |
87 end | 87 end |
88 quota_cache:set(uploader, { time = oldest_upload or now, size = total_bytes }); | 88 -- If there were no uploads then we end up caching [now, 0], which is fine |
89 -- since we increase the size on new uploads | |
90 quota_cache:set(uploader, { time = oldest_upload, size = total_bytes }); | |
89 return total_bytes; | 91 return total_bytes; |
90 end | 92 end |
91 | 93 |
92 function may_upload(uploader, filename, filesize, filetype) -- > boolean, error | 94 function may_upload(uploader, filename, filesize, filetype) -- > boolean, error |
93 local uploader_host = jid.host(uploader); | 95 local uploader_host = jid.host(uploader); |
165 if not slot then | 167 if not slot then |
166 origin.send(st.error_reply(stanza, storage_err)); | 168 origin.send(st.error_reply(stanza, storage_err)); |
167 return true; | 169 return true; |
168 end | 170 end |
169 | 171 |
170 -- Invalidate cache | 172 local cached_quota = quota_cache:get(uploader); |
171 quota_cache:set(uploader, nil); | 173 if cached_quota and cached_quota.time > os.time()-86400 then |
174 cached_quota.size = cached_quota.size + filesize; | |
175 quota_cache:set(uploader, cached_quota); | |
176 end | |
172 | 177 |
173 local authz = get_authz(uploader, filename, filesize, filetype, slot); | 178 local authz = get_authz(uploader, filename, filesize, filetype, slot); |
174 local slot_url = get_url(slot, filename); | 179 local slot_url = get_url(slot, filename); |
175 local upload_url = slot_url; | 180 local upload_url = slot_url; |
176 | 181 |