blob: 3848606b36958c26cd8363b8a3f23daa7a41016e [file] [log] [blame]
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
XSP caching sample.
WARNING - another sample depends on this!
The output of this is used by the FOP cache test - if you modify this page, please make sure that
the caching samples found at http://localhost:8888/samples/fop/welcome (or equivalent)
still work .
$Id$
-->
<xsp:page language="java"
xmlns:xsp="http://apache.org/xsp"
xmlns:xsp-request="http://apache.org/xsp/request/2.0">
<xsp:structure>
<xsp:include>org.apache.excalibur.source.SourceValidity</xsp:include>
<xsp:include>org.apache.excalibur.source.impl.validity.ExpiresValidity</xsp:include>
<xsp:include>java.io.Serializable</xsp:include>
</xsp:structure>
<xsp:logic>
// 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);
}
</xsp:logic>
<page>
<title>A Cacheable XSP Page</title>
<content>
<para>
Hi there! I'm a simple dynamic page generated by XSP (eXtensible Server Pages).
</para>
<para>
I need <xsp:expr>DELAY_SECS</xsp:expr> seconds to be generated, so you can tell
if I'm being served from the cache or not.
<br/>
What you see here has been generated on <b><xsp:expr>new java.util.Date()</xsp:expr></b>.
</para>
<para>
I'm cached for every different value of request parameter 'pageKey'.
<br/>
Here the value is:
<b><xsp-request:get-parameter name="pageKey"/></b>.
<br/>
If this is not the same as the 'pageKey' parameter in the page URL, we have a problem.
</para>
<para>
All other request parameters do not influence cache status, but
my validity will expire after <xsp:expr>getValidityFromRequest()</xsp:expr> seconds
(set by 'validity' URL parameter when page is generated).
</para>
<para>
Value of parameter 'other' is: <b><xsp:expr>String.valueOf(request.getParameter("other"))</xsp:expr></b>.
<br/>
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'.
</para>
<xsp:logic>
// slowdown page generation.
try {
Thread.sleep(DELAY_SECS * 1000L);
} catch (InterruptedException ie) {
// Not much that can be done...
}
</xsp:logic>
<para>Test links:
<ul>
<li><a target="_new" href="cacheable?pageKey=one">pageKey=one</a></li>
<li><a target="_new" href="cacheable?pageKey=two">pageKey=two</a></li>
<li><a target="_new" href="cacheable?pageKey=three&amp;other=abc">pageKey=three, other=abc</a></li>
<li><a target="_new" href="cacheable?pageKey=three&amp;other=xyz">pageKey=three, other=xyz</a></li>
<li><a target="_new" href="cacheable?pageKey=three&amp;other=wow&amp;validity=5">pageKey=three, other=wow and 5 seconds of cache validity</a></li>
</ul>
</para>
</content>
</page>
</xsp:page>