在Spring緩存機制中,包括了兩個方面的緩存操做:1.緩存某個方法返回的結果;2.在某個方法執行前或後清空緩存。java
spring是怎麼進行緩存的,白話點講就是:一個map來進行緩存,當調用aop時訪問緩存,判斷是否有對應數據存在。具體以下:spring
Spring僅僅是提供了對緩存的支持,但它並無任何的緩存功能的實現,spring使用的是第三方的緩存框架來實現緩存的功能。其中,spring對EHCache提供了很好的支持。下面咱們以EHCache爲例來介紹spring的緩存配置。緩存
在介紹Spring的緩存配置以前,咱們先看一下EHCache是如何配置。框架
<?xml version="1.0" encoding="UTF-8" ?><ehcache> <!-- 定義默認的緩存區,若是在未指定緩存區時,默認使用該緩存區 --> <defaultCache maxElementsInMemory="500" eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LFU"> </defaultCache> <!-- 定義名字爲"dao.select"的緩存區 --> <cache name="dao.select" maxElementsInMemory="500" eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LFU" /></ehcache>
因爲Spring的緩存機制是基於Spring的AOP,那麼在Spring Cache中應該存在着一個Advice。沒錯,在Spring Cache中的Advice是存在的,它就是org.springframework.cache.Cache,但 spring並非直接使用org.springframework.cache.Cache,spring把Cache對象交給 org.springframework.cache.CacheManager來管理。ide
在spring對EHCache的支持中,org.springframework.cache.ehcache.EhCacheManager就是org.springframework.cache.CacheManager的一個實現spa
<!-- 該Bean是一個org.springframework.cache.CacheManager對象 屬性cacheManager是一個net.sf.ehcache.CacheManager對象 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager"> <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache-config.xml"></property> </bean> </property> </bean>
在上一節中,咱們獲得了cacheManagr,那麼咱們就能夠對某些方法配置緩存了。下面是基於xml方式的緩存配置.net
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd "> <context:component-scan base-package="com.sin90lzc"></context:component-scan> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager"> <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache-config.xml"></property> </bean> </property> </bean> <cache:advice id="cacheAdvice" cache-manager="cacheManager"> <cache:caching> <cache:cacheable cache="dao.select" method="select" key="#id" /> <cache:cache-evict cache="dao.select" method="save" key="#obj" /> </cache:caching> </cache:advice> <aop:config> <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* com.sin90lzc.train.spring_cache.simulation.DaoImpl.*(..))"/> </aop:config></beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> <context:component-scan base-package="com.sin90lzc"></context:component-scan> <!-- 該Bean是一個org.springframework.cache.CacheManager對象 屬性cacheManager是一個net.sf.ehcache.CacheManager對象 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager"> <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache-config.xml"></property> </bean> </property> </bean> <cache:annotation-driven /></beans>
package com.sin90lzc.train.spring_cache.simulation;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Component; @Componentpublic class DaoImpl implements Dao { /** * value定義了緩存區(緩存區的名字),每一個緩存區能夠看做是一個Map對象 * key做爲該方法結果緩存的惟一標識, */ @Cacheable(value = { "dao.select" },key="#id") @Override public Object select(int id) { System.out.println("do in function select()"); return new Object(); } @CacheEvict(value = { "dao.select" }, key="#obj") @Override public void save(Object obj) { System.out.println("do in function save(obj)"); } }