`
itsnack
  • 浏览: 39536 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

osCache JSP_Tags

阅读更多

OSCache comes with a JSP tag library that controls all its major 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 dynamically added to a cached block. This tag must be nested inside <cache:cache/>.
  Tag Legend

•For all listed attributes, req means it that attribute is required and any value in [ ] is a default value. All attributes can accept runtime expressions.
•From the title of the tag you can see whether or not the tag has a body:
•<tag></tag> tags always have a body
•<tag /> does not have a body
•<tag /></tag> can have a body or not depending on the circumstances.
 

<cache></cache>
Description:
This is the main tag of OSCache. The body of the tag will be cached according to the attributes specified. The first time a cache is used the body content is executed and cached.

Each subsequent time the tag is run, it will check to see if the cached content is stale. Content is considered stale due to one (or more) 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 body again and recache the new body content. Otherwise it will serve the cached content and the body will be skipped (resulting in a large speed increase).

Attributes:
•key - [The request URI + query string] - The cache key, any string. This should be unique for the given scope since duplicate keys will map to the same cache entry. The default value uses an escaped version of the URI and query string of the current page.
It is possible to specify multiple cache tags in the same page without specifying keys - in this situation an index is appended to the key of subsequent tags. However this usage is discouraged since if the flow of the page is inconsistent, or cache tags are nested, the indicies will potentially change each time the page is executed, resulting in seemingly 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 for this attribute means that the content never expires.
•duration - [] - The duration of this cache (this attribute is an alternative to time). duration can be specified using Simple Date Format or ISO-8601 date format.
•cron - [] - A cron expression that determines when this cached content will expire. This allows content to be expired at particular dates and/or times, rather than once a cache entry reaches a certain age. See Cron Expressions to read more about this attribute.
•refresh - [false] - A boolean. If true, the cache will be refreshed regardless of whether it is considered stale or not. This enables you to decide at runtime whether or not to rebuild the content.
•mode - [] - Setting this to "silent" will prevent the body of the tag from being written to the output stream. This may be useful if you want to preload the cache with content without actually displaying that content to the user.
•groups - [] - A comma-delimited list of group names can be provided. This allows cache entries to be grouped according to your needs. Grouping is useful when you have cached content that depends on other parts of your application or data - when that dependency changes, flushing the relevant group will cause all cache entries in that group to be expired.
•language - [] - The ISO-639 language code to distinguish different content cached under an otherwise identical key. This is useful on a multilingual site where the same JSP code is used to render content in different languages depending on the current user's preferences.
•refreshpolicyclass - [] - A fully-qualified classname that extends com.opensymphony.oscache.web.WebEntryRefreshPolicy. This allows you to programmatically determine whether cached content should be exipired.
•refreshpolicyparam - [] - Any arbitrary parameters that you need to pass through to the refreshpolicyclass. Specifying this attribute without specifying a refreshpolicyclass 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 or not to use the cached version. (true = use cached version). This is useful for
programmatic control of the cache.
Example
This 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 especially useful because it can be coded into the administration section of your site so that admins can decide when to flush the caches.

Attributes:
•scope - [all] - This decides what scope will be flushed. Valid values are "application", "session" and null. A null scope will flush all caches, regardless of their scope.
•key - [] - When a key and a scope are both given, just that single cache entry will be marked to be flushed. When it is next accessed, it will be refreshed. It is not valid to specify a key without a scope.
•group - [] - Specifying a group will cause all cache entries in the group to be flushed. It is not valid to specify a group without a scope.
•pattern - [] - Any keys that contain this string will be flushed from the specified scope. It is not valid to specify a pattern without a scope. (Note: pattern flushing has been deprecated - you are encouraged to use the grouping functionality instead. It is more flexible and provides better performance.)
•language - [] - The ISO-639 language code to distinguish different content cached under an otherwise identical key. This is useful on a multilingual site where the same JSP code is used to render content in different languages depending on the current user's preferences.
Example
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. It allows 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 unknown until 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.
Example
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. It allows a comma-delimited list of groups names to be dynamically added to 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's group list.

Attributes:
•groups - req - The comma-delimited list of groups names to add the enclosing cache block to.
Example
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

 

分享到:
评论

相关推荐

    oscache-2.1.1-full.zip_full_oscache_oscache 2_oscache2

    **osCache 2.1.1 全功能详解** osCache 是一款强大的缓存解决方案,尤其在Java开发中被广泛使用。它不仅提供了类似于Map的数据结构操作,还具备内置的集群支持,使得数据缓存可以在分布式环境中高效地进行。这个...

    oscache2.1_ful

    `oscache_full` 压缩包可能包含以下内容: 1. `oscache.jar`: OSCache 主库文件。 2. `oscache.xml`: 示例配置文件。 3. `docs`: 官方文档,包括用户指南和API参考。 4. `lib`: 相关依赖库。 5. `src`: 源代码,便于...

    OSCache jsp 缓存详解

    看就知道......................

    Oscache_入门

    ### Oscache_入门详解 #### 一、Oscache简介 Oscache是一种高效且功能丰富的缓存技术,主要用于改善Web应用的性能。通过缓存Web页面的输出结果,能够显著提升系统的响应速度和整体运行效率。Oscache由OpenSymphony...

    oscache-JSP缓存

    **osCache - JSP缓存技术详解** osCache是由OpenSymphony开源组织开发的一个高效、易用的缓存解决方案,特别适用于Java Web应用程序,尤其是JSP页面。它为开发者提供了一种在JSP页面内部实现快速内存缓存的机制,...

    oscache(JSP定制标记应用)

    javaweb做页面缓存常用,OSCache是一个工业级的J2EE缓存实现。OSCache不但能缓存java对象,还可以缓存页面,http请求和二进制内容,例如pdf文件等。通过应用OSCache,我们不但可以实现通常的Cache功能,还能够改善...

    OSCache缓存jsp例子

    在本文中,我们将深入探讨OSCache在缓存JSP页面方面的应用,以及如何利用它来优化Web应用。 首先,我们需要理解缓存的基本概念。缓存是一种存储技术,用于临时存储频繁访问的数据,以便快速检索。在Web开发中,JSP...

    Oscache框架的搭建步骤

    6. **集成到JSP页面**:在JSP页面中引入Oscache标签库,使用`&lt;%@taglib uri="oscache" prefix="cache"%&gt;`声明,其中`uri`值应与`web.xml`中配置的`&lt;taglib-uri&gt;`一致。 7. **实现缓存逻辑**:在JSP页面中利用...

    OSCache需要的包

    在这个特定的案例中,`oscache.tld` 提供了关于OSCache JSP标签的元数据,如标签的名称、属性、行为等,使得开发者能够在JSP页面上方便地使用OSCache提供的缓存功能。 `oscache.properties` 是OSCache的配置文件,...

    oscache-2.4.1-full

    OSCache是OpenSymphony开发的一款高效、开源的Java缓存框架,主要应用于Web应用程序,特别是JSP环境。其核心功能是提供内存级别的缓存服务,从而显著提高网页的加载速度和减少数据库的压力。标题"oscache-2.4.1-full...

    oscache详细配置文档

    为了在 JSP 页面中使用 OSCache 功能,需要将 `soscache.tld` 文件添加到 CLASSPATH 中,通常放在 `src/webapp/WEB-INF/classes` 或 `etc` 目录下。然后在 `web.xml` 文件中添加标签库定义,如下所示: ```xml ...

    OSCache学习例子 实例

    OSCache是开源的Java缓存解决方案,主要用于提高Web应用程序的性能和响应速度。它是由OpenSymphony团队开发的,能够缓存对象、SQL查询结果甚至整个页面,避免了频繁访问数据库或执行昂贵的计算,从而降低了系统负载...

    oscache的例子

    OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存的功能。OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存...

    oscache缓存配置

    《osCache缓存配置详解》 osCache是Java平台上的一个高效、易用的缓存解决方案,它由Tuckey组织开发,广泛应用于各种Web应用中,以提高数据读取速度,减轻数据库压力。osCache的核心功能是提供了一个内存中的对象...

    OSCache配置说明文档

    1. 缓存任何对象:无论是部分JSP页面、HTTP请求,还是自定义的Java对象,OSCache都能进行有效缓存。 2. 完整的API:提供丰富的API接口,便于开发者根据需求控制缓存行为。 3. 永久缓存:支持将缓存数据持久化到硬盘...

    oscache-2.1.jar

    oscache-2.1.jar oscache-2.1.jar

    oscache缓存技术入门实例

    osCache 是一个开源的、基于Java的缓存框架,它为Java应用程序提供了高效且可配置的内存缓存功能。在本文中,我们将深入探讨osCache的基本概念、工作原理以及如何在实际项目中进行配置和使用。 一、osCache基本概念...

    oscache,缓存机制的使用

    - `oscache.tld`定义了oscache提供的标签库结构,便于在JSP页面中调用缓存相关的标签。 #### 3. 修改web.xml 在项目的`web.xml`文件中添加对oscache标签库的支持,具体代码如下: ```xml &lt;jsp-config&gt; ...

Global site tag (gtag.js) - Google Analytics