From 5ca95c40887c88397d1d910a3d9c5a3fc97155fc Mon Sep 17 00:00:00 2001 From: Marco Vermeulen Date: Fri, 16 Oct 2015 13:59:56 +0300 Subject: [PATCH] FEATURE #371 - Cache candidate list on serverside. --- src/main/vertx/server.groovy | 38 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main/vertx/server.groovy b/src/main/vertx/server.groovy index 9e460054..e29e8df5 100644 --- a/src/main/vertx/server.groovy +++ b/src/main/vertx/server.groovy @@ -95,6 +95,8 @@ rm.get("/candidates") { req -> } } +candidateList = "Candidate List initialising. Please try again..." +TreeMap candidates = [:] rm.get("/candidates/list") { req -> def cmd = [action : "find", collection: "candidates", @@ -104,23 +106,25 @@ rm.get("/candidates/list") { req -> description: 1, websiteUrl : 1, name : 1]] - vertx.eventBus.send("mongo-persistor", cmd) { msg -> - TreeMap candidates = [:] - msg.body.results.each { - candidates.put( - it.candidate, - [ - header : header(it.name, it.default, it.websiteUrl), - description: paragraph(it.description), - footer : footer(it.candidate) - ] - ) - } - addPlainTextHeader req - def binding = [candidates: candidates] - def template = listCandidatesTemplate.make(binding) - req.response.end template.toString() - } + if(candidates.isEmpty()){ + vertx.eventBus.send("mongo-persistor", cmd) { msg -> + msg.body.results.each { + candidates.put( + it.candidate, + [ + header : header(it.name, it.default, it.websiteUrl), + description: paragraph(it.description), + footer : footer(it.candidate) + ] + ) + } + def binding = [candidates: candidates] + def template = listCandidatesTemplate.make(binding) + candidateList = template.toString() + } + } + addPlainTextHeader req + req.response.end candidateList } PARAGRAPH_WIDTH = 80