You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2008/12/05 17:21:20 UTC
svn commit: r723781 - in /cocoon/cocoon3/trunk:
cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/
cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/caching/
cocoon-sample-webapp/src/test/java/org/apache/cocoon/it/
cocoon-sample/src/main/...
Author: reinhard
Date: Fri Dec 5 08:21:19 2008
New Revision: 723781
URL: http://svn.apache.org/viewvc?rev=723781&view=rev
Log:
. fix expires caching: if a pipeline contains non-cacheable components, an explicit expires-cache-key has to be provided
. fix timestamp generator (integration tests)
(done together with Steven)
Modified:
cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/AsyncCachePipeline.java
cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java
cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/caching/InvalidCacheKey.java
cocoon/cocoon3/trunk/cocoon-sample-webapp/src/test/java/org/apache/cocoon/it/CachingOfPipelinesTest.java
cocoon/cocoon3/trunk/cocoon-sample/src/main/java/org/apache/cocoon/sample/generation/CachingTimestampGenerator.java
cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap
Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/AsyncCachePipeline.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/AsyncCachePipeline.java?rev=723781&r1=723780&r2=723781&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/AsyncCachePipeline.java (original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/AsyncCachePipeline.java Fri Dec 5 08:21:19 2008
@@ -82,9 +82,8 @@
// no cached value (not even an invalid one) was present -> execute the pipeline
this.invokeStarter();
// cache the result
- this
- .setCachedValue(this.cacheKey, new CompleteCacheValue(this.cachingOutputStream.getContent(),
- this.cacheKey));
+ CompleteCacheValue cacheValue = new CompleteCacheValue(this.cachingOutputStream.getContent(), this.cacheKey);
+ this.setCachedValue(this.cacheKey, cacheValue);
}
public CacheRefreshManager getCacheRefreshManager() {
Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java?rev=723781&r1=723780&r2=723781&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java (original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java Fri Dec 5 08:21:19 2008
@@ -19,6 +19,7 @@
package org.apache.cocoon.pipeline;
import java.io.OutputStream;
+import java.io.Serializable;
import java.util.Map;
import org.apache.cocoon.pipeline.caching.CacheKey;
@@ -48,8 +49,12 @@
protected PipelineCache pipelineCache;
+ /** Expires time in seconds */
private String expires;
+ /** Expires pipelines that have non-cacheable pipeline components require an explicit cache key */
+ private Serializable expiresCacheKey;
+
private final Log logger = LogFactory.getLog(this.getClass());
public CacheKey constructCacheKey() {
@@ -74,12 +79,12 @@
// component does not support caching
if (this.logger.isDebugEnabled()) {
this.logger.debug(" ~ no caching: " + pipelineComponent);
- this.logger.debug("Stop construction a cache key");
+ this.logger.debug("Aborting cache key construction");
}
// support expires caching
if (this.expires != null) {
- return new ExpiresCacheKey(new InvalidCacheKey(), this.expires);
+ return new ExpiresCacheKey(new InvalidCacheKey(this.expiresCacheKey), this.expires);
}
return null;
}
@@ -119,6 +124,10 @@
this.setCachedValue(this.cacheKey, cacheValue);
}
+ public String getExpires() {
+ return this.expires;
+ }
+
@Override
public long getLastModified() {
if (this.cacheKey == null) {
@@ -135,10 +144,19 @@
@Override
public void setConfiguration(Map<String, ? extends Object> parameters) {
this.expires = (String) parameters.get("expires");
+ this.expiresCacheKey = (Serializable) parameters.get("expires-cache-key");
super.setConfiguration(parameters);
}
+ public void setExpiresCacheKey(Serializable expiresCacheKey) {
+ this.expiresCacheKey = expiresCacheKey;
+ }
+
+ public void setExpires(String expires) {
+ this.expires = expires;
+ }
+
public void setPipelineCache(PipelineCache pipelineCache) {
this.pipelineCache = pipelineCache;
}
Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/caching/InvalidCacheKey.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/caching/InvalidCacheKey.java?rev=723781&r1=723780&r2=723781&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/caching/InvalidCacheKey.java (original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/caching/InvalidCacheKey.java Fri Dec 5 08:21:19 2008
@@ -16,13 +16,29 @@
*/
package org.apache.cocoon.pipeline.caching;
+import java.io.Serializable;
+
public class InvalidCacheKey implements CacheKey {
private static final long serialVersionUID = 1L;
+ private final Serializable expiresCacheKey;
+
+ public InvalidCacheKey(Serializable expiresCacheKey) {
+ if (expiresCacheKey == null) {
+ throw new NullPointerException("An explicit cache key has to be provided.");
+ }
+
+ this.expiresCacheKey = expiresCacheKey;
+ }
@Override
public boolean equals(Object obj) {
- return obj instanceof InvalidCacheKey;
+ if (!(obj instanceof InvalidCacheKey)) {
+ return false;
+ }
+
+ InvalidCacheKey other = (InvalidCacheKey) obj;
+ return this.expiresCacheKey.equals(other.expiresCacheKey);
}
public long getLastModified() {
@@ -31,7 +47,12 @@
@Override
public int hashCode() {
- return InvalidCacheKey.class.hashCode();
+ int result = 1;
+
+ result = 31 * result + this.getClass().hashCode();
+ result = 31 * result + this.expiresCacheKey.hashCode();
+
+ return result;
}
public boolean isValid(CacheKey cacheKey) {
Modified: cocoon/cocoon3/trunk/cocoon-sample-webapp/src/test/java/org/apache/cocoon/it/CachingOfPipelinesTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sample-webapp/src/test/java/org/apache/cocoon/it/CachingOfPipelinesTest.java?rev=723781&r1=723780&r2=723781&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sample-webapp/src/test/java/org/apache/cocoon/it/CachingOfPipelinesTest.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sample-webapp/src/test/java/org/apache/cocoon/it/CachingOfPipelinesTest.java Fri Dec 5 08:21:19 2008
@@ -50,7 +50,7 @@
this.loadResponse("caching-pipeline/on");
String content2 = this.response.getContentAsString();
- assertTrue("The response has to be always the same.", content1.equals(content2));
+ assertEquals("The response has to be always the same.", content1, content2);
Thread.sleep(1000);
this.loadResponse("caching-pipeline/on");
@@ -68,14 +68,21 @@
public void testExpiresCachingPipeline() throws Exception {
Thread.sleep(2000);
this.loadResponse("/expires/caching-pipeline/on");
+ assertEquals(200, this.response.getStatusCode());
String content1 = this.response.getContentAsString();
Thread.sleep(2100);
this.loadResponse("/expires/caching-pipeline/on");
+ assertEquals(200, this.response.getStatusCode());
String content2 = this.response.getContentAsString();
assertFalse("The response has to be the same as before.", content1.equals(content2));
}
+ public void testInvalidExpiresCachingPipeline() throws Exception {
+ this.loadResponse("/expires/caching-pipeline/invalid");
+ assertEquals(500, this.response.getStatusCode());
+ }
+
/**
* This async-caching pipeline returns the same result for 2 seconds (see the 'expires' parameter in the sitemap).
* After 2.1 seconds it still produces the same result but because the cache key of this pipeline has changed, a
Modified: cocoon/cocoon3/trunk/cocoon-sample/src/main/java/org/apache/cocoon/sample/generation/CachingTimestampGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sample/src/main/java/org/apache/cocoon/sample/generation/CachingTimestampGenerator.java?rev=723781&r1=723780&r2=723781&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sample/src/main/java/org/apache/cocoon/sample/generation/CachingTimestampGenerator.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sample/src/main/java/org/apache/cocoon/sample/generation/CachingTimestampGenerator.java Fri Dec 5 08:21:19 2008
@@ -24,9 +24,7 @@
private static final int CACHING_PERIOD = 1500;
public CacheKey constructCacheKey() {
- final long timestamp = System.currentTimeMillis() / CACHING_PERIOD * CACHING_PERIOD;
-
- return new CacheKeyImplementation(timestamp);
+ return new CacheKeyImplementation(System.currentTimeMillis());
}
private final class CacheKeyImplementation implements CacheKey {
@@ -53,12 +51,12 @@
}
public boolean isValid(CacheKey other) {
- if (other instanceof CacheKeyImplementation) {
- CacheKeyImplementation otherCacheKey = (CacheKeyImplementation) other;
- return otherCacheKey.timestamp == this.timestamp;
+ if (!(other instanceof CacheKeyImplementation)) {
+ return false;
}
- return false;
+ CacheKeyImplementation otherCacheKey = (CacheKeyImplementation) other;
+ return Math.abs(this.timestamp - otherCacheKey.timestamp) < CACHING_PERIOD;
}
}
}
Modified: cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap?rev=723781&r1=723780&r2=723781&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap (original)
+++ cocoon/cocoon3/trunk/cocoon-sample/src/main/resources/COB-INF/sitemap.xmap Fri Dec 5 08:21:19 2008
@@ -94,14 +94,21 @@
</map:match>
</map:pipeline>
- <map:pipeline expires="2">
+ <map:pipeline expires="2" expires-cache-key="some-key">
<map:match pattern="expires/caching-pipeline/on">
<map:generate type="timestamp-noncaching" />
<map:serialize type="xml" />
</map:match>
</map:pipeline>
- <map:pipeline type="async-caching" expires="2">
+ <map:pipeline expires="2">
+ <map:match pattern="expires/caching-pipeline/invalid">
+ <map:generate type="timestamp-noncaching" />
+ <map:serialize type="xml" />
+ </map:match>
+ </map:pipeline>
+
+ <map:pipeline type="async-caching" expires="2" expires-cache-key="some-other-key">
<map:match pattern="expires/async-caching-pipeline/on">
<map:generate type="timestamp-noncaching" />
<map:serialize type="xml" />
@@ -354,6 +361,13 @@
<map:serialize type="xml" />
</map:match>
</map:pipeline>
+
+ <map:pipeline type="async-caching" expires="5">
+ <map:match pattern="expirese/object-model/request-parameters">
+ <map:generate type="request-parameters" />
+ <map:serialize type="xml" />
+ </map:match>
+ </map:pipeline>
<!-- ~~~~~~~~~~~~~~~~ controller ~~~~~~~~~~~~~~~ -->
<map:pipeline>