Custom JSP tags. This taglib supports dynamic cache. You can
cache a whole page or an any part (parts) of it. Also this taglib lets you simulate
OutputCache directive in ASP.NET.
Let us see this example:
<%@ taglib uri="taglib.tld" prefix="cache" %>
<cache:Cache refresh="60"
scope="session" key="abc">
<html>
<%
out.println(new java.util.Date());
%>
</html>
</cache:Cache>
here calculation's results (HTML code) will be saved in the cache for 60 seconds. So for
the each next request during that time client will get data directly from
the cache (without the new evaluation for tag's body).
Cached data may be persistent (this option should be described
by the parameter store). Store is just some existing directory on your
server. Saved cache could be restored (e.g. after crashes).
Each data chunk must have own unique key. Under this key you may have more than one
cache each of them will be described by request's parameters and/or headers. For doing
this you can provide a list of parameters (semicolon separated) and/or a list of headers. E.g.:
<%@ taglib uri="taglib.tld" prefix="cache" %>
<cache:Cache refresh="120" scope="application"
varyByParam="user;qty" varyByHeader="user-agent"
key="abc" store="c:\\tmp">
<!-- some code -->
</cache:Cache>
varyByParam parameter provides a semicolon-separated list of strings used to vary the output cache.
By default, these strings correspond to a query string value sent with GET method attributes, or a parameter sent
using the POST method. It is similar to an appropriate parameter in ASP.NET OutputCache directive.
When this attribute is set to multiple parameters, the output cache contains a different version of the requested body
for each specified parameter.
varyByHeader parameter does the same with HTTP headers.
Note for ASP.NET users. To simulate VaryByCustom parameter you may use an expression
in a key parameter. This expression could be for example a call
to some function that calculates cache key by your own algorithm.
Cache data are separated by the scope. You may cache data per sessions (actually it means caching per users) or per applications
(actually it means caching for all users).
Parameter refresh describes how long data will be saved in cache.
Another way to describe cache policy is to set some file name. In this case
cache validation depends on modification time for this file. Body will be re-evaluated
is last modification time has been changed. This option could be useful if
tag's body actually depends on this file (e.g. some xsl sheet etc.):
<cache:Cache depends="some_file.xsl" key="def">
<!-- some code -->
</cache:Cache>
Tag forEachKey lets you iterate over existing cache keys. E.g.:
<!-- list global caches -->
<cache:forEachKey scope="application" id="currentKey">
Key is <%=currentKey%>
</cache:forEachKey>
Tags are:
Cache
This tag puts own body into cache or outputs cached data. Parameters are:
1) key data key (identification)
2) refresh cache refresh time (in seconds). Default value is 600 (if you did not set
depends parameter).
3) depends file name cache depends on
4) scope Data scope. Possible values are:
session, application. Default value is session
5) store Optional parameter. Any existing directory on your server.
By default data are not persistent.
6) varyByParam Optional parameter. Provides a semicolon-separated
list of request parameters. By default is empty.
7) varyByHeader Optional parameter. Provides a semicolon-separated
list of request headers. By default is empty.
8) varyByCookie Optional parameter. Provides a semicolon-separated
list of cookies. By default is empty.
9) print Optional parameter. Possible values are
true or false. If this value is true cached data will be printed,
otherwise tag just saves/updates cache. Default value is true.
10) updateCache Optional parameter. Possible values are
true or false. If this value is true body will be executed and cached data will be
updated regardless of the expiration. Default value is false.
11) noCache Optional parameter. Possible values are
true or false. If this value is true body will be executed and all cache related settings
will be just ignored. During this execution tag does not change the state of your cache. Default value is false.
12) cacheEmptyBody Optional parameter. Possible values are
true or false. If this value is false tag does not cache empty bodies (strings).
Default value is true (tag caches all the bodies).
13) encoding Optional parameter. Describes an encoding for persitent data.
Default value is UTF-8.
clearCache
Clears cache for the given key (and all subcaches). Parameters are:
1) key data key (identification). You can omit this parameter
if you are using this tag inside of forEachKey tag.
2) scope Data scope. Possible values are:
session, application. Default value is session
3) cond Optional attribute tag's action
depends on. Possible values are true or false.
Default value is true (clear cache)
getCache
Prints cached data or puts them into page scope variable. If you set the parameter
id data will be saved in the page scope variable described by this
parameter. Otherwise tag just prints cached data. Parameters are:
1) key data key (identification). You can omit this parameter
if you are using this tag inside of forEachKey tag.
2) scope Data scope. Possible values are:
session, application. Default value is session
3) id page scope variable.
4) varyByParam Optional parameter. Provides a semicolon-separated
list of request parameters. By default is empty.
5) varyByHeader Optional parameter. Provides a semicolon-separated
list of request headers. By default is empty.
6) varyByCookie Optional parameter. Provides a semicolon-separated
list of cookies. By default is empty.
restoreCache
Prints cached data from the given store or puts them into page scope variable. If you set the parameter
id data will be saved in the page scope variable described by this
parameter. Otherwise tag just prints cached data. Parameters are:
1) key data key (identification).
2) scope Data scope. Possible values are:
session, application. Default value is session
3) store Describes your store (directory). See Cache tag.
4) id Optional parameter. Descibes a name for the page scope variable.
5) varyByParam Optional parameter. Provides a semicolon-separated
list of request parameters. By default is empty.
6) varyByHeader Optional parameter. Provides a semicolon-separated
list of request headers. By default is empty.
7) varyByCookie Optional parameter. Provides a semicolon-separated
list of cookies. By default is empty.
8) cond Optional attribute tag's action
depends on. Possible values are true or false.
Default value is true (restore cache)
9) encoding Optional parameter. Describes an encoding for persitent data.
Default value is UTF-8.
getHits
Prints a counter for cache hits or saves it in the page scope variable. If you set the parameter
id data will be saved in the page scope variable described by this
parameter. Otherwise tag just prints data. Parameters are:
1) key data key (identification). You can omit this parameter
if you are using this tag inside of forEachKey tag.
2) scope Data scope. Possible values are:
session, application. Default value is session
3) id Optional parameter. Describes a name for the page scope variable
(type is java.lang.Long) for data. Without this parameter tag
just prints data.
4) varyByParam Optional parameter. Provides a semicolon-separated
list of request parameters. By default is empty.
5) varyByHeader Optional parameter. Provides a semicolon-separated
list of request headers. By default is empty.
6) varyByCookie Optional parameter. Provides a semicolon-separated
list of cookies. By default is empty.
getExecutions
Prints a counter for cached data updates (body execution) or saves it in the page scope variable. If you set the parameter
id data will be saved in the page scope variable described by this
parameter. Otherwise tag just prints data. Parameters are:
1) key data key (identification). You can omit this parameter
if you are using this tag inside of forEachKey tag.
2) scope Data scope. Possible values are:
session, application. Default value is session
3) id Optional parameter. Describes a name for the page scope variable
(type is java.lang.Long) for data. Without this parameter tag
just prints data.
4) varyByParam Optional parameter. Provides a semicolon-separated
list of request parameters. By default is empty.
5) varyByHeader Optional parameter. Provides a semicolon-separated
list of request headers. By default is empty.
6) varyByCookie Optional parameter. Provides a semicolon-separated
list of cookies. By default is empty.
getLastModified
Calculates a 'last modified' time for the given cache. If you set the parameter
id data will be saved in the page scope variable described by this
parameter (type is java.util.Date). Otherwise tag just prints data. Parameters are:
1) key data key (identification). You can omit this parameter
if you are using this tag inside of forEachKey tag.
2) scope Data scope. Possible values are:
session, application. Default value is session
3) id Optional parameter. Describes a page scope variable
(type is java.util.Date) for data. Without this parameter tag
just prints data.
4) varyByParam Optional parameter. Provides a semicolon-separated
list of request parameters. By default is empty.
5) varyByHeader Optional parameter. Provides a semicolon-separated
list of request headers. By default is empty.
6) varyByCookie Optional parameter. Provides a semicolon-separated
list of cookies. By default is empty.
ifCached
Body tag. Executes own body in case of described cached data are detected.
Parameters are:
1) key data key (identification)
2) scope Data scope. Possible values are:
session, application. Default value is session
3) varyByParam Optional parameter. Provides a semicolon-separated
list of request parameters. By default is empty.
4) varyByHeader Optional parameter. Provides a semicolon-separated
list of request headers. By default is empty.
5) varyByCookie Optional parameter. Provides a semicolon-separated
list of cookies. By default is empty.
ifNotCached
Body tag. Executes own body in case of described cache does not exist.
Parameters are:
1) key data key (identification)
2) scope Data scope. Possible values are:
session, application. Default value is session
3) varyByParam Optional parameter. Provides a semicolon-separated
list of request parameters. By default is empty.
4) varyByHeader Optional parameter. Provides a semicolon-separated
list of request headers. By default is empty.
5) varyByCookie Optional parameter. Provides a semicolon-separated
list of cookies. By default is empty.
forEachKey
Body tag lets you iterate over existing cache keys. Tag executes own body for the each key
in the given scope. Parameters are:
1) id Optional parameter. Describes a name for the page scope varible (type is
java.lang.String) you can use as a reference to the current key. Default value is cacheKey.
2) scope Optional parameter. Describes a data scope. Possible values are:
session, application. Default value is session
for downloading:
Library: cachetag.jar Description: taglib.tld
© Coldbeans
Comments?
See also Coldtags suite - the largest collection of custom JSP tags.
|