com.opensymphony.oscache.web.tag
Class CacheTag

java.lang.Object
  extended by javax.servlet.jsp.tagext.TagSupport
      extended by javax.servlet.jsp.tagext.BodyTagSupport
          extended by com.opensymphony.oscache.web.tag.CacheTag
All Implemented Interfaces:
Serializable, javax.servlet.jsp.tagext.BodyTag, javax.servlet.jsp.tagext.IterationTag, javax.servlet.jsp.tagext.Tag, javax.servlet.jsp.tagext.TryCatchFinally

public class CacheTag
extends javax.servlet.jsp.tagext.BodyTagSupport
implements javax.servlet.jsp.tagext.TryCatchFinally

CacheTag is a tag that allows for server-side caching of post-processed JSP content.

It also gives great programatic control over refreshing, flushing and updating the cache.

Usage Example:


     <%@ taglib uri="oscache" prefix="cache" %>
     <cache:cache key="mycache"
                 scope="application"
                 refresh="false"
                 time="30">
              jsp content here... refreshed every 30 seconds
     </cache:cache>
 

Version:
$Revision: 331 $
Author:
Mike Cannon-Brookes, Todd Gochenour, Francois Beauregard, Alain Bergevin
See Also:
Serialized Form

Field Summary
private  String actualKey
          The actual key to use.
private  ServletCacheAdministrator admin
           
private  Cache cache
           
private static String CACHE_TAG_COUNTER_KEY
          The key under which the tag counter will be stored in the request
(package private)  boolean cancelUpdateRequired
          A flag to indicate whether a NeedsRefreshException was thrown and the update needs to be cancelled
private  String content
          The content that was retrieved from cache
private  String cron
          The cron expression that is used to expire cache entries at specific dates and/or times.
private static int DAY
           
private static int DEFAULT_TIMEOUT
           
private  List groups
          If no groups are specified, the cached content does not get put into any groups
private static int HOUR
           
private  String key
          if cache key is null, the request URI is used
private  String language
          The ISO-639 language code to distinguish different pages in application scope
private static org.apache.commons.logging.Log log
           
private static int MINUTE
           
private  int mode
          The cache mode.
private static int MONTH
           
private static int ONE_HOUR
           
private static int ONE_MINUTE
          Constants for refresh time
private  boolean refresh
          Whether the cache should be refreshed instantly
private  String refreshPolicyClass
          Class used to handle the refresh policy logic
private  String refreshPolicyParam
          Parameters that will be passed to the init method of the refresh policy instance.
private  int scope
          The cache scope to use
private static int SECOND
          Constants for time computation
private static int SILENT_MODE
          Cache modes
private  int time
          time (in seconds) before cache should be refreshed
private  boolean useBody
          used for subtags to tell this tag that we should use the cached version
private static int WEEK
           
private static int YEAR
           
 
Fields inherited from class javax.servlet.jsp.tagext.BodyTagSupport
bodyContent
 
Fields inherited from class javax.servlet.jsp.tagext.TagSupport
id, pageContext
 
Fields inherited from interface javax.servlet.jsp.tagext.BodyTag
EVAL_BODY_BUFFERED, EVAL_BODY_TAG
 
Fields inherited from interface javax.servlet.jsp.tagext.IterationTag
EVAL_BODY_AGAIN
 
Fields inherited from interface javax.servlet.jsp.tagext.Tag
EVAL_BODY_INCLUDE, EVAL_PAGE, SKIP_BODY, SKIP_PAGE
 
Constructor Summary
CacheTag()
           
 
Method Summary
(package private)  void addGroup(String group)
          Adds to the groups for this cache entry.
(package private)  void addGroups(String groupsString)
          Adds comma-delimited list of groups that the cache entry belongs to.
 int doAfterBody()
          After the cache body, either update the cache, serve new cached content or indicate an error.
 void doCatch(Throwable throwable)
           
 int doEndTag()
          The end tag - clean up variables used.
 void doFinally()
           
 int doStartTag()
          The start of the tag.
private  int parseDuration(String duration)
          Convert a SimpleDateFormat string to seconds Acceptable format are : 0s (seconds) 0m (minute) 0h (hour) 0d (day) 0w (week)
private  int parseISO_8601_Duration(String duration)
          Parse an ISO-8601 format date and return it's value in seconds
 void setCron(String cron)
          Sets the cron expression that should be used to expire content at specific dates and/or times.
 void setDuration(String duration)
          Set the time this cache entry will be cached for.
 void setGroups(String groups)
          Sets the groups for this cache entry.
 void setKey(String key)
          Set the key for this cache entry.
 void setLanguage(String language)
          Set the ISO-639 language code to distinguish different pages in application scope
 void setMode(String mode)
          Setting this to true prevents the cache from writing any output to the response, however the JSP content is still cached as normal.
 void setRefresh(boolean refresh)
          This method allows the user to programatically decide whether the cached content should be refreshed immediately.
 void setRefreshpolicyclass(String refreshPolicyClass)
          Class used to handle the refresh policy logic
 void setRefreshpolicyparam(String refreshPolicyParam)
          Parameters that will be passed to the init method of the refresh policy instance.
 void setScope(String scope)
          Set the scope of this cache.
 void setTime(int time)
          Set the time this cache entry will be cached for (in seconds)
 void setUseBody(boolean useBody)
          This controls whether or not the body of the tag is evaluated or used.
private  void validateDateFormat(String basicDate)
          Validate the basic date format
private  void validateHourFormat(String basicHour)
          Validate the basic hour format
 
Methods inherited from class javax.servlet.jsp.tagext.BodyTagSupport
doInitBody, getBodyContent, getPreviousOut, release, setBodyContent
 
Methods inherited from class javax.servlet.jsp.tagext.TagSupport
findAncestorWithClass, getId, getParent, getValue, getValues, removeValue, setId, setPageContext, setParent, setValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.servlet.jsp.tagext.Tag
getParent, setPageContext, setParent
 

Field Detail

SECOND

private static final int SECOND
Constants for time computation

See Also:
Constant Field Values

MINUTE

private static final int MINUTE
See Also:
Constant Field Values

HOUR

private static final int HOUR
See Also:
Constant Field Values

DAY

private static final int DAY
See Also:
Constant Field Values

WEEK

private static final int WEEK
See Also:
Constant Field Values

MONTH

private static final int MONTH
See Also:
Constant Field Values

YEAR

private static final int YEAR
See Also:
Constant Field Values

CACHE_TAG_COUNTER_KEY

private static final String CACHE_TAG_COUNTER_KEY
The key under which the tag counter will be stored in the request

See Also:
Constant Field Values

ONE_MINUTE

private static final int ONE_MINUTE
Constants for refresh time

See Also:
Constant Field Values

ONE_HOUR

private static final int ONE_HOUR
See Also:
Constant Field Values

DEFAULT_TIMEOUT

private static final int DEFAULT_TIMEOUT
See Also:
Constant Field Values

log

private static transient org.apache.commons.logging.Log log

SILENT_MODE

private static final int SILENT_MODE
Cache modes

See Also:
Constant Field Values

cancelUpdateRequired

boolean cancelUpdateRequired
A flag to indicate whether a NeedsRefreshException was thrown and the update needs to be cancelled


cache

private Cache cache

groups

private List groups
If no groups are specified, the cached content does not get put into any groups


admin

private ServletCacheAdministrator admin

actualKey

private String actualKey
The actual key to use. This is generated based on the supplied key, scope etc.


content

private String content
The content that was retrieved from cache


cron

private String cron
The cron expression that is used to expire cache entries at specific dates and/or times.


key

private String key
if cache key is null, the request URI is used


language

private String language
The ISO-639 language code to distinguish different pages in application scope


refreshPolicyClass

private String refreshPolicyClass
Class used to handle the refresh policy logic


refreshPolicyParam

private String refreshPolicyParam
Parameters that will be passed to the init method of the refresh policy instance.


refresh

private boolean refresh
Whether the cache should be refreshed instantly


useBody

private boolean useBody
used for subtags to tell this tag that we should use the cached version


mode

private int mode
The cache mode. Valid values are SILENT_MODE


scope

private int scope
The cache scope to use


time

private int time
time (in seconds) before cache should be refreshed

Constructor Detail

CacheTag

public CacheTag()
Method Detail

setDuration

public void setDuration(String duration)
Set the time this cache entry will be cached for. A date and/or time in either ISO-8601 format or a simple format can be specified. The acceptable syntax for the simple format can be any one of the following:

Parameters:
duration - The duration to cache this content (using either the simple or the ISO-8601 format). Passing in a duration of zero will turn off the caching, while a negative value will result in the cached content never expiring (ie, the cached content will always be served as long as it is present).

setCron

public void setCron(String cron)
Sets the cron expression that should be used to expire content at specific dates and/or times.


setGroups

public void setGroups(String groups)
Sets the groups for this cache entry. Any existing groups will be replaced.

Parameters:
groups - A comma-delimited list of groups that the cache entry belongs to.

addGroup

void addGroup(String group)
Adds to the groups for this cache entry.

Parameters:
group - A group to which the cache entry should belong.

addGroups

void addGroups(String groupsString)
Adds comma-delimited list of groups that the cache entry belongs to.

Parameters:
groups - A comma-delimited list of groups that the cache entry belongs to also.

setKey

public void setKey(String key)
Set the key for this cache entry.

Parameters:
key - The key for this cache entry.

setLanguage

public void setLanguage(String language)
Set the ISO-639 language code to distinguish different pages in application scope

Parameters:
language - The language code for this cache entry.

setRefresh

public void setRefresh(boolean refresh)
This method allows the user to programatically decide whether the cached content should be refreshed immediately.

Parameters:
refresh - Whether or not to refresh this cache entry immediately.

setMode

public void setMode(String mode)
Setting this to true prevents the cache from writing any output to the response, however the JSP content is still cached as normal.

Parameters:
mode - The cache mode to use.

setRefreshpolicyclass

public void setRefreshpolicyclass(String refreshPolicyClass)
Class used to handle the refresh policy logic


setRefreshpolicyparam

public void setRefreshpolicyparam(String refreshPolicyParam)
Parameters that will be passed to the init method of the refresh policy instance.


setScope

public void setScope(String scope)
Set the scope of this cache.

Parameters:
scope - The scope of this cache. Either "application" (default) or "session".

setTime

public void setTime(int time)
Set the time this cache entry will be cached for (in seconds)

Parameters:
time - The time to cache this content (in seconds). Passing in a time of zero will turn off the caching. A negative value for the time will result in the cached content never expiring (ie, the cached content will always be served if it is present)

setUseBody

public void setUseBody(boolean useBody)
This controls whether or not the body of the tag is evaluated or used.

It is most often called by the <UseCached /> tag to tell this tag to use the cached content.

Parameters:
useBody - Whether or not to use the cached content.
See Also:
UseCachedTag

doAfterBody

public int doAfterBody()
                throws javax.servlet.jsp.JspTagException
After the cache body, either update the cache, serve new cached content or indicate an error.

Specified by:
doAfterBody in interface javax.servlet.jsp.tagext.IterationTag
Overrides:
doAfterBody in class javax.servlet.jsp.tagext.BodyTagSupport
Returns:
The standard BodyTag return.
Throws:
javax.servlet.jsp.JspTagException - The standard exception thrown.

doCatch

public void doCatch(Throwable throwable)
             throws Throwable
Specified by:
doCatch in interface javax.servlet.jsp.tagext.TryCatchFinally
Throws:
Throwable

doEndTag

public int doEndTag()
             throws javax.servlet.jsp.JspTagException
The end tag - clean up variables used.

Specified by:
doEndTag in interface javax.servlet.jsp.tagext.Tag
Overrides:
doEndTag in class javax.servlet.jsp.tagext.BodyTagSupport
Returns:
The standard BodyTag return.
Throws:
javax.servlet.jsp.JspTagException - The standard exception thrown.

doFinally

public void doFinally()
Specified by:
doFinally in interface javax.servlet.jsp.tagext.TryCatchFinally

doStartTag

public int doStartTag()
               throws javax.servlet.jsp.JspTagException
The start of the tag.

Grabs the administrator, the cache, the specific cache entry, then decides whether to refresh.

If no refresh is needed, this serves the cached content directly.

Specified by:
doStartTag in interface javax.servlet.jsp.tagext.Tag
Overrides:
doStartTag in class javax.servlet.jsp.tagext.BodyTagSupport
Returns:
The standard doStartTag() return.
Throws:
javax.servlet.jsp.JspTagException - The standard exception thrown.

parseDuration

private int parseDuration(String duration)
Convert a SimpleDateFormat string to seconds Acceptable format are :

Parameters:
duration - The simple date time to parse
Returns:
The value in seconds

parseISO_8601_Duration

private int parseISO_8601_Duration(String duration)
                            throws Exception
Parse an ISO-8601 format date and return it's value in seconds

Parameters:
duration - The ISO-8601 date
Returns:
The equivalent number of seconds
Throws:
Exception

validateDateFormat

private void validateDateFormat(String basicDate)
                         throws Exception
Validate the basic date format

Parameters:
basicDate - The string to validate
Throws:
Exception

validateHourFormat

private void validateHourFormat(String basicHour)
                         throws Exception
Validate the basic hour format

Parameters:
basicHour - The string to validate
Throws:
Exception


Copyright © 2011 OpenSymphony. All Rights Reserved.