put the MRUCache stuff back in....
authorjohn <john>
Sun, 4 May 2003 11:42:07 +0000 (11:42 +0000)
committerjohn <john>
Sun, 4 May 2003 11:42:07 +0000 (11:42 +0000)
source/mircoders/servlet/ServletModuleOpenIndy.java

index 9e7f5be..2c7ddc7 100755 (executable)
@@ -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();