use the MRUCache for pdfs.
authorjohn <john>
Fri, 18 Apr 2003 14:54:54 +0000 (14:54 +0000)
committerjohn <john>
Fri, 18 Apr 2003 14:54:54 +0000 (14:54 +0000)
speeds things up a lot(takes like 2 seconds to generate a pdf, and tens of milliseconds
to pull one out of the cache)

source/mircoders/servlet/ServletModuleOpenIndy.java

index 9520b87..55675b1 100755 (executable)
@@ -94,7 +94,7 @@ import mir.util.HTTPRequestParser;
 import mir.util.StringRoutines;\r
 import mircoders.entity.EntityComment;\r
 import mircoders.entity.EntityContent;\r
-import mircoders.global.CacheKey;\r
+//import mircoders.global.CacheKey;\r
 import mircoders.global.MirGlobal;\r
 import mircoders.media.*;\r
 import mircoders.media.UnsupportedMediaFormatExc;\r
@@ -127,7 +127,7 @@ import mircoders.storage.DatabaseTopics;
  *    open-postings to the newswire\r
  *\r
  * @author mir-coders group\r
- * @version $Id: ServletModuleOpenIndy.java,v 1.75 2003/04/18 14:01:41 john Exp $\r
+ * @version $Id: ServletModuleOpenIndy.java,v 1.76 2003/04/18 14:54:54 john Exp $\r
  *\r
  */\r
 \r
@@ -1084,53 +1084,71 @@ public class ServletModuleOpenIndy extends ServletModule
 \r
   public void getpdf(HttpServletRequest req, HttpServletResponse res)\r
       throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {\r
+    long starttime=System.currentTimeMillis();\r
     String ID_REQUEST_PARAM = "id";\r
     int maxArticlesInNewsleter = 15; // it is nice not to be dos'ed\r
     try {\r
         String idParam = req.getParameter(ID_REQUEST_PARAM);\r
         if (idParam != null) {\r
-          ByteArrayOutputStream out = new ByteArrayOutputStream();\r
-          PDFGenerator pdfMaker = new PDFGenerator(out);\r
+          \r
 \r
           RE re = new RE("[0-9]+");\r
-\r
-\r
+         \r
+         \r
           REMatch[] idMatches=re.getAllMatches(idParam);\r
-\r
+         \r
          String cacheSelector="";\r
-\r
+         \r
          for (int i = 0; i < idMatches.length; i++){\r
            cacheSelector=   cacheSelector + "," + idMatches[i].toString();\r
          }\r
-\r
          \r
-          if (idMatches.length > 1){\r
-            pdfMaker.addLine();\r
-            for (int i = 0; i < idMatches.length  && i < maxArticlesInNewsleter; i++){\r
-              REMatch aMatch = idMatches[i];\r
-              String id=aMatch.toString();\r
-              EntityContent contentEnt = (EntityContent)contentModule.getById(id);\r
-              pdfMaker.addIndexItem(contentEnt);\r
-\r
-            }\r
-          }\r
-\r
-          for (int i = 0; i < idMatches.length; i++){\r
-            REMatch aMatch = idMatches[i];\r
-\r
-            String id=aMatch.toString();\r
-\r
-            EntityContent contentEnt = (EntityContent)contentModule.getById(id);\r
-            pdfMaker.add(contentEnt);\r
-\r
-          }\r
-\r
-          pdfMaker.stop();\r
-          res.setContentType("application/pdf");\r
-          byte[] content = out.toByteArray();\r
-          res.setContentLength(content.length);\r
-          res.getOutputStream().write(content);\r
-          res.getOutputStream().flush();\r
+         String cacheType="pdf";\r
+         \r
+         String theCacheKey = cacheType+":"+cacheSelector;\r
+         \r
+         byte[] thePDF;\r
+         \r
+         if (MirGlobal.mruCache().hasObject(theCacheKey)){\r
+           logger.warn("fetching pdf from cache");\r
+           thePDF = (byte[]) MirGlobal.mruCache().getObject(theCacheKey);\r
+         }\r
+         else {\r
+           logger.warn("generating pdf and caching it");\r
+           ByteArrayOutputStream out = new ByteArrayOutputStream();\r
+           PDFGenerator pdfMaker = new PDFGenerator(out);\r
+           \r
+           if (idMatches.length > 1){\r
+             pdfMaker.addLine();\r
+             for (int i = 0; i < idMatches.length  && i < maxArticlesInNewsleter; i++){\r
+               REMatch aMatch = idMatches[i];\r
+               String id=aMatch.toString();\r
+               EntityContent contentEnt = (EntityContent)contentModule.getById(id);\r
+               pdfMaker.addIndexItem(contentEnt);\r
+             }\r
+           }\r
+           \r
+           for (int i = 0; i < idMatches.length; i++){\r
+             REMatch aMatch = idMatches[i];\r
+             String id=aMatch.toString();\r
+             EntityContent contentEnt = (EntityContent)contentModule.getById(id);\r
+             \r
+             pdfMaker.add(contentEnt);\r
+           }\r
+           \r
+           pdfMaker.stop();\r
+           thePDF  = out.toByteArray();\r
+           \r
+           //and save all our hard work!\r
+           MirGlobal.mruCache().storeObject(theCacheKey,thePDF);\r
+         }\r
+         \r
+         res.setContentType("application/pdf");\r
+         res.setContentLength(thePDF.length);\r
+         res.getOutputStream().write(thePDF);\r
+         res.getOutputStream().flush();\r
+         String elapsedtime=(new Long(System.currentTimeMillis()-starttime)).toString();\r
+         logger.warn("pdf retireval took "+elapsedtime + " milliseconds"  );\r
 \r
         }\r
         else {\r