首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring 整合 EhCache 實戰:基於 AOP 的方法級緩存完整配置與驗證

     編輯:Java 綜合教學

EhCache 核心優勢

  • 純 Java,進程內緩存,極快;
  • 雙層存儲(內存+磁盤),容量無憂;
  • 豐富過期策略(TTI/TTL)、分布式支持(RMI/JGroups/JMS);
  • Hibernate 默認 CacheProvider,生態成熟。

缺點:磁盤存儲佔用大、突發殺進程可能導致數據衝突(重建緩存機制),不適合強持久化場景(可改用 Berkeley DB JE)。

項目環境

Eclipse + Maven + Spring 3.2.8 + EhCache 2.8.2 + JUnit 4.10

Maven 依賴(關鍵片段)

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.8.2</version>
</dependency>
<!-- Spring context-support 必須引入,提供 EhCacheFactoryBean -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>

EhCache 配置文件(ehcache-setting.xml)

<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
        maxElementsInMemory="10000" eternal="false"
        overflowToDisk="true" timeToIdleSeconds="10"
        timeToLiveSeconds="20" diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"/>

    <cache name="cacheTest"
        maxElementsInMemory="1000" eternal="false"
        overflowToDisk="true" timeToIdleSeconds="10"
        timeToLiveSeconds="20"/>
</ehcache>

關鍵屬性:eternal=true 永不過期(忽略 TTI/TTL);overflowToDisk 內存溢出寫磁盤(要求對象實現 Serializable);memoryStoreEvictionPolicy LRU/FIFO/LFU。

Spring 配置(application.xml)

<beans xmlns:cache="https://www.springframework.org/schema/cache" ...>
    <context:component-scan base-package="com.luo.service"/>
    <cache:annotation-driven cache-manager="cacheManager"/>

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"/>
    </bean>
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache-setting.xml"/>
    </bean>
</beans>

業務接口與實現

public interface EhCacheTestService {
    String getTimestamp(String param);
}

@Service
public class EhCacheTestServiceImpl implements EhCacheTestService {
    @Cacheable(value="cacheTest", key="#param")
    public String getTimestamp(String param) {
        return String.valueOf(System.currentTimeMillis());
    }
}

value 對應 EhCache 配置中的 cache name;key 支持 SpEL,#param 表示方法參數。

JUnit 測試驗證緩存生效

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application.xml"})
public class EhCacheTestServiceTest extends AbstractJUnit4SpringContextTests {
    @Autowired private EhCacheTestService service;

    @Test
    public void testCache() throws InterruptedException {
        System.out.println("第一次調用:" + service.getTimestamp("param")); // 進入方法,生成時間戳
        Thread.sleep(2000);
        System.out.println("2秒後調用:" + service.getTimestamp("param"));   // 命中緩存,返回相同時間戳
        Thread.sleep(11000); // 超過 TTL 20s? 此處配置 10s TTI + 20s TTL,實際以 TTL 為準
        System.out.println("再過11秒調用:" + service.getTimestamp("param")); // 緩存失效,重新生成
    }
}

輸出結果分析

第一次調用:1484723456789
2秒後調用:1484723456789   <-- 相同,緩存命中
再過11秒調用:1484723467890 <-- 不同,緩存過期重新執行

測試輸出截圖

源碼下載

完整工程源碼

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved