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
來源:https://my.oschina.net/iblike/blog/71260


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM