Timer taglib ver. 1.3

    Custom JSP taglib. This taglib is similar to our Cache taglib, but lets you "cache" execution (processes) rather than responses. Body tag timer executes own body once per given interval (or seldom depends on incoming requests) for all the requests (application scope) or on per user base (session scope). Here is an idea:
 


<%@ taglib uri="taglib.tld" prefix="t" %>
<t:timer interval="10">
  <%
   pageContext.setAttribute("A", ""+new java.util.Date(), pageContext.APPLICATION_SCOPE);
  %>
</t:timer>

<%
  String serverData = (String)pageContext.getAttribute("A",pageContext.APPLICATION_SCOPE);
%>
<p>Time is: <%=serverData%>

Here our server side code (simply calculates server time in this example, but could be of course more complex) will be executed not often than once per 10 seconds for all the requests (application scope by default).

You can add an attribute id for your markup. In this case taglib will create also a page scope variable with the same name (type is java.lang.String). This variable will keep the evaluated body. The following example illustrates this:
 


<t:timer interval="5" id="T1">
Time is <%=new java.util.Date()%>
</t:timer>

<p><%=T1%>

Here the body will be calculated actually once per 5 seconds. And between the calculations the old values will be printed.

What if you need to cache only a part of your JSP page? For the whole page you can use for example Expire header. But what about the partial caching? Timer taglib helps you to do that. E.g. you can request your DB back-end not often than once per the given time and simply use old data for the intermediate requests. And this taglib lets you markup such a chunks (fragments) on your JSP page

Another usage lets you deal with the rate limits in public API's. E.g. Twitter API limits the usage for 3-rd party applications (70 requests per hour), Google geocode API does the same etc. Now you can wrap API calls in your JSP file with timer tag and automatically adjust the usage with the existing limitations.

In case of negative value for interval attribute tag simply evaluates own body.

Tags are:

timer

Body tag executes own body not often than once per the given interval. Attributes are:

1) id Optional attribute. Describes an id for your code (if you have more than one "cached" chunk) as well as the name for the page scope variable (type is java.lang.String) with the evaluated body.
2) interval Describes a minimal interval (in seconds).
3) scope Optional attribute. Describes a scope. Possible values are application or session. Default value is application.

clearTimer

Tag lets you clear rate restrictions. Attributes are:

1) id Optional attribute. Describes an id for your code (if you have more than one "cached" chunk).
2) scope Optional attribute. Describes a scope. Possible values are application or session. Default value is application.
3) cond Optional attribute. Describes a boolean value tag's behavior depends on. Default value is true (clear restrictions).
 

for downloading:

Library: timertag.jar    Description: taglib.tld

© Coldbeans Software      Comments?

See also Coldtags suite - the largest collection of custom JSP tags.

Also in Coldtags: