zoukankan      html  css  js  c++  java
  • Java缓存框架使用EhCache结合Spring AOP

    一.Ehcache简介
        EhCache是一个纯Java的进程内缓存框架,具有如下特点:
        1. 快速简单,非常容易和应用集成。
        2.支持多种缓存策略 。
        3. 缓存数据有两级:内存和磁盘,因此无需担心容量问题 。
        4. 缓存数据会在虚拟机重启的过程中写入磁盘 。
        5. 可以通过RMI、可插入API等方式进行分布式缓存。
        6. 具有缓存和缓存管理器的侦听接口 。
        7. 支持多缓存管理器实例,以及一个实例的多个缓存区域 等特点。

    二.Ehcache配置的相关参数
        Ehcache 的配置很灵活,官方提供的配置方式有好几种,你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。下面以最常用的XML 配置为例说下配置的相关参数的意义,ehcache.xml是最常见的一个文件,ehcache一般会通过CacheManager从classpath 加载该文件完成Cache的实例化。
       
        1.ehcache.xml中的配置信息
            ehcache.xml片段:

    Java代码
     
          <ehcache>
                <diskStore path="java.io.tmpdir"/>
                <defaultCache
                    name="name"
                        maxElementsInMemory="10000"
                        eternal="false"
                        timeToIdleSeconds="120"
                        timeToLiveSeconds="120"
                        overflowToDisk="true"
                        maxElementsOnDisk="10000000"
                        diskPersistent="false"
                        diskExpiryThreadIntervalSeconds="120"
                        memoryStoreEvictionPolicy="LRU"
                        />
            </ehcache>

         2.Cache中常用参数的具体意义
            (1)name:Cache的唯一标识。
            (2)maxElementsInMemory:内存中最大缓存对象数。
            (3)eternal:Element是否永久有效,一旦设置true,timeout将不起作用。
            (4)timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
            (5)timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。
            (6)overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。
            (7)maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。
            (8) memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略 去清理缓存中的内容。默认策略是LRU(最近最少使用),你也可以设置为FIFO(先进先出)或是LFU(较少使用)
       
    三.Spring和Ehcache的集成
        1.ehcache.xml

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:noNamespaceSchemaLocation="ehcache.xsd">
           
                <diskStore path="java.io.tmpdir" />
           
                <defaultCache maxElementsInMemory="10000" eternal="false"
                    timeToIdleSeconds="600" overflowToDisk="false">
                </defaultCache>
           
                <cache name="levelOneCache" maxElementsInMemory="1000" eternal="false"
                    timeToIdleSeconds="300" timeToLiveSeconds="1000" overflowToDisk="false" />
            </ehcache>

        2.beans.xml的配置

     <bean id="cacheManager"
                class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
                <property name="configLocation">
                    <value>classpath:ehcache.xml</value>
                </property>
            </bean>
    
            <bean id="levelOneCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
                <property name="cacheManager">
                    <ref local="cacheManager" />
                </property>
                <property name="cacheName">
                    <value>configCache</value>
                </property>
            </bean>

        3.测试类

    Java
            package org.mango.cache.ehcache;
           
            import net.sf.ehcache.Cache;
            import net.sf.ehcache.CacheManager;
            import net.sf.ehcache.Element;
           
            import org.springframework.beans.factory.BeanFactory;
            import org.springframework.beans.factory.xml.XmlBeanFactory;
            import org.springframework.core.io.ClassPathResource;
            import org.springframework.core.io.Resource;
           
            public class EhcacheTest {
           
                public static void main(String[] args) {
                    Resource res = new ClassPathResource("beans.xml");
                    BeanFactory factory = new XmlBeanFactory(res);
           
                    CacheManager cacheManager = (CacheManager) factory.getBean("cacheManager");
                    Cache levelOneCache = cacheManager.getCache("levelOneCache");
                    CacheObject cacheObject = null;
                    for (int i = 0; i < 10; i++) {
                        Element element = levelOneCache.get("key");
           
                        if (element == null) {
                            cacheObject = new CacheObject("test");
                            element = new Element("key", cacheObject);
                            levelOneCache.put(element);
                            System.out.println("cacheObject[" + cacheObject + "]" + ",无法从缓存中取到");
                        } else {
                            cacheObject = (CacheObject) element.getValue();
                            System.out.println("cacheObject[" + cacheObject + "]" + ",从缓存中取到");
                        }
                    }
                }
            }
     

            输出如下:

     
            cacheObject[name:test],无法从缓存中取到
            cacheObject[name:test],从缓存中取到
            cacheObject[name:test],从缓存中取到
            cacheObject[name:test],从缓存中取到
            cacheObject[name:test],从缓存中取到
       

           
    四.利用Spring AOP和Ehcache实现线程级方法缓存
        在复杂的业务逻辑或在一次计算中需多次调用同一个DAO或远程服务,在这种情况下,均可对计算结果缓存起来,不但可以减少了不必要的调用次数,还同时可以提高系统运算性能。下面以缓存一个service为例说明一下其用法。
       
        1.TestService接口

     public interface TestService {
           
                /**
                 * 根据userId取得用户名。
                 *
                 * @param userId
                 * @return 
                 */
                public String getUserName(String userId);
            }
     

       
        2.TestServiceImpl实现类

     public class TestServiceImpl implements TestService {
                /*
                 * @see  org.mango.cache.ehcache.TestService#getUserName(java.lang.String)
                 */
                public String getUserName(String userId) {
                    return userId;
                }
            }

        3.拦截器的实现

     public class CacheInterceptor implements MethodInterceptor {
           
                private Cache cache;
           
                /**
                 * @see  org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
                 */
                public Object invoke(MethodInvocation invocation) throws Throwable {
                    Method method = invocation.getMethod();
                    String methodName = method.getName();
                    Object[] arguments = invocation.getArguments();
                    Object result = invocation.proceed();
           
                    String targetName = method.getDeclaringClass().getName();
                    String key = getCacheKey(targetName, methodName, arguments);
           
                    Element element = cache.get(key);
           
                    if (element == null) {
           
                        result = invocation.proceed();
                        System.out.println("第一次调用方法并缓存其值:" + result);
                        cache.put(new Element(key, result));
                    } else {
                        result = element.getValue();
                        System.out.println("从缓存中取得的值为:" + result);
                    }
                    return result;
           
                }
           
                /**
                 * 生成缓存中的KEY值。
                 */
                protected String getCacheKey(String targetName, String methodName, Object[] arguments) {
                    StringBuffer sb = new StringBuffer();
                    sb.append(targetName).append(".").append(methodName);
                    if ((arguments != null) && (arguments.length != 0)) {
                        for (int i = 0; i < arguments.length; i++) {
                            sb.append(".").append(arguments[i]);
                        }
                    }
                    return sb.toString();
                }
           
                public void setCache(Cache cache) {
                    this.cache = cache;
                }
           
            }
     

        4.Bean的配置

     <bean id="testService" class="org.mango.cache.ehcache.TestServiceImpl" />
       
            <bean id="serviceMethodInterceptor" class="org.mango.cache.ehcache.CacheInterceptor">
                <property name="cache">
                    <ref local="levelOneCache" />
                </property>
            </bean>
       
            <bean id="serviceAutoProxyCreator"
                class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
                <property name="interceptorNames">
                    <list>
                        <value>serviceMethodInterceptor</value>
                    </list>
                </property>
                <property name="beanNames">
                    <value>*Service</value>
                </property>
            </bean>

        5.测试方法

     public class ServiceTest {
               public static void main(String[] args) {
                    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
                    TestService testService = (TestService) context.getBean("testService");
                    for (int i = 0; i < 5; i++) {
                        testService.getUserName("mango");
                    }
                }
            }

            其输出结果如下:

     第一次调用方法并缓存其值:mango
    从缓存中取得的值为:mango
    从缓存中取得的值为:mango
    从缓存中取得的值为:mango
    从缓存中取得的值为:mango


    http://my.oschina.net/iblike/blog/71260
  • 相关阅读:
    《JAVA高并发编程详解》-Thread start方法的源码
    《JAVA高并发编程详解》-Thread对象的启动
    作为程序员,我建议你学会写作
    【灵异短篇】这个夜晚有点凉
    JAVA中for与while关于内存的细节问题
    通过本质看现象:关于Integer受内部初始化赋值范围限制而出现的有趣现象
    【设计模式】抽象工厂模式
    【设计模式】工厂模式
    【设计模式】单例模式
    【设计模式】基本介绍
  • 原文地址:https://www.cnblogs.com/sunxucool/p/3161319.html
Copyright © 2011-2022 走看看