Spring 集成 Ehcache 開啟緩存


1.jar包

1.1.slf4j-api-1.6.1.jar

1.2.ehcache-2.7.0.jar

1.3.spring-core-3.2.0.RELEASE.jar

1.4.spring-context-3.2.0.RELEASE.jar

2.ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <!--磁盤存儲:用來指定緩存在磁盤上的存儲目錄。
    可以使用JavaVM環境變量(user.home, user.dir, java.io.tmpdir)-->
    <diskStore path="d:/ehcache/"></diskStore>
    
    <!-- 默認緩存配置 -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="true"
        timeToIdleSeconds="120"
        timeToLiveSeconds="12000"
        overflowToDisk="true"
    />
    
    <!-- cache:為指定名稱的對象進行緩存的特殊配置 -->
    <cache 
        name="CorpInfo" 
        maxElementsInMemory="10000" 
        eternal="false"
        timeToIdleSeconds="10000" 
        timeToLiveSeconds="10000" 
        overflowToDisk="true" 
    />

</ehcache>

 

 

3.springBean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:cache="http://www.springframework.org/schema/cache"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
 
  <description>ehcache緩存配置管理文件</description>

  <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
  </bean>

  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache"/>
  </bean>

  <!-- 啟用緩存注解開關 -->
  <cache:annotation-driven cache-manager="cacheManager"/>
 
</beans>

 

 

 

 

 

4.注解使用方法

4.1.@Cacheable

主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存。

value 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 例如:
@Cacheable(value=” menuCache”) 或者 
@Cacheable(value={”cache1”,”cache2”}
key 緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合 @Cacheable(value=”menuCache”,key=”#userName”)
condition

緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進行緩存

例如:
@Cacheable(value=”menuCache”,

condition=”#userName.length()>2”)

 

 

 

 

 

 

 

 

 

 

 

4.2.@CachePut

  主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存,和 @Cacheable 不同的是,它每次都會觸發真實方法的調用。

value 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 例如:
@Cacheable(value=” menuCache”) 或者 
@Cacheable(value={”cache1”,”cache2”}
key 緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合

例如:
@Cacheable(value=”menuCache”,

key=”#userName”)

condition 緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進行緩存

例如:
@Cacheable(value=”menuCache”,

condition=”#userName.length()>2”)

 

 

 

 

 

 

 

 

 

 
 

 

 

4.3.@CachEvict

主要針對方法配置,能夠根據一定的條件對緩存進行清空。

value 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 例如:
@CachEvict(value=” menuCache”) 或者 
@CachEvict(value={”cache1”,”cache2”}
key 緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合 例如:
@CachEvict(value=” menuCache”,key=”#userName”)
condition 緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才清空緩存 例如:
@CachEvict(value=” menuCache”,
condition=”#userName.length()>2”)
allEntries 是否清空所有緩存內容,缺省為 false,如果指定為 true,則方法調用后將立即清空所有緩存 例如:
@CachEvict(value=” menuCache”,allEntries=true)
beforeInvocation 是否在方法執行前就清空,缺省為 false,如果指定為 true,則在方法還沒有執行的時候就清空緩存,缺省情況下,如果方法執行拋出異常,則不會清空緩存 例如:
@CachEvict(value=”menuCache”,beforeInvocation=true)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.案例

在 service 層實現類中,需要放入緩存的方法前添加注解

    @Override
    @Cacheable(value = "CorpInfo",key = "#cacheParam")
    public List<Ztree> getCorpZtreeList(String cacheParam) {
        List<CorpInfo> allCorpList = this.corpDao.findAll();
        List<Ztree> ztreeList = new ArrayList<Ztree>();
        
            /**
             * 處理邏輯代碼
             * */

        return ztreeList;
    }                

 


免責聲明!

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



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