2、ehCache介绍
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
下图是 Ehcache 在应用程序中的位置:
主要的特性有:
1. 快速.下面说ehcache的使用
①下载ehcache.jar,自己去google下载地址
②随后,开始配置ehCache的属性,ehCache需要一个xml文件来设置ehCache相关的一些属性,如最大缓存数量、cache刷新的时间等等
ehcache.xml放在你的classpath下.01 |
< ehcache > |
02 |
<!--<diskStore path="c:\\myapp\\cache"/> --> |
03 |
|
04 |
< defaultCache |
05 |
maxElementsInMemory = "1000" |
06 |
eternal = "false" |
07 |
timeToIdleSeconds = "120" |
08 |
timeToLiveSeconds = "120" |
09 |
overflowToDisk = "false" |
10 |
/> |
11 |
< cache name = "DEFAULT_CACHE" |
12 |
maxElementsInMemory = "10000" |
13 |
eternal = "false" |
14 |
timeToIdleSeconds = "300000" |
15 |
timeToLiveSeconds = "600000" |
16 |
overflowToDisk = "false" |
17 |
/> |
18 |
</ ehcache > |
19 |
<!-- |
20 |
1.必须要有的属性: |
21 |
name: cache的名字,用来识别不同的cache,必须惟一。 |
22 |
maxElementsInMemory: 内存管理的缓存元素数量最大限值。 |
23 |
maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。 |
24 |
eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。 |
25 |
overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。 |
26 |
2.下面是一些可选属性: |
27 |
timeToIdleSeconds: 设定元素在过期前空闲状态的时间,只对非持久性缓存对象有效。默认值为0,值为0意味着元素可以闲置至无限长时间。 |
28 |
timeToLiveSeconds: 设定元素从创建到过期的时间。其他与timeToIdleSeconds类似。 |
29 |
diskPersistent: 设定在虚拟机重启时是否进行磁盘存储,默认为false.(我的直觉,对于安全小型应用,宜设为true)。 |
30 |
diskExpiryThreadIntervalSeconds: 访问磁盘线程活动时间。 |
31 |
diskSpoolBufferSizeMB: 存入磁盘时的缓冲区大小,默认30MB,每个缓存都有自己的缓冲区。 |
32 |
memoryStoreEvictionPolicy: 元素逐出缓存规则。共有三种,Recently Used (LRU)最近最少使用,为默认。 First In First Out (FIFO),先进先出。Less Frequently Used(specified as LFU)最少使用 |
33 |
--> |
01 |
package com.woaika.loan.front.common.filter; |
02 |
|
03 |
import javax.annotation.Resource; |
04 |
import javax.servlet.http.HttpServletRequest; |
05 |
import javax.servlet.http.HttpServletResponse; |
06 |
import javax.servlet.http.HttpSession; |
07 |
|
08 |
import net.sf.ehcache.Cache; |
09 |
import net.sf.ehcache.Element; |
10 |
|
11 |
import org.apache.log4j.Logger; |
12 |
import org.springframework.web.servlet.HandlerInterceptor; |
13 |
import org.springframework.web.servlet.ModelAndView; |
14 |
|
15 |
import com.woaika.loan.front.loanuser.vo.LoginOrganVo; |
16 |
import com.woaika.loan.ip.IPUtil; |
17 |
import com.woaika.loan.po.LoanOrgan; |
18 |
|
19 |
public class OrgMgtInterceptor implements HandlerInterceptor { |
20 |
|
21 |
private Logger log = Logger.getLogger(OrgMgtInterceptor. class ); |
22 |
|
23 |
private Cache ehCache; |
24 |
|
25 |
@Resource (name= "ehCache" ) |
26 |
public void setEhCache(Cache ehCache) { |
27 |
this .ehCache = ehCache; |
28 |
} |
29 |
|
30 |
public void afterCompletion(HttpServletRequest arg0, |
31 |
HttpServletResponse arg1, Object arg2, Exception arg3) |
32 |
throws Exception { |
33 |
//log.info("==============执行顺序: 3、afterCompletion================"); |
34 |
|
35 |
} |
36 |
|
37 |
|
38 |
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, |
39 |
Object arg2, ModelAndView arg3) throws Exception { |
40 |
//log.info("==============执行顺序: 2、postHandle================"); |
41 |
|
42 |
} |
43 |
|
44 |
|
45 |
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, |
46 |
Object handler) throws Exception { |
47 |
// log.info("==============执行顺序: 1、preHandle================"); |
48 |
|
49 |
String ip = IPUtil.getRemortIP(request); |
50 |
Element elementIp = ehCache.get(ip); |
51 |
if ( null ==elementIp){ |
52 |
response.sendRedirect(com.woaika.loan.commons.constants.SiteConstant.JIGOU_URL+ "/tologin" ); |
53 |
return false ; |
54 |
} else { |
55 |
LoginOrganVo vo =(LoginOrganVo)elementIp.getObjectValue(); |
56 |
if (vo!= null ){ |
57 |
Element elementId = ehCache.get(vo.getId().toString()); |
58 |
if (elementId!= null ){ |
59 |
String tempIp = (String)elementId.getObjectValue(); |
60 |
if (tempIp!= null && ! "" .equals(tempIp) && ip.equals(tempIp)){ |
61 |
request.setAttribute( "currentOrgan" , vo); |
62 |
return true ; |
63 |
} |
64 |
|
65 |
} else { |
66 |
response.sendRedirect(com.woaika.loan.commons.constants.SiteConstant.JIGOU_URL+ "/tologin" ); |
67 |
return false ; |
68 |
|
69 |
} |
70 |
} |
71 |
} |
72 |
return true ; |
73 |
|
74 |
/* String url=request.getRequestURL().toString(); |
75 |
// if(url.matches(mappingURL)){ |
76 |
HttpSession session = request.getSession(); |
77 |
LoanOrgan org = (LoanOrgan)session.getAttribute("currentOrgan"); |
78 |
if(org!=null){ |
79 |
return true; |
80 |
}else{ |
81 |
response.sendRedirect(com.woaika.loan.commons.constants.SiteConstant.JIGOU_URL+"/tologin"); |
82 |
} |
83 |
return false; |
84 |
// } |
85 |
// return true; |
86 |
*/ |
87 |
|
88 |
} |
89 |
|
90 |
} |
01 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
02 |
xmlns:context = "http://www.springframework.org/schema/context" |
03 |
xmlns:p = "http://www.springframework.org/schema/p" |
04 |
xmlns:mvc = "http://www.springframework.org/schema/mvc" |
05 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
06 |
xmlns:aop = "http://www.springframework.org/schema/aop" |
07 |
xmlns:tx = "http://www.springframework.org/schema/tx" |
08 |
xsi:schemaLocation="http://www.springframework.org/schema/beans |
09 |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
10 |
http://www.springframework.org/schema/context |
11 |
http://www.springframework.org/schema/context/spring-context.xsd |
12 |
http://www.springframework.org/schema/tx |
13 |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd |
14 |
http://www.springframework.org/schema/aop |
15 |
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd |
16 |
http://www.springframework.org/schema/mvc |
17 |
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> |
18 |
<!-- 引用ehCache的配置 --> |
19 |
< bean id = "defaultCacheManager" class = "org.springframework.cache.ehcache.EhCacheManagerFactoryBean" > |
20 |
< property name = "configLocation" > |
21 |
< value >classpath:ehcache.xml</ value > |
22 |
</ property > |
23 |
</ bean > |
24 |
|
25 |
<!-- 定义ehCache的工厂,并设置所使用的Cache name --> |
26 |
< bean id = "ehCache" class = "org.springframework.cache.ehcache.EhCacheFactoryBean" > |
27 |
< property name = "cacheManager" > |
28 |
< ref local = "defaultCacheManager" /> |
29 |
</ property > |
30 |
< property name = "cacheName" > |
31 |
< value >DEFAULT_CACHE</ value > |
32 |
</ property > |
33 |
</ bean > |
34 |
|
35 |
</ beans > |
01 |
@RequestMapping (value= "/login" ) |
02 |
public String login(String organname, String pwd, HttpServletRequest request, HttpServletResponse response){ |
03 |
judgmentCity.getCityInfo(request); |
04 |
LoanOrgan organ = loanOrganService.login(organname, pwd); |
05 |
if (organ== null ){ |
06 |
request.setAttribute( "msg" , "用户名或密码错误" ); |
07 |
return "forward:/jigou/tologin" ; |
08 |
} else { |
09 |
//将机构信息存在cache中,来进行跨域 |
10 |
String ip = IPUtil.getRemortIP(request); |
11 |
LoginOrganVo vo = new LoginOrganVo(); |
12 |
vo.setId(organ.getId()); |
13 |
vo.setOrganname(organ.getOrganname()); |
14 |
Element elementip = new Element(ip, (Serializable) vo); |
15 |
Element elementid = new Element(organ.getId().toString(), ip); |
16 |
//Element elementvo = new Element(organ.getId().toString(), (Serializable) vo); |
17 |
ehCache.put(elementip); //添加到缓存 |
18 |
ehCache.put(elementid); |
19 |
// ehCache.put(elementvo); |
20 |
//request.getSession().setAttribute("currentOrgan", organ); |
21 |
return "redirect:/organmgt/index" ; |
22 |
} |
23 |
} |
01 |
//注销登录 |
02 |
@RequestMapping (value= "/logout" ) |
03 |
public String logout(HttpServletRequest request){ |
04 |
String ip = IPUtil.getRemortIP(request); |
05 |
Element elementIp = ehCache.get(ip); |
06 |
if (elementIp!= null ){ |
07 |
LoginOrganVo vo =(LoginOrganVo)elementIp.getObjectValue(); |
08 |
if (vo!= null ){ |
09 |
ehCache.remove(vo.getId()); |
10 |
} |
11 |
} |
12 |
ehCache.remove(ip); |
13 |
//request.getSession().removeAttribute("currentOrgan"); |
14 |
return "redirect:/jigou/tologin" ; |
15 |
} |
1 |
< mvc:interceptors > |
2 |
< mvc:interceptor > |
3 |
< mvc:mapping path = "/organmgt/**" /> <!-- 如果不配置或/*,将拦截所有的Controller --> |
4 |
< bean class = "com.woaika.loan.front.common.filter.OrgMgtInterceptor" > |
5 |
</ bean > |
6 |
</ mvc:interceptor > |
7 |
</ mvc:interceptors > |
事实上到了这里,一个简单的Spring + ehCache Framework基本完成了。