You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by gi...@apache.org on 2003/02/24 16:46:09 UTC

cvs commit: xml-cocoon2/src/java/org/apache/cocoon/caching CachedResponse.java

gianugo     2003/02/24 07:46:09

  Modified:    src/java/org/apache/cocoon/components/pipeline
                        AbstractProcessingPipeline.java
               src/java/org/apache/cocoon/components/pipeline/impl
                        AbstractCachingProcessingPipeline.java
                        CachingProcessingPipeline.java
               src/java/org/apache/cocoon/caching CachedResponse.java
  Log:
  The "expires" pipeline parameter is honored even by the internal Cocoon cache, i.e. the caching algorithms are overridden if an expires parameter is present and still fresh.
  
  Revision  Changes    Path
  1.34      +8 -4      xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/AbstractProcessingPipeline.java
  
  Index: AbstractProcessingPipeline.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/AbstractProcessingPipeline.java,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- AbstractProcessingPipeline.java	2 Feb 2003 23:21:24 -0000	1.33
  +++ AbstractProcessingPipeline.java	24 Feb 2003 15:46:08 -0000	1.34
  @@ -466,12 +466,16 @@
           }
           this.preparePipeline(environment);
   
  +
           // See if we need to set an "Expires:" header
           if (this.expires != 0) {
               Response res = ObjectModelHelper.getResponse(environment.getObjectModel());
  -            res.setDateHeader("Expires", expires);
  +            res.setDateHeader("Expires", System.currentTimeMillis() + expires);
  +            res.setHeader("Cache-Control", "max-age=" + expires/1000 + ", public");
  +            if (this.getLogger().isDebugEnabled())
  +                this.getLogger().debug("Setting a new Expires object for this resource");
               environment.getObjectModel().put(ObjectModelHelper.EXPIRES_OBJECT,
  -                 new Long(expires));
  +                 new Long(expires + System.currentTimeMillis()));
           }
   
           if ( this.reader != null ) {
  @@ -757,7 +761,7 @@
               expires += number * modifier;
           }
   
  -        return System.currentTimeMillis() + expires;
  +        return expires;
       }
   
   }
  
  
  
  1.27      +62 -2     xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/impl/AbstractCachingProcessingPipeline.java
  
  Index: AbstractCachingProcessingPipeline.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/impl/AbstractCachingProcessingPipeline.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- AbstractCachingProcessingPipeline.java	24 Feb 2003 14:20:33 -0000	1.26
  +++ AbstractCachingProcessingPipeline.java	24 Feb 2003 15:46:08 -0000	1.27
  @@ -61,6 +61,7 @@
   import org.apache.cocoon.components.sax.XMLDeserializer;
   import org.apache.cocoon.components.sax.XMLSerializer;
   import org.apache.cocoon.environment.Environment;
  +import org.apache.cocoon.environment.ObjectModelHelper;
   import org.apache.cocoon.transformation.Transformer;
   import org.apache.excalibur.source.SourceValidity;
   import org.apache.excalibur.source.impl.validity.AggregatedValidity;
  @@ -71,6 +72,7 @@
   import java.io.Serializable;
   import java.net.SocketException;
   import java.util.ArrayList;
  +import java.util.Date;
   
   /**
    * This is the base class for all caching pipeline implementations.
  @@ -392,7 +394,9 @@
       }
   
       /**
  -     * Calculate the key that can be used to get something from the cache
  +     * Calculate the key that can be used to get something from the cache, and 
  +     * handle expires properly.
  +     * 
        */
       protected void validatePipeline(Environment environment)
       throws ProcessingException {
  @@ -418,6 +422,62 @@
   
                   boolean responseIsValid = true;
                   boolean responseIsUsable = true;
  +
  +                // See if we have an explicit "expires" setting. If so,
  +                // and if it's still fresh, we're done.
  +                Long responseExpires = (Long) response.getExpires();
  +
  +                if (responseExpires != null) {
  +                    if (this.getLogger().isDebugEnabled()) {
  +                       this.getLogger().debug(
  +                       "Expires time found for " +
  +                       environment.getURI());
  +                    }
  +                    if ( responseExpires.longValue() > System.currentTimeMillis()) {
  +                        if (this.getLogger().isDebugEnabled()) {
  +                            this.getLogger().debug(
  +                                "Expires time still fresh for " +
  +                                environment.getURI() +
  +                                ", ignoring all other cache settings. This entry expires on "+
  +                                new Date(responseExpires.longValue()));
  +                        }
  +                        this.cachedResponse = response.getResponse();
  +                        return;
  +                    } else {
  +                        if (this.getLogger().isDebugEnabled()) {
  +                            this.getLogger().debug(
  +                                "Expires time has expired for " +
  +                                environment.getURI() +
  +                                " regenerating content.");
  +                        }
  +                        
  +                        // If an expires parameter was provided, use it. If this parameter is not available
  +                        // it means that the sitemap was modified, and the old expires value is not valid
  +                        // anymore.
  +                        if (expires != 0) {
  +                     
  +                            if (this.getLogger().isDebugEnabled())
  +                                this.getLogger().debug("Refreshing expires informations");
  +                     
  +                            response.setExpires(new Long(expires + System.currentTimeMillis()));    
  +                     
  +                        } else {
  +                     
  +                            if (this.getLogger().isDebugEnabled())
  +                                this.getLogger().debug("No expires defined anymore for this object, setting it to no expires");
  +                     
  +                            response.setExpires(null);
  +                        }                                   
  +                    }
  +                } else {
  +                    // The response had no expires informations. See if it needs to be set (i.e. because the configuration has changed)
  +                    if (expires != 0) {
  +                        if (this.getLogger().isDebugEnabled())
  +                            this.getLogger().debug("Setting a new expires object for this resource");
  +                        response.setExpires(new Long(expires + System.currentTimeMillis()));                                
  +                    }        
  +                }
  +                
                   SourceValidity[] fromCacheValidityObjects = response.getValidityObjects();
   
                   int i = 0;
  
  
  
  1.35      +8 -3      xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java
  
  Index: CachingProcessingPipeline.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- CachingProcessingPipeline.java	31 Jan 2003 22:51:31 -0000	1.34
  +++ CachingProcessingPipeline.java	24 Feb 2003 15:46:08 -0000	1.35
  @@ -59,6 +59,7 @@
   import org.apache.cocoon.components.sax.XMLSerializer;
   import org.apache.cocoon.components.sax.XMLTeePipe;
   import org.apache.cocoon.environment.Environment;
  +import org.apache.cocoon.environment.ObjectModelHelper;
   import org.apache.cocoon.xml.XMLConsumer;
   import org.apache.cocoon.xml.XMLProducer;
   
  @@ -81,15 +82,19 @@
       */
       protected void cacheResults(Environment environment, OutputStream os)  throws Exception {
           if (this.toCacheKey != null) {
  +            // See if there is an expires object for this resource.                
  +            Long expiresObj = (Long) environment.getObjectModel().get(ObjectModelHelper.EXPIRES_OBJECT);
               if ( this.cacheCompleteResponse ) {
                   CachedResponse response = new CachedResponse(this.toCacheSourceValidities,
  -                                          ((CachingOutputStream)os).getContent());
  +                                          ((CachingOutputStream)os).getContent(),
  +                                          expiresObj);
                   this.cache.store(environment.getObjectModel(),
                                    this.toCacheKey,
                                    response);
               } else {
                   CachedResponse response = new CachedResponse(this.toCacheSourceValidities,
  -                                          (byte[])this.xmlSerializer.getSAXFragment());
  +                                          (byte[])this.xmlSerializer.getSAXFragment(),
  +                                          expiresObj);
                   this.cache.store(environment.getObjectModel(),
                                    this.toCacheKey,
                                    response);
  
  
  
  1.6       +37 -1     xml-cocoon2/src/java/org/apache/cocoon/caching/CachedResponse.java
  
  Index: CachedResponse.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/caching/CachedResponse.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- CachedResponse.java	31 Jan 2003 22:51:22 -0000	1.5
  +++ CachedResponse.java	24 Feb 2003 15:46:08 -0000	1.6
  @@ -70,6 +70,7 @@
   
       private SourceValidity[] validityObjects;
       private byte[]           response;
  +    private Long             expires;
   
       /**
        * Create a new entry for the cache.
  @@ -82,6 +83,24 @@
                             byte[]           response) {
           this.validityObjects = validityObjects;
           this.response = response;
  +        this.expires = null;
  +    }
  +
  +    /**
  +     * Create a new entry for the cache.
  +     *
  +     * @param validityObjects The SourceValidity objects in the order
  +     *                        they occured in the pipeline
  +     * @param response        The cached sax stream or character stream
  +     * @param expires         The configured expires, or null if no
  +     *                        expires was defined.
  +     */
  +    public CachedResponse(SourceValidity[] validityObjects,
  +                          byte[]           response,
  +                          Long expires) {
  +        this.validityObjects = validityObjects;
  +        this.response = response;
  +        this.expires = expires;
       }
   
       /**
  @@ -98,5 +117,22 @@
        */
       public byte[] getResponse() {
           return this.response;
  +    }
  +
  +    /**
  +     * Get the configured expires.
  +     *
  +     * @return The configured expires, or null if no expires was defined
  +     */
  +    public Long getExpires() {
  +        return this.expires;
  +    }
  +    
  +    /**
  +     * Set the (newly) configured expires.
  +     * 
  +     */
  +    public void setExpires(Long newExpires) {
  +        this.expires = newExpires;    
       }
   }