I am getting the same problem that was reported in
CACHE-278.
Issue
CACHE-278 is reported as being closed but I am still getting the propblem after trying the 2.4 production release of oscache and have also tried versions; cache-2.4-20070519071202.jar and oscache-2.4.1-dev-20070519111245.jar from:
http://ivyrep.opensymphony.com/opensymphony/oscache/.
I have the following configuration for the CacheFilter
<init-param>
<param-name>max-age</param-name>
<param-value>no init</param-value>
</init-param>
The first time I request a page from the cache I get the expected response (no max-age filter).
First Response:
HTTP/1.x 200 OK
Date: Wed, 06 Jun 2007 06:25:01 GMT
Content-Language: en-GB
Content-Type: application/xhtml+xml
Transfer-Encoding: chunked
On the second request when I recevie the cached response I get the following headers:
Second Response:
HTTP/1.x 200 OK
Date: Wed, 06 Jun 2007 06:25:22 GMT
Content-Language: en-GB
Content-Length: 11252
Content-Type: application/xhtml+xml
Cache-Control: max-age=60
I have traced the problem to the CacheHttpServletResponseWrapper class. In the constructor there is a block of code for setting up the max-age on the ResponseContent object that looks like this:
// setting the cache control with max-age
if (this.cacheControl == CacheFilter.MAX_AGE_TIME) {
// set the count down
long maxAge = System.currentTimeMillis();
maxAge = maxAge - (maxAge % 1000) + time;
result.setMaxAge(maxAge);
super.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + time / 1000);
} else if (this.cacheControl != CacheFilter.MAX_AGE_NO_INIT) {
result.setMaxAge(this.cacheControl);
super.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + (-this.cacheControl));
}
The problem is that for the case when this.cacheControl == CacheFilter.MAX_AGE_NO_INIT you are not setting the value of the maxAge property on the ResponseContent (result) object. The ResponseContent object defaults maxAge to 60 so what happens is that when the response get's served from the cache instead of providing no value for max-age we get the default value of 60. For the first request the response does not get built from this ResponseContent object so that is why there was no max-age header.
The problem can be fixed by changing the block of code above to look like this:
// setting the cache control with max-age
if (this.cacheControl == CacheFilter.MAX_AGE_TIME) {
// set the count down
long maxAge = System.currentTimeMillis();
maxAge = maxAge - (maxAge % 1000) + time;
result.setMaxAge(maxAge);
super.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + time / 1000);
} else if (this.cacheControl != CacheFilter.MAX_AGE_NO_INIT) {
result.setMaxAge(this.cacheControl);
super.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + (-this.cacheControl));
} else if (this.cacheControl == CacheFilter.MAX_AGE_NO_INIT ) {
result.setMaxAge(this.cacheControl);
}
With the maxAge property set to equal CacheFilter.MAX_AGE_NO_INIT the ResponseContent object will not set the max-age header when the response gets served from the cache.
CACHE-297to issueCACHE-278