2008年12月26日星期五

EHCACHE配置详解(转)

摘自:http://hi.baidu.com/vsign/blog/item/7863c011ac6b6413b8127bc2.html


Ehcache有两种使用方式,一种通过在代码中直接调用Ehcahe提供的接口进行缓存,另一种就是通过与Spring结合,通过对方法的拦截技术来缓存方法调用返回的结果.

方式一:手动配置你的EHCACHE

ehcache.xml中加入如下节点,并按需设置参数.

 

<cache name="your cache name"
             maxElementsInMemory="300"
             eternal="false"
             timeToIdleSeconds="500"
             timeToLiveSeconds="500"
             overflowToDisk="true"
       />

    

参数的含义:

maxInMemory               - 设定内存中创建对象的最大值。
             eternal                             -
设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超
                                              
时限制且元素永不消亡。
             timeToIdleSeconds       -
设置某个元素消亡前的停顿时间。
                                              
也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。
                                              
这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则
                                              
设置该属性也无用)。
                                              
如果该值是 0 就意味着元素可以停顿无穷长的时间。
             timeToLiveSeconds -
为元素设置消亡前的生存时间。
                                               
也就是一个元素从构建到消亡的最大时间间隔值。
                                               
这只能在元素不是永久驻留时有效。
             overflowToDisk             -
设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘
                                               
上。

通过写代码调用Ehcache的接口。

优点:

1.         可以在任何java代码中使用来缓存需要的结果

2.         可以缓存非系列化的java对象

缺点:

1.       缓存代码直接嵌入应用代码中,无法分离

综上所述,当需要在一个方法内部使用缓存或缓存非系列化对象时,选用此缓存方式。

示例代码:

 

Vector vobject= new Vector();
     
Cache cache = CacheManager.getInstance().getCache("your cache name");     
String key = "someKeyOfTheCacheElement";
Element element = cache.get(key);     
if (element == null) {      
vobject= loadObjDAO(arg1, arg2);
cache.put(new Element(key, vobject));
} else {
vobject= (Vector) element.getValue();
}


    

方式二:通过与spring结合,可以对bean中的任意一个方法返回的结果进行缓存。

优点:

1.       不需要写任何缓存代码,缓存代码与应用代码分离,通过配置方式对方法返回结果进行缓存

缺点:

1.       只能以方法为单位,对方法返回结果缓存

2.       暂时不能缓存非序列化对象

如果想对某个bean的所有方法进行缓存,可以在你的spring配置文件中加入如下代码:

 

<bean id="cacheBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target">
         <bean class="yourcompany.youBean"/>
        </property>
        <property name="interceptorNames">
          <list>
            <value>methodCacheInterceptor</value>
          </list>
        </property>
</bean>

    

注:methodCacheInterceptor拦截器实现了org.aopalliance.intercept.MethodInterceptor接口。一旦运行起来,它首先检查被拦截方法是否被配置为可缓存的。这将可选择性的配置想要缓存的 bean 方法,只要调用的方法配置为可缓存,拦截器将为该方法生成 cache key 并检查该方法返回的结果是否已缓存。如果已缓存,就返回缓存的结果,否则再次调用被拦截方法,并缓存结果供下次调用。

如果想对bean中的一部分方法进行缓存,如对TestCacheBean所有get方法进行缓存,需要在你的spring配置文件中加入如下代码:

 

<bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

      <property name="advice">

      <ref bean="methodCacheInterceptor"/>

      </property>

      <property name="patterns">

      <list>

      <value>>*.get*</value>

      </list>

      </property>

      </bean>


<bean id="cacheBean" class="org.springframework.aop.framework.ProxyFactoryBean">

      <property name="target">

      <bean class="com.yourcompany.TestCacheBean"/>

      </property>

      <property name="interceptorNames">

      <list>

      <value>methodCacheInterceptor</value>

      </list>

      </property>

      </bean>



安装与资料:

如果想单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。如果你的项目已安装了Hibernate2.1 ,则不需要做什么,直接可以使用Ehcache

1. Ehcache官方网站:http://ehcache.sourceforge.net/samples.html

2.http://opensource.atlassian.com/confluence/spring/display/DISC/Caching+the+result+of+methods+using+Spring+and+EHCache

没有评论: