From: john Date: Sun, 4 May 2003 11:42:07 +0000 (+0000) Subject: put the MRUCache stuff back in.... X-Git-Tag: BEFORE_MERGE_1_1~116 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=00bfd76517d039070b0e126b8f4f200e8b692ed0;p=mir.git put the MRUCache stuff back in.... --- diff --git a/source/mircoders/servlet/ServletModuleOpenIndy.java b/source/mircoders/servlet/ServletModuleOpenIndy.java index 9e7f5be4..2c7ddc71 100755 --- a/source/mircoders/servlet/ServletModuleOpenIndy.java +++ b/source/mircoders/servlet/ServletModuleOpenIndy.java @@ -91,6 +91,7 @@ import mir.util.HTTPRequestParser; import mir.util.StringRoutines; import mircoders.entity.EntityComment; import mircoders.entity.EntityContent; +import mircoders.global.CacheKey; import mircoders.global.MirGlobal; import mircoders.media.MediaUploadProcessor; import mircoders.module.ModuleComment; @@ -122,7 +123,7 @@ import mircoders.storage.DatabaseTopics; * open-postings to the newswire * * @author mir-coders group - * @version $Id: ServletModuleOpenIndy.java,v 1.84 2003/04/29 02:36:50 zapata Exp $ + * @version $Id: ServletModuleOpenIndy.java,v 1.85 2003/05/04 11:42:07 john Exp $ * */ @@ -623,6 +624,10 @@ public class ServletModuleOpenIndy extends ServletModule catch (Throwable e){ throw new ServletModuleFailure("Couldn't get content for article "+aid + ": " + e.getMessage(), e); } + + + + String producerStorageRoot=configuration.getString("Producer.StorageRoot"); String producerDocRoot=configuration.getString("Producer.DocRoot"); String publishPath = contentEnt.getValue("publish_path"); @@ -989,49 +994,77 @@ public class ServletModuleOpenIndy extends ServletModule * Method for dynamically generating a pdf using iText */ - public void getpdf(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + 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); - - if (idMatches.length > 1) { - pdfMaker.addLine(); - for (int i = 0; i < idMatches.length; 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(); + + + 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(); + } + + String cacheType="pdf"; + + CacheKey theCacheKey = new CacheKey(cacheType,cacheSelector); + + byte[] thePDF; + + if (MirGlobal.mruCache().hasObject(theCacheKey)){ + logger.info("fetching pdf from cache"); + thePDF = (byte[]) MirGlobal.mruCache().getObject(theCacheKey); + } + else { + logger.info("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.info("pdf retireval took "+elapsedtime + " milliseconds" ); } else { - throw new ServletModuleExc("Missing id."); + throw new ServletModuleExc("Missing id."); } } catch (Throwable t) { @@ -1040,6 +1073,7 @@ public class ServletModuleOpenIndy extends ServletModule } } + public String generateOnetimePassword() { Random r = new Random();