OSCache comes with a JSP tag library that controls all itsmajor functions. The tags are listed below with descriptions,attributes and examples of use.
For instructions on installing OSCache in a web application, see the Installation Guide. You just have to add the following line declaring the OSCache custom tag library for use on the jsp page:
Taglib URI <%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %> In OSCache releases before 2.1.1 you have to change the URI to /oscache, see CACHE-61. |
Summary
The tags are:
- cache - The main caching tag
- usecached - A nested tag to force using a cached version.
- flush - To flush caches programmatically.
- addgroup - It allows a single group name to be dynamically added to a cached block. This tag must be nested inside <cache:cache/>.
- addgroups- It allows a comma-delimited list of group names to be dynamicallyadded to a cached block. This tag must be nested inside<cache:cache/>.
Tag Legend
|
<cache></cache>
Description:
This is the main tag of OSCache. The body of the tag will be cachedaccording to the attributes specified. The first time a cache is usedthe body content is executed and cached.
Each subsequent time the tag is run, it will check to see if thecached content is stale. Content is considered stale due to one (ormore) of the following being true:
- The cached body content has been in the cache for longer than the time specified by the time or duration attribute.
- The cron attribute matches a date/time that is more recent than the time the body content was originally cached.
- The scope the body content is cached in was flushed since the content was originally cached.
If the cached body content is stale, the tag will execute the bodyagain and recache the new body content. Otherwise it will serve thecached content and the body will be skipped (resulting in a large speedincrease).
Attributes:
- key - [The request URI + query string] - The cachekey, any string. This should be unique for the given scope sinceduplicate keys will map to the same cache entry. The default value usesan escaped version of the URI and query string of the current page.
It is possible to specify multiple cache tags in the same page withoutspecifying keys - in this situation an index is appended to the key ofsubsequent tags. However this usage is discouraged since if the flow ofthe page is inconsistent, or cache tags are nested, the indicies willpotentially change each time the page is executed, resulting inseemingly jumbled cache entries. - scope - [application] - The scope of this cache (valid values are "application" and "session").
- time- [3600] The amount of time to cache this content for (in seconds).(Default is 3600 seconds, one hour). Supplying a negative value forthis attribute means that the content never expires.
- duration- [] - The duration of this cache (this attribute is an alternative totime). duration can be specified using Simple Date Format or ISO-8601date format.
- cron - [] - A cron expression thatdetermines when this cached content will expire. This allows content tobe expired at particular dates and/or times, rather than once a cacheentry reaches a certain age. See Cron Expressions to read more about this attribute.
- refresh- [false] - A boolean. If true, the cache will be refreshed regardlessof whether it is considered stale or not. This enables you to decide atruntime whether or not to rebuild the content.
- mode -[] - Setting this to "silent" will prevent the body of the tag frombeing written to the output stream. This may be useful if you want topreload the cache with content without actually displaying that contentto the user.
- groups - [] - A comma-delimited list ofgroup names can be provided. This allows cache entries to be groupedaccording to your needs. Grouping is useful when you have cachedcontent that depends on other parts of your application or data - whenthat dependency changes, flushing the relevant group will cause allcache entries in that group to be expired.
- language -[] - The ISO-639 language code to distinguish different content cachedunder an otherwise identical key. This is useful on a multilingual sitewhere the same JSP code is used to render content in differentlanguages depending on the current user's preferences.
- refreshpolicyclass- [] - A fully-qualified classname that extendscom.opensymphony.oscache.web.WebEntryRefreshPolicy. This allows you toprogrammatically determine whether cached content should be exipired.
- refreshpolicyparam- [] - Any arbitrary parameters that you need to pass through to therefreshpolicyclass. Specifying this attribute without specifying arefreshpolicyclass will have no effect.
Examples
This will cache the JSP content using the current URI as a key (which means this must be the only cache tag on the page to work). <cache:cache> ... some jsp content ... </cache:cache> This will cache the content with a constant key in the user's session scope. Any page that uses this key will access one shared cache. <cache:cache key="foobar" scope="session" > ... some jsp content ... </cache:cache> This will cache the content with a programmatic key (here a product ID) for 30 minutes. It will also refresh if the variable needRefresh is true. <cache:cache key="<%= product.getId() %> " time="1800" refresh="<%= needRefresh %> " > ... some jsp content ... </cache:cache> This will cache the content with a programmatic key, expiring it every morning at 2am. It will also refresh if the variable needRefresh is true. <cache:cache key="<%= product.getId() %> " cron="0 2 * * *" refresh="<%= needRefresh %> " > ... some jsp content ... </cache:cache> Suppose we had a dynamic list of categories that we pull from a database, and we also store currency exchange rates that get updated occasionally by calling a webservice. Suppose also that we have some content that displays information about both the categories and the current exchange rate values. The following example caches the body content and assigns it to two cache groups, "currencyData" and "categoryList" . When the exchange rates or the category list is updated, the appropriate group can be flushed causing this content (along with any other content associated with that group) to be exipired and then rebuilt the next time the page is processed: <cache:cache key="<%= product.getId() %> " time="-1" group="currencyData, categories" > ... display category list ... ... display currency information ... </cache:cache>
<usecached />
Description:
This tag is nested within a <cache> tag and tells its parent whether or not to use the cached version.
Attributes:
- use - [true] - A boolean that tells the tag whether ornot to use the cached version. (true = use cached version). This isuseful for
programmatic control of the cache.ExampleThis is a good example of error tolerance. If an exception occurs, the cached version of this content will be output instead. <cache:cache> <% try { %> ... some jsp content ... <% } catch (Exception e) { %> <cache:usecached /> <% } %> </cache:cache>
<flush />
Description:
This tag is used to flush caches at runtime. It is especiallyuseful because it can be coded into the administration section of yoursite so that admins can decide when to flush the caches.
Attributes:
- scope - [all] - This decides what scope will beflushed. Valid values are "application", "session" and null. A nullscope will flush all caches, regardless of their scope.
- key- [] - When a key and a scope are both given, just that single cacheentry will be marked to be flushed. When it is next accessed, it willbe refreshed. It is not valid to specify a key without a scope.
- group- [] - Specifying a group will cause all cache entries in the group tobe flushed. It is not valid to specify a group without a scope.
- pattern- [] - Any keys that contain this string will be flushed from thespecified scope. It is not valid to specify a pattern without a scope.(Note: pattern flushing has been deprecated - you are encouraged to usethe grouping functionality instead. It is more flexible and providesbetter performance.)
- language - [] - The ISO-639language code to distinguish different content cached under anotherwise identical key. This is useful on a multilingual site wherethe same JSP code is used to render content in different languagesdepending on the current user's preferences.
This will flush the application scope. <cache:flush scope="application" /> This will flush the cache entry with key "foobar" in the session scope. <cache:flush scope="session" key="foobar" /> This will flush all cache entries in the "currencyData" group from the application scope. <cache:flush scope="application" group="currencyData" />
<addgroup />
Description:
This tag must be nested inside a <cache:cache/> tag. Itallows a single group name to be dynamically added to a cached block.It is useful when the group a cached block should belong to are unknownuntil the block is actually rendered. As each group is 'discovered',this tag can be used to add the group to the block's group list.
Attributes:
- group - req - The name of the group to add the enclosing cache block to.
This will add the cache block with the key 'test1' to groups 'group1' and 'group2'. <cache:cache key="test1" > <cache:addgroup group="group1" /> ... some jsp content ... <cache:addgroup group="group2" /> ... some more jsp content ... </cache:cache>
<addgroups /> (New! Since 2.3)
Description:
This tag must be nested inside a <cache:cache/> tag. Itallows a comma-delimited list of groups names to be dynamically addedto a cached block with a single tag statement. As a group list is'discovered', this tag can be used to add the groups to the block'sgroup list.
Attributes:
- groups - req - The comma-delimited list of groups names to add the enclosing cache block to.
This will add the cache block with the key 'test1' to groups 'group1' and 'group2'. <cache:cache key="test1" > ... some jsp content ... <cache:addgroups groups="group1,group2" /> ... some jsp content ... </cache:cache> source: http://www.opensymphony.com/oscache/wiki/JSP%20Tags.html