OSCache 官方文档翻译:servlet 缓存过滤器 cacheFilter

原文链接http://blog.csdn.net/citi007/article/details/6697889

翻译:Allchin

email:citi007@126.com

Introduction :介绍

OSCache comes with a servlet filter that enables you to transparently cache entire pages of your website, and even binary files. 
Caching of binary files is extremely useful when they are generated dynamically, e.g. PDF files or images. In addition by using the 
last modified header the transaction overhead and server load is reduced excellently which speed ups the server response time.
OSCache 拥有一个 servlet 过滤器,你可以用它在你的网站上显示的缓存过期页面,或者二进制文件.缓存二进制文件是在动态创建
它们后一种极为有用的做法,比如 pdf 文件或者图像。由于使用最后编辑的响应头来处理,所以避免了对同一次响应的多次处理,

减小了负载,缩短了响应时间。


How to configure OSCache to cache entire servlet responses is described in the configuration page of the CacheFilter. This short 
tutorial should demonstrate how to make your web site more responsive, and save load on your server. Using the CacheFilter the 
user will appreciate a faster loading site and will visit it more often.
如何配置 OSCache 去缓存过期 servlet 响应在配置缓存过滤器页面有描述.这个简短的实例将会示范如何让你的网站更快响应,如
何减小服务器负载。使用 Cache 过滤器用户将会感受到更快的加载站点并且更加频繁的访问。
Improvements:改进
Major improvements have been made to the CacheFilter in the releases 2.2 and 2.3:
主要的改进已经在 2.2 和 2.3 的基础上完成了

 


Default initialization of the last modified header which reduces transaction overhead and server load 
默认初始化对最后修改的响应头的缓存将会减少事物过度处理与负载.
CRON expressions to expire content at specific dates and/or times 
CRON 表达式可以配置在特定的日期去刷新内容。
Preserving more http headers, e.g. the expires header 
保存更多的 http 相应头,比如过期的头
Special handling for fragments of a page 
对页面碎片进行特殊处理
Custom cache key generation by subclassing CacheFilter or by implementing a special interface 
继承 CacheFilter 或者实现特定的接口可以自定义缓存主键。
Custom cache groups generation by subclassing CacheFilter or by implementing a special interface 
继承 CacheFilter 或者实现特定的接口可以自定义缓存组.
Support of GZip filters in the filter chain 
可以在过滤器链表中使用 GZip 过滤器.
Avoids session creation for application scope pages 
为应用范围缓存的页面可以避免在用户范围重复缓存.
Reduced memory consumption 
减小内存消耗.
Multiple matching cache filters won't dead­lock the response anymore 
多个缓存过滤器不再导致响应死锁。
The cache won't be serve the same response twice before the client begins to cache it anymore 
在客户端开始缓存一个相应的时候不会对相同的相应缓存多次了.
Cacheable Content:可缓存的内容
Cacheable content
Note that the filter will only cache content that has a status of 200 (HttpServletResponse.SC_OK).
注意缓存过滤器仅仅会缓存状态为 200 的响应内容(就是响应成功的)
Configuring the filter:配置过滤器
Example 1:例 1
To configure the filter, add something like the following to your web.xml file (obviously you will want to set the URL pattern to 
match only the content you want to cache; this example will cache all JSP pages for 10 minutes in session scope):
为了配置缓存,在 web。xml 中加入如下内容(很明显你需要设置 URL 表达式去匹配那些你想要缓存的内容;下面的例子将会缓存所
有的 JSP 页面 10 分钟)
<filter>
    <filter­name>CacheFilter</filter­name>
    <filter­class>com.opensymphony.oscache.web.filter.CacheFilter</filter­class>
    <init­param>
        <param­name>time</param­name>
        <param­value>600</param­value>
    </init­param>
    <init­param>
        <param­name>scope</param­name>
        <param­value>session</param­value>
    </init­param>
</filter>
<filter­mapping>
    <filter­name>CacheFilter</filter­name>
    <url­pattern>*.jsp</url­pattern>

</filter­mapping>


The default duration is one hour and the default scope for the cache is application scope. You can change these settings using 
initialization parameters.

默认的持久化时间是 1 小时,范围是 application.你可以通过改变初始化参数来改变这些设置。


Example 2:例 2
The initialization of the last modified header based on the current time reduces transaction overhead and server load, because the 
browser can ask the server if the cached content in the browser cache was changed on the server since the last request. If the 
content wasn't changed , the server will response with the status 304 (not modified).
对最后一次修改的相应头的初始化操作将会减少对相应的过度处理与服务器负载,由于浏览器可以询问服务器是否缓存的内容已

经在上次请求后被改变。如果没变,服务器就会返回 304(没变).


Furthermore if the expires parameter is the set to time, the server will send the date and time after which the content is 
considered stale. Then common browsers won't request the server anymore until the cached content is considered stale. The 
example will cache the content for one hour by default and the expires date and time will be calculated based on the creation 
time and the time parameter (default is one hour).
更深入的过期参数是设置时间,服务器将会在内容被判定过期后发送日期与时间。然后大多数浏览器不会再请求服务器,直到缓
存内容被判定过期。这个例子将会默认缓存内容 1 小时并且过期时间将会根据创建的时间和时间参数来计算(默认 1 小时)
<filter>
    <filter­name>CacheFilterStaticContent</filter­name>
    <filter­class>com.opensymphony.oscache.web.filter.CacheFilter</filter­class>
    <init­param>
        <param­name>expires</param­name>
        <param­value>time</param­value>
    </init­param>
</filter>
<filter­mapping>
    <filter­name>CacheFilterStaticContent</filter­name>
    <url­pattern>*.jsp</url­pattern>

</filter­mapping>


Using the filter:使用过滤器
Example 1: IcacheKeyProvider  例 1:I 缓存主键提供器

A simple example how to use the ICacheKeyProvider parameter of the CacheFilter. The cache key in constructed with the http 
request URI and with two request parameters pageid and pagination.
一个简单的例子如何使用缓存过滤器的 ICacheKeyProvier 参数。缓存的主键由 http 请求的 URI 和两个请求的参数 pageid 与
pagination 构成.
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.oscache.web.filter.ICacheKeyProvider;
public class ExampleCacheKeyProvider implements ICacheKeyProvider {
    public String createCacheKey(HttpServletRequest httpRequest, ServletCacheAdministrator scAdmin, Cache cache) {
        // buffer for the cache key
        StringBuffer buffer = new StringBuffer(100);
        
        // part 1 of the key: the request uri
        buffer.append(httpRequest.getRequestURI());
        
        // separation
        buffer.append('_');
        // part 2 of the key: the page id
        buffer.append(httpRequest.getParameter("pageid"));
        
        // separation
        buffer.append('_');
        
        // part 3 of the key: the pagination
        buffer.append(httpRequest.getParameter("pagination"));
        
        return buffer.toString();
    }
}
You can use session attributes values for the cache key also, if request parameters aren't available or e.g. security settings have to 
be add to the cache key.

你也可以使用 session 属性值作为缓存主键,如果请求参数不能够被使用或者例如安全设置必须被添加到缓存主键上。


Example 2: Flush:例子 2,刷出
The flush example shows how to flush a CacheFilter with scope application based on group names. In this example the http servlet 
request of the user is required to get the cache object.
输出例子展示了如何刷出一个带有 appclication 范围并基于组名的缓存过滤器.在这个例子中用户的 http servlet 请求将被需求拿取
缓存对象.
import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
public class OSCacheAdmin {
    
    /**
     * flush the CacheFilter according to dependent group
     *
     * @param request the HttpServletRequest of the user
     * @param groupNames a string collection of group names
     */
    public static void flushCacheGroup(HttpServletRequest request, Collection groupNames) {
        Cache cache = ServletCacheAdministrator.getInstance(request.getSession().getServletContext()).getCache(request, 
PageContext.APPLICATION_SCOPE); 
        Iterator groups = groupNames.iterator();
        while (groups.hasNext()) {
            String group = (String) groups.next();
            cache.flushGroup(group);
        }
    }
}
If you're CacheFilter is running with scope session, you have to get the cache as follows:
如果你在 session 范围使用 CacheFilter,你就必须按照下面的方式获取缓存
Cache cache = ServletCacheAdministrator.getInstance(request.getSession(true).getServletContext()).getCache(request, 
PageContext.SESSION_SCOPE);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值