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

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

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

    Jason ---分享,共同進步

    激情成就夢想,努力創造未來
    隨筆 - 53, 文章 - 1, 評論 - 45, 引用 - 0
    數據加載中……

    Ehcache緩存配置

    近期項目用到Ehcache,以前項目主要用到Oscache,并且緩存也是針對orm來處理,只要配置就好,不需要自定義緩存,不需管理緩存。下面描述一下,Spring+Ehcache來處理緩存。

    1,首先引入jar包就不說了環境也不說了,要引入ehcache.xml文件(ehcache配置文件)。

    Xml代碼 復制代碼
    1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    2.          xsi:noNamespaceSchemaLocation="ehcache.xsd">  
    3.   
    4.       <diskStore path="java.io.tmpdir"/>  
    5.   
    6.       <cacheManagerEventListenerFactory class="" properties=""/>  
    7.   
    8.   
    9.     <cacheManagerPeerListenerFactory  
    10.             class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/>  
    11.   
    12.   
    13.      <defaultCache  
    14.             maxElementsInMemory="10000"  
    15.             eternal="false"  
    16.             timeToIdleSeconds="120"  
    17.             timeToLiveSeconds="120"  
    18.             overflowToDisk="true"  
    19.             diskSpoolBufferSizeMB="30"  
    20.             maxElementsOnDisk="10000000"  
    21.             diskPersistent="false"  
    22.             diskExpiryThreadIntervalSeconds="120"  
    23.             memoryStoreEvictionPolicy="LRU"  
    24.             />  
    25.        
    26.     <Cache  name="InstantCache"  
    27.             maxElementsInMemory="100000"  
    28.             eternal="false"  
    29.             timeToIdleSeconds="120"  
    30.             timeToLiveSeconds="300"  
    31.             overflowToDisk="true"  
    32.             diskSpoolBufferSizeMB="30"  
    33.             maxElementsOnDisk="10000000"  
    34.             diskPersistent="false"  
    35.             diskExpiryThreadIntervalSeconds="120"  
    36.             memoryStoreEvictionPolicy="LRU"  
    37.             />    
    38.     <Cache  name="fixCache"  
    39.             maxElementsInMemory="100000"  
    40.             eternal="true"  
    41.             timeToIdleSeconds="120"  
    42.             timeToLiveSeconds="120"  
    43.             overflowToDisk="false"  
    44.             diskSpoolBufferSizeMB="30"  
    45.             maxElementsOnDisk="10000000"  
    46.             diskPersistent="false"  
    47.             diskExpiryThreadIntervalSeconds="120"  
    48.             memoryStoreEvictionPolicy="LRU"  
    49.             />            
    50.     <Cache  name="methodCache"  
    51.             maxElementsInMemory="100000"  
    52.             eternal="false"  
    53.             timeToIdleSeconds="300000"  
    54.             timeToLiveSeconds="600000"  
    55.             overflowToDisk="true"  
    56.             diskSpoolBufferSizeMB="30"  
    57.             maxElementsOnDisk="10000000"  
    58.             diskPersistent="false"  
    59.             diskExpiryThreadIntervalSeconds="120"  
    60.             memoryStoreEvictionPolicy="LRU"  
    61.             />  
    62.        
    63.   
    64.   
    65. </ehcache>  
    66.    

     2,自定義Ehcache配置文件ehcache-cfg.xml

    Xml代碼 復制代碼
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
    3.     "http://www.springframework.org/dtd/spring-beans.dtd">  
    4. <beans>  
    5.     <!-- 引用ehCache的配置 -->  
    6.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
    7.         <property name="configLocation">  
    8.             <value>/WEB-INF/ehcache.xml</value>  
    9.         </property>  
    10.     </bean>  
    11.   
    12.     <!-- 配置即時緩存 Start-->  
    13.     <bean id="instantCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">  
    14.         <property name="cacheManager">  
    15.             <ref local="cacheManager" />  
    16.         </property>  
    17.         <property name="cacheName">  
    18.             <value>instantCache</value>  
    19.         </property>  
    20.     </bean>  
    21.     <!-- 配置即時緩存 End-->  
    22.   
    23.     <!-- 配置固定緩存 Start-->  
    24.     <bean id="fixCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">  
    25.         <property name="cacheManager">  
    26.             <ref local="cacheManager" />  
    27.         </property>  
    28.         <property name="cacheName">  
    29.             <value>fixCache</value>  
    30.         </property>  
    31.     </bean>  
    32.     <bean id="fixCacheInterceptor" class="<SPAN style="COLOR: #000000">FixCacheInterceptor</SPAN>"></bean>  <SPAN style="COLOR: #ff0000"> <!--紅色字為自定類--></SPAN>  
    33.     <bean id="fixCacheAfterAdvice" class="<SPAN style="COLOR: #000000">FixCacheAfterAdvice</SPAN>"></bean><SPAN style="COLOR: #ff0000"> <!--紅色字為自定類--></SPAN>  
    34.   
    35.     <bean id="fixCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
    36.         <property name="advice">  
    37.             <ref local="fixCacheInterceptor" />  
    38.         </property>  
    39.         <property name="patterns">  
    40.             <list><!-- 不能為空,不然SpringHelper會報拿不到bean的錯 -->  
    41.                 <value>.*findAllConfig.*</value>  
    42.                 <value>.*getConfig.*</value>  
    43.                 <value>.*queryConfig.*</value>  
    44.                 <value>.*listConfig.*</value>  
    45.                 <value>.*searchConfig.*</value>  
    46.                 <value>.*loadConfig.*</value>  
    47.                 </list>  
    48.         </property>  
    49.     </bean>  
    50.     <bean id="fixCachePointCutAdivsor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
    51.         <property name="advice">  
    52.             <ref local="fixCacheAfterAdvice" />  
    53.         </property>  
    54.         <property name="patterns">  
    55.             <list>  
    56.                 <value>.*createConfig.*</value>  
    57.                 <!--value>.*saveConfig.*</value-->  
    58.                 <!--value>.*addConfig.*</value-->  
    59.                 <!--value>.*updateConfig.*</value-->  
    60.                 <!--value>.*delConfig.*</value>  
    61.                 <value>.*deleteConfig.*</value>  
    62.                 <value>.*modConfig.*</value-->  
    63.                 <value>.*freeConfig.*</value>  
    64.                 <value>.*bindConfig.*</value>  
    65.             </list>  
    66.         </property>  
    67.     </bean>  
    68.     <!-- 配置固定緩存 End-->  
    69.   
    70.     <!-- 配置接口緩存 Start-->  
    71.     <bean id="methodCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">  
    72.         <property name="cacheManager">  
    73.             <ref local="cacheManager" />  
    74.         </property>  
    75.         <property name="cacheName">  
    76.             <value>methodCache</value>  
    77.         </property>  
    78.     </bean>  
    79.     <!-- find/create cache攔截器 excludeMethods 過濾的方法-->  
    80.     <bean id="methodCacheInterceptor" class="<SPAN style="COLOR: #000000">MethodCacheInterceptor</SPAN>">  
    81.         <property name="excludeMethods">  
    82.             <value>  
    83.                 deleteConfig,findConfig   
    84.             </value>  
    85.         </property>  
    86.     </bean>  
    87.     <!-- flush cache攔截器 excludeMethods 過濾的方法-->  
    88.     <bean id="methodCacheAfterAdvice" class="<SPAN style="COLOR: #000000">MethodCacheAfterAdvice</SPAN>">  
    89.         <property name="excludeMethods">  
    90.             <value>  
    91.                 deleteConfig,findConfig   
    92.             </value>  
    93.         </property>  
    94.     </bean>  
    95.     <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
    96.         <property name="advice">  
    97.             <ref local="methodCacheInterceptor" />  
    98.         </property>  
    99.         <property name="patterns">  
    100.             <list>  
    101.                 <value>.*find.*</value>  
    102.                 <value>.*get.*</value>  
    103.                 <value>.*query.*</value>  
    104.                 <value>.*list.*</value>  
    105.                 <value>.*search.*</value>  
    106.                 <value>.*load.*</value>  
    107.             </list>  
    108.         </property>  
    109.     </bean>  
    110.     <bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
    111.         <property name="advice">  
    112.             <ref local="methodCacheAfterAdvice" />  
    113.         </property>  
    114.         <property name="patterns">  
    115.             <list>  
    116.                 <value>.*create.*</value>  
    117.                 <value>.*save.*</value>  
    118.                 <value>.*add.*</value>  
    119.                 <value>.*update.*</value>  
    120.                 <value>.*del.*</value>  
    121.                 <value>.*delete.*</value>  
    122.                 <value>.*mod.*</value>  
    123.                 <value>.*free.*</value>  
    124.                 <value>.*bind.*</value>  
    125.             </list>  
    126.         </property>  
    127.     </bean>  
    128.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
    129.         <property name="proxyTargetClass">  
    130.             <value>false</value>  
    131.         </property>  
    132.         <property name="beanNames">  
    133.             <list>  
    134.                 <value>*BO</value>  
    135.             </list>  
    136.         </property>  
    137.         <property name="interceptorNames">  
    138.             <list>  
    139.                 <value>fixCachePointCut</value>  
    140.                 <value>fixCachePointCutAdivsor</value>  
    141.                 <value>methodCachePointCut</value>  
    142.                 <value>methodCachePointCutAdvice</value>  
    143.             </list>  
    144.         </property>  
    145.     </bean>  
    146.     <!-- 配置接口緩存 End -->  
    147.   
    148.     <!-- 刷新固定緩存定時器 Start-->  
    149.     <bean id="FixJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">  
    150.         <property name="jobClass">  
    151.             <value>com.ot.opf.timer.FixCacheJob</value>  
    152.         </property>  
    153.     </bean>  
    154.     <bean id="FixCacheTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
    155.         <property name="jobDetail" ref="FixJobDetail" />  
    156.         <property name="cronExpression">  
    157.             <value>0 0 2 * * ?</value>  
    158.         </property>  
    159.     </bean>  
    160.     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
    161.         <property name="triggers">  
    162.            <list><ref bean="FixCacheTrigger"/></list>  
    163.         </property>  
    164.     </bean>  
    165.     <!-- 刷新固定緩存定時器 End -->  
    166. </beans>  

     3,增加相應緩存的管理類

    (1)FixEhCacheManager

    Java代碼 復制代碼
    1. import java.util.List;   
    2.   
    3. import net.sf.ehcache.Cache;   
    4. import net.sf.ehcache.Element;   
    5.   
    6. import org.apache.log4j.Logger;   
    7.   
    8.   
    9.   
    10. public class FixEhCacheManager {   
    11.     private static Cache fixCache = (Cache)SpringHelper.getBean("fixCache");   
    12.     private static Logger logger = Logger.getLogger(FixEhCacheManager.class);   
    13.   
    14.     private static boolean isStop = true;//初始化暫停   
    15.   
    16.     public synchronized static boolean isStop() {   
    17.         return isStop;   
    18.     }   
    19.   
    20.     public synchronized static void setStop(boolean isStop) {   
    21.         FixEhCacheManager.isStop = isStop;   
    22.     }   
    23.   
    24.     public synchronized static void put(Object key,Object value){   
    25.         if(!isStop()){   
    26.             Element e = new Element(key,value);   
    27.             logger.debug("cache object to fixCache... key:"+key+",size of value"+e.getSerializedSize());   
    28.             fixCache.put(e);   
    29.         }   
    30.     }   
    31.   
    32.     public synchronized static Object get(Object key){   
    33.         if(isStop())return null;   
    34.         Element element = fixCache.get(key);   
    35.         if(element!=null)logger.debug("get object from fixCache... key:"+key);   
    36.         return element == null ? null : element.getObjectValue();   
    37.     }   
    38.   
    39.     public synchronized static void remove(Object key){   
    40.         logger.debug("remove object from fixCache... key:"+key);   
    41.         fixCache.remove(key);   
    42.     }   
    43.   
    44.     public synchronized static void removeAll(){   
    45.         logger.debug("remove all object from fixCache");   
    46.         fixCache.removeAll();   
    47.     }   
    48.   
    49.     public synchronized static List getKeys(){   
    50.         return fixCache.getKeys();   
    51.     }   
    52.   
    53.     public synchronized static int getSize(){   
    54.         return fixCache.getSize();   
    55.     }   
    56.   
    57.     public synchronized static String getCacheName(){   
    58.         return fixCache.getName();   
    59.     }   
    60.   
    61.     public synchronized static String getCacheKey(Class _class, String methodName, Object... arguments) {   
    62.         StringBuffer sb = new StringBuffer("fixCache_");   
    63.         sb.append(_class.getName()).append(".").append(methodName);   
    64.         if ((arguments != null) && (arguments.length != 0)) {   
    65.             for (int i = 0; i < arguments.length; i++) {   
    66.                 sb.append(".").append(arguments[i]);   
    67.             }   
    68.         }   
    69.         return sb.toString();   
    70.     }   
    71. }  

     (2)InstantEhCacheManager

    Java代碼 復制代碼
    1. import java.util.List;   
    2.   
    3. import net.sf.ehcache.Cache;   
    4. import net.sf.ehcache.Element;   
    5.   
    6. import org.apache.log4j.Logger;   
    7.   
    8.   
    9.   
    10.   
    11. public class InstantEhCacheManager {   
    12.     private static Cache instantCache = (Cache)SpringHelper.getBean("instantCache");   
    13.     private static Logger logger = Logger.getLogger(InstantEhCacheManager.class);   
    14.   
    15.     private static boolean isStop = true;//初始化停止   
    16.   
    17.     public synchronized static boolean isStop() {   
    18.         return isStop;   
    19.     }   
    20.   
    21.     public synchronized static void setStop(boolean isStop) {   
    22.         InstantEhCacheManager.isStop = isStop;   
    23.     }   
    24.   
    25.     public synchronized static void put(Object key,Object value){   
    26.         if(!isStop()){   
    27.             Element e = new Element(key,value);   
    28.             logger.debug("cache object to instantCache... key:"+key+",size of value??"+e.getSerializedSize());   
    29.             instantCache.put(e);   
    30.         }   
    31.     }   
    32.   
    33.     public synchronized static Object get(Object key){   
    34.         if(isStop())return null;   
    35.         Element element = instantCache.get(key);   
    36.         if(element!=null)logger.debug("get object from instantCache... key:"+key);   
    37.         return element == null ? null : element.getObjectValue();   
    38.     }   
    39.   
    40.     public synchronized static void remove(Object key){   
    41.         logger.debug("remove object from instantCache... key:"+key);   
    42.         instantCache.remove(key);   
    43.     }   
    44.   
    45.     public synchronized static void removeAll(){   
    46.         logger.debug("remove all object from instantCache");   
    47.         instantCache.removeAll();   
    48.     }   
    49.   
    50.     public synchronized static List getKeys(){   
    51.         return instantCache.getKeys();   
    52.     }   
    53.   
    54.     public synchronized static int getSize(){   
    55.         return instantCache.getSize();   
    56.     }   
    57.   
    58.     public synchronized static String getCacheName(){   
    59.         return instantCache.getName();   
    60.     }   
    61.   
    62.     public synchronized static String getCacheKey(Class _class, String methodName, Object... arguments) {   
    63.         StringBuffer sb = new StringBuffer("instantCache_");   
    64.         sb.append(_class.getName()).append(".").append(methodName);   
    65.         if ((arguments != null) && (arguments.length != 0)) {   
    66.             for (int i = 0; i < arguments.length; i++) {   
    67.                 sb.append(".").append(arguments[i]);   
    68.             }   
    69.         }   
    70.         return sb.toString();   
    71.     }   
    72. }  

     

    (3)MethodEhCacheManager

    Java代碼 復制代碼
    1. import java.util.List;   
    2.   
    3. import net.sf.ehcache.Cache;   
    4. import net.sf.ehcache.Element;   
    5.   
    6. import org.apache.log4j.Logger;   
    7.   
    8.   
    9.   
    10. public class MethodEhCacheManager {   
    11.     private static Cache methodCache = (Cache)SpringHelper.getBean("methodCache");   
    12.     private static Logger logger = Logger.getLogger(MethodEhCacheManager.class);   
    13.   
    14.     private static boolean isStop = true;//初始化暫停   
    15.   
    16.     public synchronized static boolean isStop() {   
    17.         return isStop;   
    18.     }   
    19.   
    20.     public synchronized static void setStop(boolean isStop) {   
    21.         MethodEhCacheManager.isStop = isStop;   
    22.     }   
    23.   
    24.     public synchronized static void put(Object key,Object value){   
    25.         if(!isStop()){   
    26.             Element e = new Element(key,value);   
    27.             logger.debug("cache object to methodCache ... key:"+key+",size of value??"+e.getSerializedSize());   
    28.             methodCache.put(e);   
    29.         }   
    30.     }   
    31.   
    32.     public synchronized static Object get(Object key){   
    33.         if(isStop())return null;   
    34.         Element element = methodCache.get(key);   
    35.         if(element!=null)logger.debug("get object from methodCache... key:"+key);   
    36.         return element == null ? null : element.getObjectValue();   
    37.     }   
    38.   
    39.     public synchronized static void remove(Object key){   
    40.         logger.debug("remove object from methodCache... key:"+key);   
    41.         methodCache.remove(key);   
    42.     }   
    43.   
    44.     public synchronized static void removeAll(){   
    45.         logger.debug("remove all object from methodCache");   
    46.         methodCache.removeAll();   
    47.     }   
    48.   
    49.     public synchronized static List getKeys(){   
    50.         return methodCache.getKeys();   
    51.     }   
    52.   
    53.     public synchronized static int getSize(){   
    54.         return methodCache.getSize();   
    55.     }   
    56.   
    57.     public synchronized static String getCacheName(){   
    58.         return methodCache.getName();   
    59.     }   
    60.   
    61.     public synchronized static String getCacheKey(Class _class, String methodName, Object... arguments) {   
    62.         StringBuffer sb = new StringBuffer("methodCache_");   
    63.         sb.append(_class.getName()).append(".").append(methodName);   
    64.         if ((arguments != null) && (arguments.length != 0)) {   
    65.             for (int i = 0; i < arguments.length; i++) {   
    66.                 sb.append(".").append(arguments[i]);   
    67.             }   
    68.         }   
    69.         return sb.toString();   
    70.     }   
    71.   
    72. }  

     

    4,aop相應的類,只要對上面的通過BeanName捕獲到,執行相應的AOP處理相應的緩存放入和移出。

    posted on 2010-12-04 11:46 agun 閱讀(2866) 評論(0)  編輯  收藏 所屬分類: java web架構設計與系統分析

    主站蜘蛛池模板: 亚洲综合久久久久久中文字幕| 香蕉免费在线视频| 911精品国产亚洲日本美国韩国| 永久免费无码网站在线观看| 免费A级毛片无码视频| 久青草视频97国内免费影视| 色九月亚洲综合网| 亚洲综合丁香婷婷六月香| 亚洲AV日韩AV永久无码绿巨人| 亚洲精品国产成人影院| 特级淫片国产免费高清视频| 久久受www免费人成_看片中文| 久久国产乱子伦精品免费一 | 免费无码一区二区三区| 国产日韩在线视频免费播放| 深夜A级毛片视频免费| 理论亚洲区美一区二区三区| 亚洲精品中文字幕无码A片老| 亚洲国产av一区二区三区丶| 久久久无码精品亚洲日韩京东传媒 | 亚洲国产精品无码中文字| 亚洲无码日韩精品第一页| 亚洲AV无码乱码在线观看| 亚洲AV无码一区二区三区国产| 国产在线观看免费不卡| 在线观看国产情趣免费视频| 日韩成人免费视频播放| 青春禁区视频在线观看直播免费| 免费黄色福利视频| 久久久久久久免费视频| 女人18毛片水真多免费播放| 好吊妞788免费视频播放| 大陆一级毛片免费视频观看i| 女人18毛片免费观看| 天天拍拍天天爽免费视频| 日本免费人成黄页在线观看视频 | 91在线亚洲精品专区| 亚洲日本视频在线观看| 亚洲导航深夜福利| 亚洲女子高潮不断爆白浆| 337p日本欧洲亚洲大胆人人|