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过滤后,添加标志位,避免重复缓存。





OSCache是什么? OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存的功能。OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存解决方案。 2、OSCache的特点 (1) 缓存任何对象:你可以不受限制的缓存部分jsp页面或HTTP请求,任何java对象都可以缓存。 (2) 拥有全面的API:OSCache API允许你通过编程的方式来控制所有的OSCache特性。 (3) 永久缓存缓存能被配置写入硬盘,因此允许在应用服务器的多次生命周期间缓存创建开销昂贵的数据。 (4) 支持集群:集群缓存数据能被单个的进行参数配置,不需要修改代码。 (5) 缓存过期:你可以有最大限度的控制缓存对象的过期,包括可插入式的刷新策略(如果默认性能不能满足需要时)。 3、OSCache的安装与配置 网上已经有一个不错的使用教程:http://blog.csdn.net/ezerg/archive/2004/10/14/135769.aspx 4、有关“用OSCache进行缓存对象”的研究 这个是我今天要说的东西。网上对于OSCache缓存Web页面很多说明和例子,但对于缓存对象方面说得不多,我就把自已写得一些东西放出来,让大家看一看是怎样缓存对象的! 我基于GeneralCacheAdministrator类来写的BaseCache类 view plaincopy to clipboardprint?package com.klstudio.cache; import java.util.Date; import com.opensymphony.oscache.base.NeedsRefreshException; import com.opensymphony.oscache.general.GeneralCacheAdministrator; public class BaseCache extends GeneralCacheAdministrator { //过期时间(单位为秒); private int refreshPeriod; //关键字前缀字符; private String keyPrefix; private static final long serialVersionUID = -4397192926052141162L; public BaseCache(String keyPrefix,int refreshPeriod){ super(); this.keyPrefix = keyPrefix; this.refreshPeriod = refreshPeriod; } //添加被缓存的对象; public void put(String key,Object value){ this.putInCache(this.keyPrefix+"_"+key,value); } //删除被缓存的对象; public void remove(String key){ this.flushEntry(this.keyPrefix+"_"+key); } //删除所有被缓存的对象; public void removeAll(Date date){ this.flushAll(date); } public void removeAll(){ this.flushAll(); } //获取被缓存的对象; public Object get(String key) throws Exception{ try{ return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod); } catch (NeedsRefreshException e) { this.cancelUpdate(this.keyPrefix+"_"+key); throw e; } } } package com.klstudio.cache;<br/><br/>import java.util.Date;<br/><br/>import com.opensymphony.oscache.base.NeedsRefreshException;<br/>import com.opensymphony.oscache.general.GeneralCacheAdministrator;<br/><br/>public class BaseCache extends GeneralCacheAdministrator {<br/> //过期时间(单位为秒);<br/> private int refreshPeriod;<br/> //关键字前缀字符;<br/> private String keyPrefix;<br/> <br/> private static final long serialVersionUID = -4397192926052141162L;<br/> <br/> public BaseCache(String keyPrefix,int refreshPeriod){<br/> super();<br/> this.keyPrefix = keyPrefix;<br/> this.refreshPeriod = refreshPeriod;<br/> }<br/> //添加被缓存的对象;<br/> public void put(String key,Object value){<br/> this.putInCache(this.keyPrefix+"_"+key,value);<br/> }<br/> //删除被缓存的对象;<br/> public void remove(String key){<br/> this.flushEntry(this.keyPrefix+"_"+key);<br/> }<br/> //删除所有被缓存的对象;<br/> public void removeAll(Date date){<br/> this.flushAll(date);<br/> }<br/> <br/> public void removeAll(){<br/> this.flushAll();<br/> }<br/> //获取被缓存的对象;<br/> public Object get(String key) throws Exception{<br/> try{<br/> return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);<br/> } catch (NeedsRefreshException e) {<br/> this.cancelUpdate(this.keyPrefix+"_"+key);<br/> throw e;<br/> }<br/><br/> }<br/> <br/>}<br/><br/><br/> 通过CacheManager类来看怎样缓存对象的,这个类中所用的News只是具体功能的类,我就不贴出来了,你可以自己写一个! view plaincopy to clipboardprint?package com.klstudio; import com.klstudio.News; import com.klstudio.cache.BaseCache; public class CacheManager { private BaseCache newsCache; private static CacheManager instance; private static Object lock = new Object(); public CacheManager() { //这个根据配置文件来,初始BaseCache而已; newsCache = new BaseCache("news",1800); } public static CacheManager getInstance(){ if (instance == null){ synchronized( lock ){ if (instance == null){ instance = new CacheManager(); } } } return instance; } public void putNews(News news) { // TODO 自动生成方法存根 newsCache.put(news.getID(),news); } public void removeNews(String newsID) { // TODO 自动生成方法存根 newsCache.remove(newsID); } public News getNews(String newsID) { // TODO 自动生成方法存根 try { return (News) newsCache.get(newsID); } catch (Exception e) { // TODO 自动生成 catch 块 System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage()); News news = new News(newsID); this.putNews(news); return news; } } public void removeAllNews() { // TODO 自动生成方法存根 newsCache.removeAll(); } } package com.klstudio;<br/><br/>import com.klstudio.News;<br/>import com.klstudio.cache.BaseCache;<br/><br/>public class CacheManager {<br/> <br/> private BaseCache newsCache;<br/><br/> <br/> private static CacheManager instance;<br/> private static Object lock = new Object();<br/> <br/> public CacheManager() {<br/> //这个根据配置文件来,初始BaseCache而已;<br/> newsCache = new BaseCache("news",1800); <br/> }<br/> <br/> public static CacheManager getInstance(){<br/> if (instance == null){<br/> synchronized( lock ){<br/> if (instance == null){<br/> instance = new CacheManager();<br/> }<br/> }<br/> }<br/> return instance;<br/> }<br/><br/> public void putNews(News news) {<br/> // TODO 自动生成方法存根<br/> newsCache.put(news.getID(),news);<br/> }<br/><br/> public void removeNews(String newsID) {<br/> // TODO 自动生成方法存根<br/> newsCache.remove(newsID);<br/> }<br/><br/> public News getNews(String newsID) {<br/> // TODO 自动生成方法存根<br/> try {<br/> return (News) newsCache.get(newsID);<br/> } catch (Exception e) {<br/> // TODO 自动生成 catch 块<br/> System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());<br/> News news = new News(newsID);<br/> this.putNews(news);<br/> return news;<br/> }<br/> }<br/><br/> public void removeAllNews() {<br/> // TODO 自动生成方法存根<br/> newsCache.removeAll();<br/> }<br/><br/>}<br/><br/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值