在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)"); } }