<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-153  評論-235  文章-19  trackbacks-0
          先描述下我的環(huán)境eclipse 3.2.2+myeclilpse 5.5.1
    Spring 2.0.6,Ehcache用spring2.0.6帶的1.2.4
    加入的jar
    Spring.jar
    commons-logging.jar
    ehcache-1.2.4.jar
    log4j-1.2.14.jar
    junit3.8.1.jar

          示例描述:通過省ID找它下面的市,緩存市.

    1.我用IdAndName類封裝市,可以替換成String.
    package com.chenlb.study.ehcache;

    /**
     * 2007-8-2
     * 
    @author chenlb
     * 
     
    */
    public class IdAndName {

        
    private Integer id;
        
    private String name;
        
        
    public IdAndName() {}
        
        
        
    /**
         * 
    @param id
         * 
    @param name
         
    */
        
    public IdAndName(Integer id, String name) {
            
    super();
            
    this.id = id;
            
    this.name = name;
        }
        
        
    public Integer getId() {
            
    return id;
        }
        
    public void setId(Integer id) {
            
    this.id = id;
        }
        
    public String getName() {
            
    return name;
        }
        
    public void setName(String name) {
            
    this.name = name;
        }    
    }

    2.定義一個接口CityCache
    package com.chenlb.study.ehcache;

    import java.util.List;

    /**
     * 2007-8-2
     * 
    @author chenlb
     * 
     
    */
    public interface CityCache {
        
    public List<IdAndName> getCitys(Integer provinceId);
    }

    3.定義數(shù)據(jù)類CityData,
    /*
     * 
     
    */
    package com.chenlb.study.ehcache;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    /**
     * 2007-8-2
     * 
    @author chenlb
     * 
     
    */
    public class CityData {

        
    static Map<Integer, List<IdAndName>> provinces = new HashMap<Integer, List<IdAndName>>();
        
        
    static {
            ArrayList
    <IdAndName> al = new ArrayList<IdAndName>();    //廣東
            al.add(new IdAndName(1,"廣州市"));    al.add(new IdAndName(2,"梅州市"));
            provinces.put(
    1, al);
            
            al 
    = new ArrayList<IdAndName>();    //上海
            al.add(new IdAndName(1,"黃浦"));    al.add(new IdAndName(2,"盧灣"));
            provinces.put(
    2, al);
        }
        
        
    /**
         * 此方法可以換成從數(shù)據(jù)庫得到數(shù)據(jù)
         * 
    @param provinceId
         * 
    @return
         
    */
        
    public static List<IdAndName> getCityByProvince(Integer provinceId) {
            
    return provinces.get(provinceId);
        }
    }

    4.市緩存實現(xiàn)CityCacheMemoryImpl
    /*
     * 
     
    */
    package com.chenlb.study.ehcache;

    import java.util.List;

    import net.sf.ehcache.Cache;
    import net.sf.ehcache.Element;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    /**
     * 2007-8-2
     * 
    @author chenlb
     * 
     
    */
    public class CityCacheMemoryImpl implements CityCache{
        
    private static final Log logger = LogFactory.getLog(CityCacheMemoryImpl.class);
        
    private Cache cityCache;
        
        
    /* (non-Javadoc)
         * @see com.chenlb.study.ehcache.CityCache#getCitys(java.lang.Integer)
         
    */
        
    public List<IdAndName> getCitys(Integer provinceId) {
            
    // TODO Auto-generated method stub
            Element element = cityCache.get(provinceId);
            
    if(element == null) {
                List
    <IdAndName> lt = CityData.getCityByProvince(provinceId);//可以認為是數(shù)據(jù)查詢
                element 
    = new Element(provinceId, lt);
                cityCache.put(element);
                
    if(logger.isInfoEnabled()) {
                    logger.info(
    "城市加載到緩存");
                }
            }
            
    return (List<IdAndName>) element.getValue();
        }


        
    public void setCityCache(Cache cityCache) {
            
    this.cityCache = cityCache;
        }

    }
    說明:getcitys()方法重要,要加載沒有的內(nèi)容


    5.ehcache.xml同時把ehcache.xsd放到同一目錄
    <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
    ="120"
                timeToLiveSeconds
    ="120"
                overflowToDisk
    ="true"
                maxElementsOnDisk
    ="10000000"
                diskPersistent
    ="false"
                diskExpiryThreadIntervalSeconds
    ="120"
                memoryStoreEvictionPolicy
    ="LRU"
                
    />
        
    <cache name="cityCache"
               maxElementsInMemory
    ="10"   //最大緩存10個對象
               eternal
    ="false"   //不是永久有效
               overflowToDisk
    ="false" //緩存滿了不寫的磁盤
               timeToIdleSeconds
    ="1"   //1秒后沒有讀就失效
               timeToLiveSeconds
    ="2" //2秒后失效
               memoryStoreEvictionPolicy
    ="LFU"
                
    />
    </ehcache>

    6.Spring 配置文件夾applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

        
    <bean id="cacheManager"
            class
    ="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            
    <property name="configLocation"
                value
    ="classpath:com/chenlb/study/ehcache/ehcache.xml">
            
    </property>
        
    </bean>
        
        
    <bean id="cityCacheBean" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
            
    <property name="cacheManager" ref="cacheManager"></property>
            
    <property name="cacheName" value="cityCache"></property>
        
    </bean>

        
    <bean id="cityCache" class="com.chenlb.study.ehcache.CityCacheMemoryImpl">
            
    <property name="cityCache" ref="cityCacheBean"></property>
        
    </bean>
    </beans>

    7.測試用例CityCacheTest
    /*
     * 
     
    */
    package com.chenlb.study.ehcache;

    import java.util.List;
    import java.util.Random;

    import junit.framework.TestCase;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    /**
     * 2007-8-2
     * 
    @author chenlb
     * 
     
    */
    public class CityCacheTest extends TestCase {

        
    protected final Log logger = LogFactory.getLog(getClass());

        
    private ApplicationContext context;
        
    private Random random;
        
    /**
         * 
    @throws java.lang.Exception
         
    */
        
    protected void setUp() throws Exception {
            context 
    = new ClassPathXmlApplicationContext(
                    
    new String[] {"application*.xml"}
                );
            random 
    = new Random();
        }

        
    /**
         * 
    @throws java.lang.Exception
         
    */
        
    protected void tearDown() throws Exception {
        }

        
    /**
         * Test method for {
    @link com.chenlb.study.ehcache.CityCache#getCitys(java.lang.Integer)}.
         
    */
        
    public void testGetCitys() {
            CityCache cityCache 
    = (CityCache) context.getBean("cityCache");
            
            List
    <IdAndName> citys = cityCache.getCitys(1);
            
            assertNotNull(citys);
            assertTrue(citys.size() 
    > 0);
            
    for(IdAndName inn : citys) {
                logger.info(
    "省1 : "+inn.getName());
            }
            
            citys 
    = cityCache.getCitys(2);
            assertNotNull(citys);
            assertTrue(citys.size() 
    > 0);
            
    for(IdAndName inn : citys) {
                logger.info(
    "省2 : "+inn.getName());
            }
        }

        
    public void testGetCitysWithLoop() {
            CityCache cityCache 
    = (CityCache) context.getBean("cityCache");
            
            List
    <IdAndName> citys, citys_2;
            
    int loopNum = 5;
            
    int totelTimes = 0;
            
    do {
                logger.info(
    "===============第 "+(6-loopNum)+"次循環(huán)===============");
                citys 
    = cityCache.getCitys(1);
                assertNotNull(citys);
                assertTrue(citys.size() 
    > 0);
                
    for(IdAndName inn : citys) {
                    logger.info(
    "省1 : "+inn.getName());
                }
                citys_2 
    = cityCache.getCitys(2);
                assertNotNull(citys_2);
                assertTrue(citys_2.size() 
    > 0);
                
    for(IdAndName inn : citys_2) {
                    logger.info(
    "省2 : "+inn.getName());
                }
                loopNum    
    --;
                
    try {
                    
    int times = random.nextInt(800+ 400;//400 - 1200 ms
                    Thread.sleep(times);
                    totelTimes 
    += times;
                    logger.info(totelTimes 
    + " ms 后, 間隔: "+times+" ms");
                } 
    catch (InterruptedException e) {
                    
    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    while(loopNum > 0);
            
            loopNum 
    = 5;
            
    do {    //省2
                logger.info("===============第 "+(6-loopNum)+"次循環(huán)===============");
                citys 
    = cityCache.getCitys(2);
                assertNotNull(citys);
                assertTrue(citys.size() 
    > 0);
                
    for(IdAndName inn : citys) {
                    logger.info(
    "省2 : "+inn.getName());
                }
                loopNum    
    --;
                
    try {
                    
    int times = random.nextInt(400+ 400;//400 - 800 ms
                    Thread.sleep(times);
                    totelTimes 
    += times;
                    logger.info(totelTimes
    +" ms 后, 間隔: "+times+" ms");
                } 
    catch (InterruptedException e) {
                    
    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    while(loopNum > 0);
            
        }
    }


    有什么不足的地方請高人指點
    posted on 2007-08-02 14:35 流浪汗 閱讀(3141) 評論(1)  編輯  收藏 所屬分類: OpenSource

    評論:
    # re: Spring ehcache 之"城市"緩存 2008-02-20 17:43 | kuan
    會不會有點太過嵌入的味道?  回復  更多評論
      
    主站蜘蛛池模板: 国产免费一区二区三区免费视频| 国产性爱在线观看亚洲黄色一级片| 成人自慰女黄网站免费大全| 亚洲欧美日韩中文无线码| 亚洲尹人九九大色香蕉网站| 亚洲真人日本在线| 国产又黄又爽又猛的免费视频播放| 毛片免费全部播放无码| 日韩精品无码免费专区网站| 免费在线观看自拍性爱视频| 亚洲欧美国产欧美色欲| 亚洲va成无码人在线观看| 91大神亚洲影视在线| 亚洲成A人片777777| 久久亚洲精品无码播放| 免费国产成人午夜私人影视 | 亚洲中文无码永久免费| 久久精品国产这里是免费| 亚洲一级片免费看| 成年免费a级毛片| 特级毛片免费观看视频| 黄色免费网站在线看| 相泽南亚洲一区二区在线播放| 亚洲人成无码网站在线观看| 亚洲免费人成视频观看| 亚洲日韩乱码久久久久久| 亚洲色图校园春色| 亚洲视频一区在线观看| 亚洲日韩乱码中文无码蜜桃| 中文字幕在线观看亚洲| 亚洲精品在线免费观看视频| 亚洲综合激情六月婷婷在线观看 | 老司机精品免费视频| 巨胸喷奶水www永久免费| 99久久婷婷免费国产综合精品| 久久久精品视频免费观看| 国产一二三四区乱码免费| 国精产品一区一区三区免费视频| a级毛片免费全部播放| 久久久久久久岛国免费播放| 蜜桃成人无码区免费视频网站 |