org.apache.excalibur.source.SourceValidity
org.apache.excalibur.source.impl.validity.ExpiresValidity
java.io.Serializable
// artificial slowdown to make the effects of the cache visible
final int DELAY_SECS = 2;
// request parameter "validity" contains number of seconds to cache
private int getValidityFromRequest() {
int result = 15;
try {
result = Integer.valueOf(request.getParameter("validity")).intValue();
} catch(Exception e) {
// keep default value
}
return result;
}
/**
* Generate the unique key for the cache.
*
* This key must be unique inside the space of this XSP page, it is used
* to find the page contents in the cache (if getValidity says that the
* contents are still valid).
*
* This method will be invoked before the getValidity() method.
*
* @return The generated key or null if the component
* is currently not cacheable.
*/
public Serializable getKey()
{
// for our test, pages having the same value of "pageKey" will share
// the same cache location
return "" + request.getParameter("pageKey");
}
/**
* Generate the validity object, tells the cache how long to
* keep contents having this key around.
*
* Before this method can be invoked the getKey() method
* will be invoked.
*
* @return The generated validity object or null if the
* component is currently not cacheable.
*/
public SourceValidity getValidity() {
// keep in cache for our validity time
return new ExpiresValidity(getValidityFromRequest()*1000);
}
A Cacheable XSP Page
Hi there! I'm a simple dynamic page generated by XSP (eXtensible Server Pages).
I need DELAY_SECS seconds to be generated, so you can tell
if I'm being served from the cache or not.
What you see here has been generated on new java.util.Date().
I'm cached for every different value of request parameter 'pageKey'.
Here the value is:
.
If this is not the same as the 'pageKey' parameter in the page URL, we have a problem.
All other request parameters do not influence cache status, but
my validity will expire after getValidityFromRequest() seconds
(set by 'validity' URL parameter when page is generated).
Value of parameter 'other' is: String.valueOf(request.getParameter("other")).
This might be different than the URL parameter 'other', in case the version of the page you're
seeing was cached from a request having the same 'pageKey' but a different value of 'other'.
// slowdown page generation.
try {
Thread.sleep(DELAY_SECS * 1000L);
} catch (InterruptedException ie) {
// Not much that can be done...
}
Test links: