comparison mod_http_muc_log/mod_http_muc_log.lua @ 2685:cd5781ca782d

mod_http_muc_log: Remove caching
author Kim Alvefur <zash@zash.se>
date Thu, 13 Apr 2017 22:47:09 +0200
parents b61b0ff1c0f9
children a6d19aea9b73
comparison
equal deleted inserted replaced
2684:e491a15d7621 2685:cd5781ca782d
1 local mt = require"util.multitable"; 1 local mt = require"util.multitable";
2 local datetime = require"util.datetime"; 2 local datetime = require"util.datetime";
3 local jid_split = require"util.jid".split; 3 local jid_split = require"util.jid".split;
4 local nodeprep = require"util.encodings".stringprep.nodeprep; 4 local nodeprep = require"util.encodings".stringprep.nodeprep;
5 local uuid = require"util.uuid".generate;
6 local it = require"util.iterators"; 5 local it = require"util.iterators";
7 local gettime = require"socket".gettime;
8 local url = require"socket.url"; 6 local url = require"socket.url";
9 local os_time, os_date = os.time, os.date; 7 local os_time, os_date = os.time, os.date;
10 local render = require"util.interpolation".new("%b{}", require"util.stanza".xml_escape); 8 local render = require"util.interpolation".new("%b{}", require"util.stanza".xml_escape);
11 9
12 local archive = module:open_store("muc_log", "archive"); 10 local archive = module:open_store("muc_log", "archive");
138 if i > 1 and tmp.wday == 2 then 136 if i > 1 and tmp.wday == 2 then
139 days = {}; 137 days = {};
140 weeks[#weeks+1] = { days = days }; 138 weeks[#weeks+1] = { days = days };
141 current_day = 1; 139 current_day = 1;
142 end 140 end
143 days[current_day], current_day = { wday = tmp.wday, day = i, href = days_t[i] and datetime.date(days_t[i]) }, current_day+1; 141 days[current_day] = {
142 wday = tmp.wday, day = i, href = days_t[i] and datetime.date(days_t[i])
143 };
144 current_day = current_day+1;
144 end 145 end
145 end 146 end
146 table.sort(year, sort_m); 147 table.sort(year, sort_m);
147 end 148 end
148 table.sort(years, sort_Y); 149 table.sort(years, sort_Y);
275 jid = module.host; 276 jid = module.host;
276 rooms = room_list; 277 rooms = room_list;
277 }); 278 });
278 end 279 end
279 280
280 local cache = setmetatable({}, {__mode = 'v'});
281
282 local function with_cache(f)
283 return function (event, path)
284 local request, response = event.request, event.response;
285 local ckey = path or "";
286 local cached = cache[ckey];
287
288 if cached then
289 local etag = cached.etag;
290 local if_none_match = request.headers.if_none_match;
291 if etag == if_none_match then
292 module:log("debug", "Client cache hit");
293 return 304;
294 end
295 module:log("debug", "Server cache hit");
296 response.headers.etag = etag;
297 response.headers.content_type = "text/html; charset=utf-8";
298 return cached[1];
299 end
300
301 local start = gettime();
302 local rendered = f(event, path);
303 module:log("debug", "Rendering took %dms", math.floor( (gettime() - start) * 1000 + 0.5));
304
305 if type(rendered) == "string" then
306 local etag = uuid();
307 cached = { rendered, etag = etag, date = datetime.date() };
308 response.headers.etag = etag;
309 cache[ckey] = cached;
310 end
311
312 response.headers.content_type = "text/html; charset=utf-8";
313 return rendered;
314 end
315 end
316
317 -- How is cache invalidation a hard problem? ;)
318 module:hook("muc-broadcast-message", function (event)
319 local room = event.room;
320 local room_name = jid_split(room.jid);
321 local today = datetime.date();
322 cache[get_link(room_name)] = nil;
323 cache[get_link(room_name, today)] = nil;
324 end);
325
326 module:provides("http", { 281 module:provides("http", {
327 route = { 282 route = {
328 ["GET /"] = list_rooms; 283 ["GET /"] = list_rooms;
329 ["GET /*"] = with_cache(logs_page); 284 ["GET /*"] = logs_page;
330 }; 285 };
331 }); 286 });
332 287