OSCache源码阅读(一)

自己在开发JavaEE的项目时,采用了基于Spring MVC + MyBatis +Sitemesh +AngularJS + semantic-ui的组合,使用maven作为项目管理、SVN作为代码版本控制工具。

前台通过ajax从后台获取数据,再在前台进行DOM渲染,于是,数据加载的时候,页面会有一定程度的"空白"现象。

为了解决这个问题,最好的办法的是把动态页面静态化,页面只进行一次渲染,但这种方式,略显麻烦,于是自己采取了片段化缓存和数据缓存的方式,加快

页面渲染和数据加载。

1.配置

页面片段化缓存

为了统一页面风格,我使用Sitemesh来做页面布局。于是,页首的导航栏、菜单栏、页面底部的版权信息,可以直接缓存掉,比如下图(-1表示永久缓存)。

打开页面的速度确实提升很快。

数据缓存

对于页面、数据接口进行全局缓存,在web.xml配置如下:

<!-- osCacheFilter -->
	<filter>
		<filter-name>osCacheFilter</filter-name>
		<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
		<init-param>
			<param-name>time</param-name>
			<param-value>7200</param-value><!-- 2h -->
		</init-param>
		<init-param>
			<param-name>scope</param-name>
			<param-value>application</param-value><!-- default -->
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/</url-pattern><!-- home page -->
	</filter-mapping>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/group/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/article/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/case/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/disease/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/doctor/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/drug/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/hospital/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/page/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>osCacheFilter</filter-name>
		<url-pattern>/u/*</url-pattern>
	</filter-mapping>

2.源码

个人从github上得到相关源码,地址:https://github.com/cdemoustier/opensymphony-oscache-backup

web.xml这么配置参数,是如何被OSCache得到呢?

先看com.opensymphony.oscache.web.filter.CacheFilter类,它的init方法如下:

/**
	 * Initialize the filter. This retrieves a {@link ServletCacheAdministrator}
	 * instance and configures the filter based on any initialization
	 * parameters.
	 * <p>
	 * The supported initialization parameters are:
	 * <ul>
	 *
	 * <li><b>oscache-properties-file</b> - the properties file that contains
	 * the OSCache configuration options to be used by the Cache that this
	 * Filter should use.</li>
	 *
	 * @param filterConfig
	 *            The filter configuration
	 */
	public void init(FilterConfig filterConfig) {
		// Get whatever settings we want...
		config = filterConfig;

		log.info("OSCache: Initializing CacheFilter with filter name "
				+ config.getFilterName());

		// setting the request filter to avoid reentrance with the same filter
		requestFiltered = REQUEST_FILTERED + config.getFilterName();
		log.info("Request filter attribute is " + requestFiltered);

		// filter Properties file
		Properties props = null;
		try {
			String propertiesfile = config
					.getInitParameter("oscache-properties-file");

			if (propertiesfile != null && propertiesfile.length() > 0) {
				props = Config.loadProperties(
						propertiesfile,
						"CacheFilter with filter name '"
								+ config.getFilterName() + "'");
			}
		} catch (Exception e) {
			log.info("OSCache: Init parameter 'oscache-properties-file' not set, using default.");
		}
		admin = ServletCacheAdministrator.getInstance(
				config.getServletContext(), props);

		// filter parameter time
		String timeParam = config.getInitParameter("time");
		if (timeParam != null) {
			try {
				setTime(Integer.parseInt(timeParam));
			} catch (NumberFormatException nfe) {
				log.error("OSCache: Unexpected value for the init parameter 'time', defaulting to one hour. Message="
						+ nfe.getMessage());
			}
		}

		// filter parameter scope
		String scopeParam = config.getInitParameter("scope");
		if (scopeParam != null) {
			if ("session".equalsIgnoreCase(scopeParam)) {
				setCacheScope(PageContext.SESSION_SCOPE);
			} else if ("application".equalsIgnoreCase(scopeParam)) {
				setCacheScope(PageContext.APPLICATION_SCOPE);
			} else {
				log.error("OSCache: Wrong value '"
						+ scopeParam
						+ "' for init parameter 'scope', defaulting to 'application'.");
			}

		}

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

		// filter parameter scope
		String disableCacheOnMethodsParam = config
				.getInitParameter("disableCacheOnMethods");
		if (StringUtil.hasLength(disableCacheOnMethodsParam)) {
			disableCacheOnMethods = StringUtil.split(
					disableCacheOnMethodsParam, ',');
			// log.error("OSCache: Wrong value '" + disableCacheOnMethodsParam +
			// "' for init parameter 'disableCacheOnMethods', defaulting to 'null'.");
		}

	}

通过FilterConfig filterConfig来获取参数值,完成初始化。

那Cache是如何对request进行过滤呢?

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

		// avoid reentrance (CACHE-128) and check if request is cacheable
		if (isFilteredBefore(request) || !isCacheableInternal(request)) {
			chain.doFilter(request, response);
			return;
		}
		//标记:已过滤
		request.setAttribute(requestFiltered, 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("OSCache: 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("OSCache: New cache entry, cache stale or cache scope flushed for "
							+ key);
				}

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

				// Only cache if the response is cacheable
				if (isCacheableInternal(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;
					if (log.isInfoEnabled()) {
						log.info("OSCache: New entry added to the cache with key "
								+ key);
					}
				}
			} finally {
				if (!updateSucceeded) {
					cache.cancelUpdate(key);
				}
			}
		}
	}

对request过滤后,添加标志位,避免重复缓存。

时间: 2024-12-06 10:10:34

OSCache源码阅读(一)的相关文章

OSCache源码阅读(二)

前文LRU Cache 暨LinkedHashMap源码阅读提到了如何使用LinkedHashMap来实现一个LRU数据结构,今天在看OSCache代码算法部分的时候,就用到了该知识,what was done contributes what is done now. algorithm包是包含下列缓存过期策略的类: 下面重点介绍LRU和FIFO. LRU private Collection list = new LinkedHashSet(); 使用一个LinkedHashSet对象来实现

CI框架源码阅读笔记3 全局函数Common.php

从本篇开始,将深入CI框架的内部,一步步去探索这个框架的实现.结构和设计. Common.php文件定义了一系列的全局函数(一般来说,全局函数具有最高的加载优先权,因此大多数的框架中BootStrap引导文件都会最先引入全局函数,以便于之后的处理工作). 打开Common.php中,第一行代码就非常诡异: if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 上一篇(CI框架源码阅读笔记2 一切的入口 index

淘宝数据库OceanBase SQL编译器部分 源码阅读--生成逻辑计划

body, td { font-family: tahoma; font-size: 10pt; } 淘宝数据库OceanBase SQL编译器部分 源码阅读--生成逻辑计划 SQL编译解析三部曲分为:构建语法树,生成逻辑计划,指定物理执行计划.第一步骤,在我的上一篇博客淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树里做了介绍,这篇博客主要研究第二步,生成逻辑计划. 一. 什么是逻辑计划?我们已经知道,语法树就是一个树状的结构组织,每个节点代表一种类型的语法含义.如

JDK部分源码阅读与理解

本文为博主原创,允许转载,但请声明原文地址:http://www.coselding.cn/article/2016/05/31/JDK部分源码阅读与理解/ 不喜欢重复造轮子,不喜欢贴各种东西.JDK代码什么的,让整篇文章很乱...JDK源码谁都有,没什么好贴的...如果你没看过JDK源码,建议打开Eclipse边看源码边看这篇文章,看过的可以把这篇文章当成是知识点备忘录... JDK容器类中有大量的空指针.数组越界.状态异常等异常处理,这些不是重点,我们关注的应该是它的一些底层的具体实现,这篇

如何阅读Java源码 阅读java的真实体会

刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧,如果你从来没有学过Java,或是任何一门编程语言如C++,一开始去啃<Core Java>,你是很难从中吸收到营养的,特别是<深入Java虚拟机>这类书,别人觉得好,未必适合现在的你. 虽然Tomcat的源码很漂亮,但我绝不建议你一开始就读它.我文中会专门谈到这个,暂时不展开. 强烈

Memcache-Java-Client-Release源码阅读(之七)

一.主要内容 本章节的主要内容是介绍Memcache Client的Native,Old_Compat,New_Compat三个Hash算法的应用及实现. 二.准备工作 1.服务器启动192.168.0.106:11211,192.168.0.106:11212两个服务端实例. 2.示例代码: String[] servers = { "192.168.0.106:11211", "192.168.0.106:11212" }; SockIOPool pool =

源码阅读笔记 - 1 MSVC2015中的std::sort

大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格式化,去掉或者展开用于条件编译或者debug检查的宏,依重要程度重新排序函数,但是不会改变命名方式(虽然MSVC的STL命名实在是我不能接受的那种),对于代码块的解释会在代码块前(上面)用注释标明. template<class _RanIt, class _Diff, class _Pr> in

JDK 源码 阅读 - 2 - 设计模式 - 创建型模式

A.创建型模式 抽象工厂(Abstract Factory) javax.xml.parsers.DocumentBuilderFactory DocumentBuilderFactory通过FactoryFinder实例化具体的Factory. 使用例子: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilder

CI源码阅读

CodeIgniter源码分析 http://calixwu.com/2014/11/codeigniter-yuanmafenxi.html CI框架源码阅读笔记 http://www.cnblogs.com/ohmygirl/p/4052686.html