我的Blog我做主^_^

走向一条通往JAVA的不归路...

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  64 随笔 :: 68 文章 :: 77 评论 :: 0 Trackbacks

OSCache是当前运用最广的缓存方案,JBoss,Hibernate,Spring等都对其有支持,
下面简单介绍一下OSCache的配置和使用过程。

1.安装过程
http://www.opensymphony.com/oscache/download.html 下载合适的OSCache版本,
我下载的是oscache-2.0.2-full版本。
解压缩下载的文件到指定目录

从解压缩目录取得oscache.jar 文件放到 /WEB-INF/lib 或相应类库目录 目录中,
jar文件名可能含有版本号和该版本的发布日期信息等,如oscache-2.0.2-22Jan04.jar

如果你的jdk版本为1.3.x,建议在lib中加入Apache Common Lib 的commons-collections.jar包。
如jdk是1.4以上则不必

从src或etc目录取得oscache.properties 文件,放入src根目录或发布环境的/WEB-INF/classes 目录
如你需要建立磁盘缓存,须修改oscache.properties 中的cache.path信息 (去掉前面的#注释)。
win类路径类似为c:\\app\\cache
unix类路径类似为/opt/myapp/cache

拷贝OSCache标签库文件oscache.tld到/WEB-INF/classes目录。

现在你的应用目录类似如下:
$WEB_APPLICATION\WEB-INF\lib\oscache.jar
$WEB_APPLICATION\WEB-INF\classes\oscache.properties
$WEB_APPLICATION\WEB-INF\classes\oscache.tld


将下列代码加入web.xml文件中
<taglib>
<taglib-uri>oscache</taglib-uri>
<taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>
</taglib>

为了便于调试日志输出,须加入commons-logging.jar和log4j-1.2.8.jar到当前类库路径中

在src目录加入下面两个日志输出配置文件:
log4j.properties 文件内容为:
log4j.rootLogger=DEBUG,stdout,file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n

 

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=oscache.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n


log4j.logger.org.apache.commons=ERROR
log4j.logger.com.opensymphony.oscache.base=INFO


commons-logging.properties 文件内容为

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JCategoryLog

2.oscache.properties 文件配置向导

cache.memory
值为true 或 false ,默认为在内存中作缓存,
如设置为false,那cache只能缓存到数据库或硬盘中,那cache还有什么意义:)

cache.capacity
缓存元素个数

cache.persistence.class
持久化缓存类,如此类打开,则必须设置cache.path信息

cache.cluster 相关
为集群设置信息。

cache.cluster.multicast.ip为广播IP地址
cache.cluster.properties为集群属性


3.OSCache的基本用法

cache1.jsp 内容如下

<%@ page import="java.util.*" %>
<%@ taglib uri="oscache" prefix="cache" %>

<html>
<body>

没有缓存的日期: <%= new Date() %><p>
<!--自动刷新-->
<cache:cache time="30">
每30秒刷新缓存一次的日期: <%= new Date() %>
</cache:cache>
<!--手动刷新-->
<cache:cache key="testcache">
手动刷新缓存的日期: <%= new Date() %> <p>
</cache:cache>
<a href="/cache2.jsp">手动刷新</a>

</body>
</html>

cache2.jsp 执行手动刷新页面如下
<%@ taglib uri="oscache" prefix="cache" %>

<html>
<body>

缓存已刷新...<p>

<cache:flush key="testcache" scope="application"/>

<a href="/cache1.jsp">返回</a>

</body>
</html>

OSCache库提供的flush标记能够刷新缓冲内容。我们可以把下面的代码加入到处理用户动作且可能影响这一区域的页面之中:

<cache:flush key="testcache" scope="application"/>

当用户下次访问它时,testcache缓冲块将被刷新


你也可以通过下面语句定义Cache的有效范围,如不定义scope,scope默认为Applcation
<cache:cache time="30" scope="session">
...
</cache:cache>

4. 缓存过滤器 CacheFilter

package com.lvke.company.common;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.base.EntryRefreshPolicy;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.web.ServletCacheAdministrator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import com.opensymphony.oscache.web.filter.CacheHttpServletResponseWrapper;
import com.opensymphony.oscache.web.filter.ICacheGroupsProvider;
import com.opensymphony.oscache.web.filter.ICacheKeyProvider;
import com.opensymphony.oscache.web.filter.ExpiresRefreshPolicy;
import com.opensymphony.oscache.web.filter.ResponseContent;

public class CacheFilter implements Filter, ICacheKeyProvider, ICacheGroupsProvider {
    // Header
    public static final String HEADER_LAST_MODIFIED = "Last-Modified";
    public static final String HEADER_CONTENT_TYPE = "Content-Type";
    public static final String HEADER_CONTENT_ENCODING = "Content-Encoding";
    public static final String HEADER_EXPIRES = "Expires";
    public static final String HEADER_IF_MODIFIED_SINCE = "If-Modified-Since";
    public static final String HEADER_CACHE_CONTROL = "Cache-Control";
    public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";

    // Fragment parameter
    public static final int FRAGMENT_AUTODETECT = -1;
    public static final int FRAGMENT_NO = 0;
    public static final int FRAGMENT_YES = 1;

    // No cache parameter
    public static final int NOCACHE_OFF = 0;
    public static final int NOCACHE_SESSION_ID_IN_URL = 1;

    // Last Modified parameter
    public static final long LAST_MODIFIED_OFF = 0;
    public static final long LAST_MODIFIED_ON = 1;
    public static final long LAST_MODIFIED_INITIAL = -1;

    // Expires parameter
    public static final long EXPIRES_OFF = 0;
    public static final long EXPIRES_ON = 1;
    public static final long EXPIRES_TIME = -1;

    // Cache Control
    public static final long MAX_AGE_NO_INIT = Long.MIN_VALUE;
    public static final long MAX_AGE_TIME = Long.MAX_VALUE;

    // request attribute to avoid reentrance
    private final static String REQUEST_FILTERED = "__oscache_filtered";

    // the policy for the expires header
    private EntryRefreshPolicy expiresRefreshPolicy;

    // the logger
    private final Log log = LogFactory.getLog(this.getClass());

    // filter variables
    private FilterConfig config;
    private ServletCacheAdministrator admin = null;
    private int cacheScope = PageContext.APPLICATION_SCOPE; // filter scope - default is APPLICATION
    private int fragment = FRAGMENT_AUTODETECT; // defines if this filter handles fragments of a page - default is auto detect
    private int time = 60 * 60; // time before cache should be refreshed - default one hour (in seconds)
    private String cron = null; // A cron expression that determines when this cached content will expire - default is null
    private int nocache = NOCACHE_OFF; // defines special no cache option for the requests - default is off
    private long lastModified = LAST_MODIFIED_INITIAL; // defines if the last-modified-header will be sent - default is intial setting
    private long expires = EXPIRES_ON; // defines if the expires-header will be sent - default is on
    private long cacheControlMaxAge = -60; // defines which max-age in Cache-Control to be set - default is 60 seconds for max-age
    private ICacheKeyProvider cacheKeyProvider = this; // the provider of the cache key - default is the CacheFilter itselfs
    private ICacheGroupsProvider cacheGroupsProvider = this; // the provider of the cache groups - default is the CacheFilter itselfs

    /**
     * Filter clean-up
     */
    public void destroy() {
        //Not much to do...
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (log.isInfoEnabled()) {
            log.info("<cache>: filter in scope " + cacheScope);
        }

        // avoid reentrance (CACHE-128) and check if request is cacheable
        if (isFilteredBefore(request) || !isCacheable(request)) {
            chain.doFilter(request, response);
            return;
        }
        request.setAttribute(REQUEST_FILTERED, Boolean.TRUE);

        HttpServletRequest httpRequest = (HttpServletRequest) request;

        // checks if the response will be a fragment of a page
        boolean fragmentRequest = isFragment(httpRequest);

        // avoid useless session creation for application scope pages (CACHE-129)
        Cache cache;
        if (cacheScope == PageContext.SESSION_SCOPE) {
            cache = admin.getSessionScopeCache(httpRequest.getSession(true));
        } else {
            cache = admin.getAppScopeCache(config.getServletContext());
        }

        // generate the cache entry key
        String key = cacheKeyProvider.createCacheKey(httpRequest, admin, cache);

        try {
            ResponseContent respContent = (ResponseContent) cache.getFromCache(key, time, cron);

            if (log.isInfoEnabled()) {
                log.info("<cache>: Using cached entry for " + key);
            }

            boolean acceptsGZip = false;
            if ((!fragmentRequest) && (lastModified != LAST_MODIFIED_OFF)) {
                long clientLastModified = httpRequest.getDateHeader(HEADER_IF_MODIFIED_SINCE); // will return -1 if no header...

                // only reply with SC_NOT_MODIFIED
                // if the client has already the newest page and the response isn't a fragment in a page
                if ((clientLastModified != -1) && (clientLastModified >= respContent.getLastModified())) {
                    ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    return;
                }

                acceptsGZip = respContent.isContentGZiped() && acceptsGZipEncoding(httpRequest);
            }

            respContent.writeTo(response, fragmentRequest, acceptsGZip);
            // acceptsGZip is used for performance reasons above; use the following line for CACHE-49
            // respContent.writeTo(response, fragmentRequest, acceptsGZipEncoding(httpRequest));
        } catch (NeedsRefreshException nre) {
            boolean updateSucceeded = false;

            try {
                if (log.isInfoEnabled()) {
                    log.info("<cache>: New cache entry, cache stale or cache scope flushed for " + key);
                }

                CacheHttpServletResponseWrapper cacheResponse = new CacheHttpServletResponseWrapper((HttpServletResponse) response, fragmentRequest, time * 1000L, lastModified, expires, cacheControlMaxAge);
                chain.doFilter(request, cacheResponse);
                cacheResponse.flushBuffer();

                // Only cache if the response is cacheable
                if (isCacheable(cacheResponse)) {
                    // get the cache groups of the content
                    String[] groups = cacheGroupsProvider.createCacheGroups(httpRequest, admin, cache);
                    // Store as the cache content the result of the response
                    cache.putInCache(key, cacheResponse.getContent(), groups, expiresRefreshPolicy, null);
                    updateSucceeded = true;
                }
            } finally {
                if (!updateSucceeded) {
                    cache.cancelUpdate(key);
                }
            }
        }
    }


    public void init(FilterConfig filterConfig) {
        //Get whatever settings we want...
        config = filterConfig;
        admin = ServletCacheAdministrator.getInstance(config.getServletContext());

        // filter parameter time
        try {
            time = Integer.parseInt(config.getInitParameter("time"));
        } catch (Exception e) {
            log.info("Could not get init parameter 'time', defaulting to one hour.");
        }

        // setting the refresh period for this cache filter
        expiresRefreshPolicy = new ExpiresRefreshPolicy(time);

        // filter parameter scope
        try {
            String scopeString = config.getInitParameter("scope");

            if (scopeString.equals("session")) {
                cacheScope = PageContext.SESSION_SCOPE;
            } else if (scopeString.equals("application")) {
                cacheScope = PageContext.APPLICATION_SCOPE;
            } else if (scopeString.equals("request")) {
                cacheScope = PageContext.REQUEST_SCOPE;
            } else if (scopeString.equals("page")) {
                cacheScope = PageContext.PAGE_SCOPE;
            }
        } catch (Exception e) {
            log.info("Could not get init parameter 'scope', defaulting to 'application'.");
        }

        // filter parameter cron
        cron = config.getInitParameter("cron");

        // filter parameter fragment
        try {
            String fragmentString = config.getInitParameter("fragment");

            if (fragmentString.equals("no")) {
                fragment = FRAGMENT_NO;
            } else if (fragmentString.equals("yes")) {
                fragment = FRAGMENT_YES;
            } else if (fragmentString.equalsIgnoreCase("auto")) {
                fragment = FRAGMENT_AUTODETECT;
            }
        } catch (Exception e) {
            log.info("Could not get init parameter 'fragment', defaulting to 'auto detect'.");
        }

        // filter parameter nocache
        try {
            String nocacheString = config.getInitParameter("nocache");

            if (nocacheString.equals("off")) {
                nocache = NOCACHE_OFF;
            } else if (nocacheString.equalsIgnoreCase("sessionIdInURL")) {
                nocache = NOCACHE_SESSION_ID_IN_URL;
            }
        } catch (Exception e) {
            log.info("Could not get init parameter 'nocache', defaulting to 'off'.");
        }

        // filter parameter last modified
        try {
            String lastModifiedString = config.getInitParameter("lastModified");

            if (lastModifiedString.equals("off")) {
                lastModified = LAST_MODIFIED_OFF;
            } else if (lastModifiedString.equals("on")) {
                lastModified = LAST_MODIFIED_ON;
            } else if (lastModifiedString.equalsIgnoreCase("initial")) {
                lastModified = LAST_MODIFIED_INITIAL;
            }
        } catch (Exception e) {
            log.info("Could not get init parameter 'lastModified', defaulting to 'initial'.");
        }

        // filter parameter expires
        try {
            String expiresString = config.getInitParameter("expires");

            if (expiresString.equals("off")) {
                expires = EXPIRES_OFF;
            } else if (expiresString.equals("on")) {
                expires = EXPIRES_ON;
            } else if (expiresString.equalsIgnoreCase("time")) {
                expires = EXPIRES_TIME;
            }
        } catch (Exception e) {
            log.info("Could not get init parameter 'expires', defaulting to 'on'.");
        }

        // filter parameter Cache-Control
        try {
            String cacheControlMaxAgeString = config.getInitParameter("max-age");

            if (cacheControlMaxAgeString.equals("no init")) {
                    cacheControlMaxAge = MAX_AGE_NO_INIT;
            } else if (cacheControlMaxAgeString.equals("time")) {
                    cacheControlMaxAge = MAX_AGE_TIME;
            } else {
                    cacheControlMaxAge = Long.parseLong(cacheControlMaxAgeString);
                    if (cacheControlMaxAge >= 0) {
                            // declare the cache control as a constant
                            cacheControlMaxAge = - cacheControlMaxAge;
                    } else {
                    log.warn("Init parameter 'max-age' must be at least a positive integer, defaulting to 'time'. ");
                        cacheControlMaxAge = 60;
                    }
            }
        } catch (Exception e) {
            log.info("Could not get init parameter 'max-age', defaulting to 'time'.");
        }

        // filter parameter ICacheKeyProvider
        ICacheKeyProvider cacheKeyProvider = (ICacheKeyProvider)instantiateFromInitParam("ICacheKeyProvider", ICacheKeyProvider.class, this.getClass().getName());
        if (cacheKeyProvider != null) {
            this.cacheKeyProvider = cacheKeyProvider;
        }

        // filter parameter ICacheGroupsProvider
        ICacheGroupsProvider cacheGroupsProvider = (ICacheGroupsProvider)instantiateFromInitParam("ICacheGroupsProvider", ICacheGroupsProvider.class, this.getClass().getName());
        if (cacheGroupsProvider != null) {
            this.cacheGroupsProvider = cacheGroupsProvider;
        }

        // filter parameter EntryRefreshPolicy
        EntryRefreshPolicy expiresRefreshPolicy = (EntryRefreshPolicy)instantiateFromInitParam("EntryRefreshPolicy", EntryRefreshPolicy.class, this.expiresRefreshPolicy.getClass().getName());
        if (expiresRefreshPolicy != null) {
            this.expiresRefreshPolicy = expiresRefreshPolicy;
        }
    }

    private Object instantiateFromInitParam(String classInitParam, Class interfaceClass, String defaultClassName) {
                String className = config.getInitParameter(classInitParam);
                if (className == null) {
                        log.info("Could not get init parameter '" + classInitParam + "', defaulting to " + defaultClassName + ".");
                        return null;
                } else {
                        try {
                                Class clazz = Class.forName(className);
                                if (!interfaceClass.isAssignableFrom(clazz)) {
                                        log.error("Specified class '" + className + "' does not implement" + interfaceClass.getName() + ". Using default " + defaultClassName + ".");
                                        return null;
                                } else {
                                        return clazz.newInstance();
                                }
                        } catch (ClassNotFoundException e) {
                                log.error("Class '" + className + "' not found. Defaulting to " + defaultClassName + ".", e);
                        } catch (InstantiationException e) {
                                log.error("Class '" + className + "' could not be instantiated because it is not a concrete class. Using default class " + defaultClassName + ".", e);
                        } catch (IllegalAccessException e) {
                                log.error("Class '"+ className+ "' could not be instantiated because it is not public. Using default class " + defaultClassName + ".", e);
                        }
                        return null;
                }
        }


    public String createCacheKey(HttpServletRequest httpRequest, ServletCacheAdministrator scAdmin, Cache cache) {
        return scAdmin.generateEntryKey(null, httpRequest, cacheScope);
    }

    public String[] createCacheGroups(HttpServletRequest httpRequest, ServletCacheAdministrator scAdmin, Cache cache) {
        return null;
    }


    protected boolean isFragment(HttpServletRequest request) {
        if (fragment == FRAGMENT_AUTODETECT) {
            return request.getAttribute("javax.servlet.include.request_uri") != null;
        } else {
            return (fragment == FRAGMENT_NO) ? false : true;
        }
    }

    protected boolean isFilteredBefore(ServletRequest request) {
        return request.getAttribute(REQUEST_FILTERED) != null;
    }

    protected boolean isCacheable(ServletRequest request) {
        // TODO implement CACHE-137 and CACHE-141 here
        boolean cachable = request instanceof HttpServletRequest;

        if (cachable) {
            HttpServletRequest requestHttp = (HttpServletRequest) request;
            if (nocache == NOCACHE_SESSION_ID_IN_URL) { // don't cache requests if session id is in the URL
                cachable = !requestHttp.isRequestedSessionIdFromURL();
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("<cache>: the request " + ((cachable) ? "is" : "is not") + " cachable.");
        }

        return cachable;
    }

    protected boolean isCacheable(CacheHttpServletResponseWrapper cacheResponse) {
        // TODO implement CACHE-137 and CACHE-141 here
        // Only cache if the response was 200
        boolean cachable = cacheResponse.getStatus() == HttpServletResponse.SC_OK;

        if (log.isDebugEnabled()) {
            log.debug("<cache>: the response " + ((cachable) ? "is" : "is not") + " cachable.");
        }

        return cachable;
    }


    protected boolean acceptsGZipEncoding(HttpServletRequest request) {
        String acceptEncoding = request.getHeader(HEADER_ACCEPT_ENCODING);
        return  (acceptEncoding != null) && (acceptEncoding.indexOf("gzip") != -1);
    }

}


你可以在web.xml中定义缓存过滤器,定义特定资源的缓存。
<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>60</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>
上面定义将缓存所有.jsp页面,缓存刷新时间为60秒,缓存作用域为Session

 

注意,CacheFilter只捕获Http头为200的页面请求,即只对无错误请求作缓存,
而不对其他请求(如500,404,400)作缓存处理

OSCache - Tags http://www.opensymphony.com/oscache/wiki/JSP%20Tags.html
  
  注:本文引自
http://www.zhanglihai.com/
                                                                                                                                                  13:11:20 12-22-2006



posted on 2006-12-22 13:16 java_蝈蝈 阅读(853) 评论(0)  编辑  收藏 所属分类: OPEN SOURCE

只有注册用户登录后才能发表评论。


网站导航: