天天看点

springmvc的缓存注解1.概念2.Spring配置文件中加入缓存配置3.使用

springmvc的缓存注解1.概念2.Spring配置文件中加入缓存配置3.使用

Spring为我们提供了几个注解来支持Spring Cache。其核心主要是@Cacheable和@CacheEvict。使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回结果,而使用@CacheEvict标记的方法会在方法执行前或者执行后移除缓存。

1.概念

1.Cacheable

可以标记在一个方法上,也可以标记在一个类上。当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。Spring在缓存方法的返回值时是以键值对进行缓存的,值就是方法的返回结果。

@Cacheable可以指定三个属性,value、key和condition。value属性指定Cache名称,key属性是用来指定Spring缓存方法的返回结果时对应的key的。该属性支持SpringEL表达式。 condition属性指定发生的条件

@Cacheable(value="users", key="#id")
       public User find(Integer id) {
          returnnull;
       }
    @Cacheable(value="users", key="#p0.id")
       public User find(User user) {
          returnnull;
       }
    @Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")      

2.CachePut

对于使用@Cacheable标注的方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。@CachePut也可以声明一个方法支持缓存功能。与@Cacheable不同的是使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。

3.CacheEvict

@CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、condition。其中value、key和condition的语义与@Cacheable对应的属性类似。即value表示清除操作是发生在哪些Cache上的;key表示需要清除的是哪个key。

2.Spring配置文件中加入缓存配置

<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd">
    <cache:annotation-driven/>
     
   <bean id="cacheManager"class="org.springframework.cache.support.SimpleCacheManager">  
        <property name="caches">  
            <set>            
                   <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">  
                    <property name="name" value="default"/>  
                   </bean>     
                   <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">  
                    <property name="name" value="cacheDemo"/>  指定缓存名
                   </bean>  
            </set>  
        </property>  
    </bean>    
</beans>      

3.使用

@Service
public class CachedemoService { 与配置文件中设置的缓存名一致
    @Cacheable(value = "cacheDemo", key = "'cacheDemo'+#id")
    public Object getData(String id) {
        return null;
    }
    @CachePut(value = "cacheDemo", key = "'cacheDemo'+#id")
    public Object putData(String id, Object obj) {
        return obj;
    }
    @CacheEvict(value = "cacheDemo", key = "'cacheDemo'+#id")
    public void delData(String id) {
    }
}