1 下载地址
http://www.opensymphony.com/oscache/
2 把下载的jar 加入到你的build path 里面。 建议直接放到 tomcat/shared/lib 目录下面
3 在 web.xml 里面增加配置
- <taglib>
- <taglib-uri>oscache</taglib-uri>
- <taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>
- </taglib>
<taglib>
<taglib-uri>oscache</taglib-uri>
<taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>
</taglib>
4 在你的jsp 页面里增加如下标签
- <%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache"%>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache"%>
5 在需要缓冲的部分,采用如下标签
-
- <cache:cache key="myPageCachekey" groups="myDefaultGroup" scope="application" time="1800">
- 。。。 这中间是你要缓冲的内容
- </cache:cache>
<cache:cache key="myPageCachekey" groups="myDefaultGroup" scope="application" time="1800">
。。。 这中间是你要缓冲的内容
</cache:cache>
6 刷新缓冲
- <cache:flush scope="application" group="myDefaultGroup" />
<cache:flush scope="application" group="myDefaultGroup" />
其中的
key - [默认为请求的 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.
缓冲的key,可以是任何的字符串。在一个范围内是唯一的,因为相同的key代表了相同的缓冲入口。默认使用请求的URI和请求的字符串(问号后面的部分)
scope - [application] - The scope of this cache (valid values are "application" and "session").
范围,有application和session, 默认为 application
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.
缓冲内容的时间(秒)。默认为1小时,3600秒,如果为-1则内容你永远不过期
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.
另外一种设置缓冲时间的方法。可以用简单日期格式或者ISO-8601的日期格式
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.
设置为[silent]可以让你提前把内容读取,但不会输出到。
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.
传递给刷新策略类的参数