ehcache在struts2中的使用
?放假了,首先祝大家端午节快乐,今天下雨,没事干研究了下ehcache,从ehcache官网查看了一番后,看了看网上别人的blog,没有ehcache的详细用法,所以自己小试了一下,第一次使用ehcache可能有些地方不正确,希望大家帮我纠正,共同进步。
我采用struts2+ehcache。代码如下:
1、index.jsp
view plaincopy to clipboardprint?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>??
<%??
String path = request.getContextPath();??
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";??
%>??
?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">??
<html>??
? <head>??
??? <base href="<%=basePath%>">??
??????
??? <title>My JSP 'index.jsp' starting page</title>??
??? <meta http-equiv="pragma" content="no-cache">??
??? <meta http-equiv="cache-control" content="no-cache">??
??? <meta http-equiv="expires" content="0">??????
??? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">??
??? <meta http-equiv="description" content="This is my page">??
??? <!--??
??? <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">??
??? -->??
? </head>??
????
? <body>??
??? <a href="colorAction.action" mce_href="colorAction.action">查看所有颜色</a>??
? </body>??
</html>?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <base href="<%=basePath%>">
???
??? <title>My JSP 'index.jsp' starting page</title>
?<meta http-equiv="pragma" content="no-cache">
?<meta http-equiv="cache-control" content="no-cache">
?<meta http-equiv="expires" content="0">???
?<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
?<meta http-equiv="description" content="This is my page">
?<!--
?<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
?-->
? </head>
?
? <body>
??? <a href="colorAction.action" mce_href="colorAction.action">查看所有颜色</a>
? </body>
</html>
?
2、ColorAction.java
view plaincopy to clipboardprint?
package com.ehcache.servlet;??
?
import java.awt.Color;??
import java.util.ArrayList;??
import java.util.List;??
import java.util.Map;??
?
import net.sf.ehcache.Cache;??
import net.sf.ehcache.CacheManager;??
import net.sf.ehcache.Element;??
import com.ehcache.database.ColorDatabase;??
import com.ehcache.util.EhcacheUtil;??
import com.opensymphony.xwork2.ActionSupport;??
?
@SuppressWarnings("serial")??
public class ColorAction extends ActionSupport {??
?
??? private List<String> list = null;??
?
??? @SuppressWarnings("unchecked")??
??? public String allColor() throws Exception {??
??????? //获取CacheManager??
??????? CacheManager cacheManager = EhcacheUtil.getCacheManager();??
??????????
??????? list = new ArrayList<String>();??
??????????
??????? //用配置文件中配置的colorcache创建cache缓存??
??????? Cache cache = cacheManager.getCache("colorcache");??
??????????
??????????
??????? //查看cache中是否存在allColor的缓存??
??????? Element element = cache.get("allColor");??
??????????
??????? //如果不存在,从数据库中查询??
??????? if (element == null) {??
??????????? Map<String, Color> maps = ColorDatabase.getAllColor();??
??????????? java.util.Iterator<String> iter = maps.keySet().iterator();??
??????????? while (iter.hasNext()) {??
??????????????? Color color = maps.get(iter.next());??
??????????????? list.add(color.toString());??
??????????? }??
??????????? cache.put(new Element("allColor", list));??
??????? } else {//如果存在,从缓存中加载??
??????????? list = (List<String>) element.getValue();??
??????? }??
??????? return "allColor";??
??? }??
??????
?
??? public List<String> getList() {??
??????? return list;??
??? }??
?
??? public void setList(List<String> list) {??
??????? this.list = list;??
??? }??
?
}?
package com.ehcache.servlet;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import com.ehcache.database.ColorDatabase;
import com.ehcache.util.EhcacheUtil;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ColorAction extends ActionSupport {
?private List<String> list = null;
?@SuppressWarnings("unchecked")
?public String allColor() throws Exception {
??//获取CacheManager
??CacheManager cacheManager = EhcacheUtil.getCacheManager();
??
??list = new ArrayList<String>();
??
??//用配置文件中配置的colorcache创建cache缓存
??Cache cache = cacheManager.getCache("colorcache");
??
??
??//查看cache中是否存在allColor的缓存
??Element element = cache.get("allColor");
??
??//如果不存在,从数据库中查询
??if (element == null) {
???Map<String, Color> maps = ColorDatabase.getAllColor();
???java.util.Iterator<String> iter = maps.keySet().iterator();
???while (iter.hasNext()) {
????Color color = maps.get(iter.next());
????list.add(color.toString());
???}
???cache.put(new Element("allColor", list));
??} else {//如果存在,从缓存中加载
???list = (List<String>) element.getValue();
??}
??return "allColor";
?}
?
?public List<String> getList() {
??return list;
?}
?public void setList(List<String> list) {
??this.list = list;
?}
}
?
3、ColorDatabase.java 代替数据库。
view plaincopy to clipboardprint?
package com.ehcache.database;??
?
import java.awt.Color;??
import java.util.HashMap;??
import java.util.Map;??
?
/**?
?* 在ehcache-2.1.0中的samples\colorcache 的例子中修改了一下,这个代替了数据库。?
?*/?
public class ColorDatabase {??
?
??? private static Map<String,Color> colorMap = new HashMap<String,Color>();??
??? static{??
???????? colorMap.put("red", Color.red);??
??????????? colorMap.put("blue", Color.blue);??
??????????? colorMap.put("green", Color.green);??
??????????? colorMap.put("white", Color.white);??
??????????? colorMap.put("black", Color.black);??
??????????? colorMap.put("lightGray", Color.lightGray);??
??????????? colorMap.put("gray", Color.gray);??
??????????? colorMap.put("darkGray", Color.darkGray);??
??????????? colorMap.put("pink", Color.pink);??
??????????? colorMap.put("orange", Color.orange);??
??????????? colorMap.put("yellow", Color.yellow);??
??????????? colorMap.put("magenta", Color.magenta);??
??????????? colorMap.put("cyan", Color.cyan);??
??????????????
??? }??
??????
??? public static Map<String,Color> getAllColor(){??
??????? return colorMap;??
??? }??
}?
package com.ehcache.database;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
/**
?* 在ehcache-2.1.0中的samples\colorcache 的例子中修改了一下,这个代替了数据库。
?*/
public class ColorDatabase {
?private static Map<String,Color> colorMap = new HashMap<String,Color>();
?static{
?? colorMap.put("red", Color.red);
????? colorMap.put("blue", Color.blue);
????? colorMap.put("green", Color.green);
????? colorMap.put("white", Color.white);
????? colorMap.put("black", Color.black);
????? colorMap.put("lightGray", Color.lightGray);
????? colorMap.put("gray", Color.gray);
????? colorMap.put("darkGray", Color.darkGray);
????? colorMap.put("pink", Color.pink);
????? colorMap.put("orange", Color.orange);
????? colorMap.put("yellow", Color.yellow);
????? colorMap.put("magenta", Color.magenta);
????? colorMap.put("cyan", Color.cyan);
?????
?}
?
?public static Map<String,Color> getAllColor(){
??return colorMap;
?}
}
?
4、allColor.jsp
view plaincopy to clipboardprint?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>??
<%@taglib prefix="s" uri="/struts-tags"%>??
<%??
String path = request.getContextPath();??
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";??
%>??
?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">??
<html>??
? <head>??
??? <base href="<%=basePath%>">??
??????
??? <title>My JSP 'allColor.jsp' starting page</title>??
??????
??? <meta http-equiv="pragma" content="no-cache">??
??? <meta http-equiv="cache-control" content="no-cache">??
??? <meta http-equiv="expires" content="0">??????
??? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">??
??? <meta http-equiv="description" content="This is my page">??
??? <!--??
??? <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">??
??? -->??
?
? </head>??
????
? <body>??
? <s:iterator value="list">??
??? <s:property/><br>??
? </s:iterator>??
? </body>??
</html>?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <base href="<%=basePath%>">
???
??? <title>My JSP 'allColor.jsp' starting page</title>
???
?<meta http-equiv="pragma" content="no-cache">
?<meta http-equiv="cache-control" content="no-cache">
?<meta http-equiv="expires" content="0">???
?<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
?<meta http-equiv="description" content="This is my page">
?<!--
?<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
?-->
? </head>
?
? <body>
? <s:iterator value="list">
??? <s:property/><br>
? </s:iterator>
? </body>
</html>
?
5、ehcache.xml
view plaincopy to clipboardprint?
<ehcache>?
??? <diskStore path="java.io.tmpdir" />?
??? <defaultCache maxElementsInMemory="10000" eternal="false"?
??????? timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />?
?
??? <cache name="colorcache" maxElementsInMemory="10000" eternal="false"?
??????? timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />?
</ehcache>?
<ehcache>
?<diskStore path="java.io.tmpdir" />
?<defaultCache maxElementsInMemory="10000" eternal="false"
??timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
?<cache name="colorcache" maxElementsInMemory="10000" eternal="false"
??timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
</ehcache>
?
6、struts.xml
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8" ?>?
<!DOCTYPE struts PUBLIC??
??? "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"??
??? "http://struts.apache.org/dtds/struts-2.0.dtd">?
?
<struts>?
?
? <package name="struts" extends="struts-default">?
????
??? <action name="colorAction" method="allColor">?
?????? <result name="allColor">/allColor.jsp</result>?
??? </action>?
? </package>?
</struts>?
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
??? "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
??? "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
? <package name="struts" extends="struts-default">
?
??? <action name="colorAction" method="allColor">
?????? <result name="allColor">/allColor.jsp</result>
??? </action>
? </package>
</struts>
?
7、EhcacheUtil.java
view plaincopy to clipboardprint?
package com.ehcache.util;??
?
import net.sf.ehcache.CacheManager;??
?
public class EhcacheUtil {??
?
??? private EhcacheUtil(){}??
??????
??? private static CacheManager? cacheManager = null;??
??????
??? static{??
??????????
??????? cacheManager = CacheManager.create();??
??? }??
??????
??? public static CacheManager getCacheManager(){??
??????? return cacheManager;??
??? }??
??????
}?
?