From: john Date: Fri, 18 Apr 2003 14:54:54 +0000 (+0000) Subject: use the MRUCache for pdfs. X-Git-Tag: BEFORE_MERGE_1_1~169 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=ec2f3b513913f585ebdde2ff106848a977421daf;p=mir.git use the MRUCache for pdfs. speeds things up a lot(takes like 2 seconds to generate a pdf, and tens of milliseconds to pull one out of the cache) --- diff --git a/source/mircoders/servlet/ServletModuleOpenIndy.java b/source/mircoders/servlet/ServletModuleOpenIndy.java index 9520b87d..55675b1b 100755 --- a/source/mircoders/servlet/ServletModuleOpenIndy.java +++ b/source/mircoders/servlet/ServletModuleOpenIndy.java @@ -94,7 +94,7 @@ import mir.util.HTTPRequestParser; import mir.util.StringRoutines; import mircoders.entity.EntityComment; import mircoders.entity.EntityContent; -import mircoders.global.CacheKey; +//import mircoders.global.CacheKey; import mircoders.global.MirGlobal; import mircoders.media.*; import mircoders.media.UnsupportedMediaFormatExc; @@ -127,7 +127,7 @@ import mircoders.storage.DatabaseTopics; * open-postings to the newswire * * @author mir-coders group - * @version $Id: ServletModuleOpenIndy.java,v 1.75 2003/04/18 14:01:41 john Exp $ + * @version $Id: ServletModuleOpenIndy.java,v 1.76 2003/04/18 14:54:54 john Exp $ * */ @@ -1084,53 +1084,71 @@ public class ServletModuleOpenIndy extends ServletModule public void getpdf(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + long starttime=System.currentTimeMillis(); String ID_REQUEST_PARAM = "id"; int maxArticlesInNewsleter = 15; // it is nice not to be dos'ed try { String idParam = req.getParameter(ID_REQUEST_PARAM); if (idParam != null) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - PDFGenerator pdfMaker = new PDFGenerator(out); + RE re = new RE("[0-9]+"); - - + + REMatch[] idMatches=re.getAllMatches(idParam); - + String cacheSelector=""; - + for (int i = 0; i < idMatches.length; i++){ cacheSelector= cacheSelector + "," + idMatches[i].toString(); } - - if (idMatches.length > 1){ - pdfMaker.addLine(); - for (int i = 0; i < idMatches.length && i < maxArticlesInNewsleter; i++){ - REMatch aMatch = idMatches[i]; - String id=aMatch.toString(); - EntityContent contentEnt = (EntityContent)contentModule.getById(id); - pdfMaker.addIndexItem(contentEnt); - - } - } - - for (int i = 0; i < idMatches.length; i++){ - REMatch aMatch = idMatches[i]; - - String id=aMatch.toString(); - - EntityContent contentEnt = (EntityContent)contentModule.getById(id); - pdfMaker.add(contentEnt); - - } - - pdfMaker.stop(); - res.setContentType("application/pdf"); - byte[] content = out.toByteArray(); - res.setContentLength(content.length); - res.getOutputStream().write(content); - res.getOutputStream().flush(); + String cacheType="pdf"; + + String theCacheKey = cacheType+":"+cacheSelector; + + byte[] thePDF; + + if (MirGlobal.mruCache().hasObject(theCacheKey)){ + logger.warn("fetching pdf from cache"); + thePDF = (byte[]) MirGlobal.mruCache().getObject(theCacheKey); + } + else { + logger.warn("generating pdf and caching it"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + PDFGenerator pdfMaker = new PDFGenerator(out); + + if (idMatches.length > 1){ + pdfMaker.addLine(); + for (int i = 0; i < idMatches.length && i < maxArticlesInNewsleter; i++){ + REMatch aMatch = idMatches[i]; + String id=aMatch.toString(); + EntityContent contentEnt = (EntityContent)contentModule.getById(id); + pdfMaker.addIndexItem(contentEnt); + } + } + + for (int i = 0; i < idMatches.length; i++){ + REMatch aMatch = idMatches[i]; + String id=aMatch.toString(); + EntityContent contentEnt = (EntityContent)contentModule.getById(id); + + pdfMaker.add(contentEnt); + } + + pdfMaker.stop(); + thePDF = out.toByteArray(); + + //and save all our hard work! + MirGlobal.mruCache().storeObject(theCacheKey,thePDF); + } + + res.setContentType("application/pdf"); + res.setContentLength(thePDF.length); + res.getOutputStream().write(thePDF); + res.getOutputStream().flush(); + String elapsedtime=(new Long(System.currentTimeMillis()-starttime)).toString(); + logger.warn("pdf retireval took "+elapsedtime + " milliseconds" ); } else {