`
精神分裂
  • 浏览: 28354 次
  • 性别: Icon_minigender_1
  • 来自: 二次元世界
社区版块
存档分类
最新评论

OSCache - CacheFilter工作原理

阅读更多
系统启动 – CacheFilter
OSCache页面缓存在使用时需要在web.xml中配置CacheFilter,在容器中,Filter先于Servlet启动,下面看看CacheFilter在容器启动时,做了哪些工作:
首先需要了解OSCache的几个类:
ServletCacheAdministrator -- 该类用于创建、刷新、管理缓存。
Config -- 负责获取缓存配置文件,即oscache.properties。如果使用默认的构造函数,这个类会从properties文件中加载缓存配置。
Cache -- 为缓存本身提供一个接口。这个类的实例会根据它的构造参数创建一个缓存。
/** 初始化Filter
 * 获取web.xml文件的配置,创建ServletCacheAdministrator对象
 * 获取web.xml文件中CacheFilter的初始化配置 */
public void init(FilterConfig filterConfig) {
    config = filterConfig;
    //初始化了ServletCacheAdministrator对象
    admin = ServletCacheAdministrator.getInstance(config.getServletContext());
    try {
        time = Integer.parseInt(config.getInitParameter("time"));
    } catch (Exception e) {
        log.info("Could not get init paramter 'time', defaulting to one hour.");
    }
    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 paramter 'scope', defaulting to 'application'");
    }
}


从以上可以看出,OSCache随系统启动,仅仅做了三个步骤:
1. 创建了一个ServletCacheAdministrator对象;
2. 获取web.xml配置文件中刷新时间
3. 获取web.xml配置文件中刷新范围
在创建ServletCacheAdministrator对象时,OSCache进行了如下工作:
public static ServletCacheAdministrator getInstance(ServletContext context) {
    return getInstance(context, null);
}
public static ServletCacheAdministrator getInstance(ServletContext context, Properties p) {
    ServletCacheAdministrator admin = null;
    admin = (ServletCacheAdministrator) context.getAttribute(CACHE_ADMINISTRATOR_KEY);
    //检测Web Application中是否包含名为__oscache_admin的属性,启动时不包含,故admin为null
    if (admin == null) {
        admin = new ServletCacheAdministrator(context, p); //调用私有化构造函数
context.setAttribute(CACHE_ADMINISTRATOR_KEY, admin); //将admin加入__oscache_admin属性
        if (log.isInfoEnabled()) {
            log.info("Created new instance of ServletCacheAdministrator");
        }
        admin.getAppScopeCache(context);
//获取application范围缓存的方法,在这个方法中,创建并初始化了Cache和Config对象
    }
    return admin;
}


ServletCacheAdministrator的构造函数:
private ServletCacheAdministrator(ServletContext context, Properties p) {
    super(p);
    /* 父类方法做了如下工作:
     * 从classpath下读取OSCache属性文件
     * 通过属性文件的配置初始化缓存参数 */
config.set(HASH_KEY_CONTEXT_TMPDIR, 
context.getAttribute("javax.servlet.context.tempdir"));
    //设置context.tempdir = D:\Tomcat6\work\Catalina\localhost\OSCacheWebTest
    flushTimes = new HashMap();
    initHostDomainInKey(); //私有变量boolean useHostDomainInKey = null
}

通过以上方法,在ServletContext上下文中,设置了如下属性:
context.setAttribute(CACHE_ADMINISTRATOR_KEY, admin);
将创建的ServletCacheAdministrator对象放入名为__oscache_admin的属性
context.setAttribute(getCacheKey(), cache);
将创建的Cache对象放入名为__oscache_cache的属性

缓存页面首次打开与刷新 – doFilter
首先需要了解OSCache的几个类:
ResponseContent:在缓存中用byte数组形式保留servlet响应对象。
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException, IOException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    //产生一个缓存目录的key:/OSCacheWebTest/cache1.jsp_GET_
    String key = admin.generateEntryKey(null, httpRequest, cacheScope);
    //获取指定范围的缓存
Cache cache = admin.getCache(httpRequest, cacheScope);
以上语句进行了如下工作:
1.	定位缓存范围:session还是application(这里在配置文件中设置的是session)
2.	检查HttpSession对象中名为__oscache_cache的属性是否为空
如果为空(第一次打开页面):创建一个缓存,将其放入HttpSession对象的__oscache_cache属性中
不为空(刷新页面):直接从HttpSession对象中获取__oscache_cache属性的值,即直接获取缓存
    try {
      	//通过缓存中的key得到相应的对象
        ResponseContent respContent = (ResponseContent) cache.getFromCache(key, time);
        respContent.writeTo(response); //将缓存数据写入到ServletResponse
    } catch (NeedsRefreshException nre) {
        ………………
        } finally {
            ………………
        }
    }
}
分享到:
评论

相关推荐

    oscache-2.1.jar

    oscache-2.1.jar oscache-2.1.jar

    JavaEE源代码 oscache-2.1

    JavaEE源代码 oscache-2.1JavaEE源代码 oscache-2.1JavaEE源代码 oscache-2.1JavaEE源代码 oscache-2.1JavaEE源代码 oscache-2.1JavaEE源代码 oscache-2.1JavaEE源代码 oscache-2.1JavaEE源代码 oscache-2.1JavaEE源...

    oscache-2.4.1-full.rar

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

    oscache-2.4.jar

    oscache-2.4.jar osCache框架的jar文件,版本是2.4的噢! --------java爱好者 java交流群:166256747, 分享自己的技术是一种美德!

    oscache-2.4.1-full

    OSCache由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能。 该版本为2.4.1

    ajax4jsf-1.1.0.jar,oscache-2.3.2.jar

    A4J User Guide提到的两个jar文件,在网上找了很久,现在拿出来和大家一起分享。

    mybatis-oscache-1.0.0.jar

    Mybatis中配置OSCache缓存所需要的jar

    oscache-2.3.jar

    oscache-2.3.jar

    黎活明-传智播客-OSCache-PPT

    黎活明-传智播客-OSCache-PPT,上课用的PPT

    oscache-2.2jar包

    oscache-2.2jar包

    缓存技术 oscache-2.3.2.jar包下载

    缓存技术 oscache-2.3.2.jar包下载

    oscache-java缓存框架

    oscache-java缓存框架插件和安装教程,使用教程一步到位

    oscache-2.1.0.jar

    oscache-2.1.0.jar

    oscache-2.4.1-javadoc.chm

    自制CHM版的API文档,带索引。 注:如果各位下载后打开或无法显示页面,请在CHM文件右键—属性—解除锁定即可。

    Oscache-入门教程.doc

    OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存解决方案。 OSCache是当前运用最广的缓存方案,JBoss,Hibernate,Spring等都对其有支持。 Oscache的使用非常方便,...

    oscache-2.1.1-full.zip_full_oscache_oscache 2_oscache2

    一个不错的cache,不但能够实现map的所有操作,而却具有内嵌的集群支持能力

    oscache-JSP缓存

    OSCache由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能。

    oscache-2.0.jar

    解压即可看到全部内容。

Global site tag (gtag.js) - Google Analytics