EhCache的配置


JPA和Hibernate的二級緩存都是這樣做的

代碼目錄:

 

 

這是基礎的jar包,如果少的話,再去maven下載

<!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <!-- AspectJ --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${org.aspectj-version}</version> </dependency> <!-- Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> </dependency> <!-- AspectJ -->

 

ehcache.xml :

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">
    <cache name="baseCache"
           eternal="false"
           maxEntriesLocalHeap="200"
           overflowToDisk="false"
           diskPersistent="false"
           timeToIdleSeconds="600"
           statistics="true"
           timeToLiveSeconds="600"/>
</ehcache>

 

這里采用兩種bean的配置方式,一種是xml(EhCacheConfig.xml),一種是java(EhCacheConfig.java),如下:

EhCacheConfig.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"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- 啟用緩存 -->
    <cache:annotation-driven cache-manager="cacheManager"/>
    
    <bean id="cm" class="com.spring.ehcache.CacheMethod"></bean>
     
     
     <bean id="ehCacheManagerFactory"
          class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>
    
    <!-- 這個bean的id必須叫 cacheManager,不然會報錯 No bean named 'cacheManager' is defined-->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehCacheManagerFactory"/>
        <property name="transactionAware" value="true"/>
    </bean>

<!-- 可以使用ConcurrentMapCacheManager作為緩存管理器,
  它非常簡單,對應開發,測試或基礎的應用來說,是個不錯的選擇。但對於生產級別的應用不理想
  用這個替換掉上面的ehCacheManagerFactory和cacheManager這兩個bean就行啦

  <bean id="cacheManager" 

    class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
  </bean>

-->

<!--

Spring3.1內置了五個緩存管理器實現

SimpleCacheManager

NoOpCacheManager

ConcurrentMapCacheManager

CompositeCacheManager

EhCacheCacheManager

Spring Data又提供了兩個緩存器

RedisCacheManager

GemfireCacheManager

-->

    
</beans>

EhcacheConfig .java:
package com.spring.ehcache;
import net.sf.ehcache.CacheManager;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
@EnableCaching //啟用緩存
public class EhcacheConfig {
    
    @Bean(name="ehCacheCacheManager")
    public EhCacheCacheManager ehCacheCacheManager(CacheManager cm){
        EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
        ehCacheCacheManager.setTransactionAware(true);
        ehCacheCacheManager.setCacheManager(cm);
        return ehCacheCacheManager;
    }
    @Bean(name="ehCacheManagerFactoryBean")
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        String src = "ehcache.xml";
        System.out.println("EhCacheManagerFactoryBean..");
        EhCacheManagerFactoryBean ehFactoryBean = 
                new EhCacheManagerFactoryBean();
        ehFactoryBean.setConfigLocation(new ClassPathResource(src));
        return ehFactoryBean;
    }
    
    @Bean(name="cm")
    public CacheMethod cacheMethod(){
        
        return new CacheMethod();
        
    }
    
    

}

 

CacheMethod.java: 這個是緩存測試的類,如果有緩存的話,里面的getStr()方法會執行一次,否則會執行多次

package com.spring.ehcache;

import org.springframework.cache.annotation.Cacheable;

public class CacheMethod {
    public CacheMethod(){
        
        System.out.println("CacheMethod..");
    }

//@Cacheable 表明Spring在調用方法之前,首先應該在緩存中查找方法的返回值。如果這個值能夠找到,就返回緩存的值。否則方法被調用,返回值放入緩存中
//@CachePut 表明Spring應該將方式的緩存值放到緩存中。在方法的調用前並不會檢查緩存,方法始終都會被調用
//@CacheEvict 表明Spring應該在緩存中清除一個或多個條目
//@Caching 這是一個分組的注解,能夠同時應用多個其他的緩存注解 @Cacheable(
"baseCache") public String getStr(){ System.out.println("get Str.."); return "test get str"; } }

 

 

TestCache.java

package com.spring.ehcache;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(classes=EhcacheConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
//SpringJUnit4ClassRunner.class使用時要注意,junit的版本要求9以上
public class TestCache {
    @Autowired
    private CacheMethod cm;
    @Autowired
    private EhCacheManagerFactoryBean ehCacheManagerFactoryBean;
    @Autowired
    private EhCacheCacheManager ehCacheCacheManager;
    /**
     * 使用java配置bean
     * */

    @Test
    public void getCache(){
        System.out.println(ehCacheManagerFactoryBean);
        System.out.println(ehCacheCacheManager);
        System.out.println(cm.getStr());
        System.out.println(cm.getStr());
        System.out.println(cm.getStr());
    }
    
    
    /**
     * 使用xml配置bean
     * 
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("com/spring/ehcache/EhCacheConfig.xml");
        System.out.println(app.getBean("ehCacheManagerFactory"));
        System.out.println(app.getBean("cacheManager"));
        System.out.println(((CacheMethod)app.getBean("cm")).getStr());
        System.out.println(((CacheMethod)app.getBean("cm")).getStr());
        System.out.println(((CacheMethod)app.getBean("cm")).getStr());
    }
    */

}

 


免責聲明!

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



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