天天看點

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) {
    }
}